text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.feature_selection import SelectKBest, f_classif

from sklearn.metrics import accuracy_score

from sklearn.metrics import f1_score

from sklearn.neighbors import KNeighborsClassifier

import time

k = 15
selector = SelectKBest(score_func=f_classif, k=k)
X_train_selected = selector.fit_transform(X_train, y_train)
X_test_selected = selector.transform(X_test)

from sklearn.neighbors import KNeighborsClassifier
res1 = time.time()
knn = KNeighborsClassifier()
knn = knn.fit(X_train_selected, y_train)
knn
res2 = time.time()

y_pred1 = knn.predict(X_test_selected)

f1 = f1_score(y_test, y_pred1, pos_label='DDoS')

res1 = time.time()
rf = RandomForestClassifier()
rf.fit(X_train_selected, y_train)
res2 = time.time()

y_pred1 = rf.predict(X_test_selected)
accuracy = accuracy_score(y_test, y_pred1)

f1 = f1_score(y_test, y_pred1, pos_label='DDoS')
print('F1 Score: ', f1)