text_plain_output_1.png
from sklearn.feature_selection import SelectKBest, f_classif

from sklearn.model_selection import KFold

from sklearn.model_selection import cross_val_score

from sklearn.neighbors import KNeighborsClassifier

import numpy as np

import pandas as pd

import time

df = pd.read_csv('/kaggle/input/ddos-dataset/Friday-WorkingHours-Afternoon-DDos.pcap_ISCX.csv')

df.columns

columns_to_drop = ['Flow ID', ' Source IP', ' Destination IP', ' Timestamp']
df = df.drop(columns_to_drop, axis=1)

df.columns

df = df[~np.isinf(df['Flow Bytes/s'])]
df.dropna(inplace=True)

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()

k = 5
kf = KFold(n_splits=k)
scores = cross_val_score(knn, X_train_selected, y_train, cv=kf)
for fold_idx, score in enumerate(scores):
    print(f'Fold {fold_idx + 1} accuracy: {score}')
mean_accuracy = np.mean(scores)
print(f'\nMean accuracy: {mean_accuracy}')