I'm implementing KNN algorithm to predict outliers in network data which contains the following columns: source IP address, source port number, protocol and total bytes transferred. To achieve this, I'm using the Python library sklearn to predict response variable (total bytes) with regards to explanatory variables (source IP address, source port number, protocol).
However, the accuracy of prediction on test data is extremely low (0.21). Any recommendations to improve the accuracy score will be much appreciated.
import pandas as pd from sklearn.neighbors import KNeighborsRegressor import random import numpy as np from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from numpy.random import permutation from sklearn.metrics import accuracy_score import sys import math import netaddr as nt from netaddr import IPAddress #Import csv into dataframe df = pd.read_csv('data.csv') df = df[['sourceaddress','sourceport','protocol','total_bytes']] df_arr = df.as_matrix() for i in range(df_arr.shape): df_arr[i,0] = int(nt.IPAddress(df_arr[i,0])) df = pd.DataFrame(df_arr) df.columns = ['sourceaddress','sourceport','protocol','total_bytes'] df = df.astype('str') # Design matrix X and target vector y X = np.array(df.ix[:, 0:3]) Y = np.array(df['total_bytes']) # split into train and test X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=42) knn = KNeighborsClassifier(n_neighbors=24) # fitting the model knn.fit(X_train, Y_train) # prediction of the response variable pred = knn.predict(X_test) print pred # evaluate accuracy print accuracy_score(Y_test, pred)