<jupyter_start><jupyter_text>K Nearest Neighbors Steps: 1. Standardize the variablesScale all the features to the same size, because distance between values can have surprising effects in KNN.<jupyter_code>%matplotlib inline
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing, model_selection, neighbors, metrics
data = pd.read_csv('dataset/Classified Data.csv')
data = data.drop('Unnamed: 0', axis=1)
display(data)

X = data.drop('TARGET CLASS', axis=1)
y = data['TARGET CLASS']
# Standardize the variables
scaler = preprocessing.StandardScaler()
scaler.fit(X)
X_scaled = pd.DataFrame(data=scaler.transform(X), columns=X.columns)
display(X_scaled)

# Split train and test data from the same set:
X_train, X_test, y_train, y_test = model_selection.train_test_split(X_scaled, y, test_size=0.3)
knn = neighbors.KNeighborsClassifier(n_neighbors=42)
knn.fit(X_train, y_train)

# Make our model predict
y_pred = knn.predict(X_test)

# Analyse our prediction accuracy and visualise with a Confusion Matrix
cm = metrics.confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, annot_kws={"size": 16})
plt.xlabel('Predicted Label', size=16), plt.ylabel('Truth Label', size=16)
print("Accuracy: ", metrics.accuracy_score(y_test, y_pred))
n = 100
err = [None] * n
for i in range(0, n):
    knn = neighbors.KNeighborsClassifier(n_neighbors=i+1)
    knn.fit(X_train, y_train)
    y_pred_i = knn.predict(X_test)
    err[i] = 100*metrics.accuracy_score(y_test, y_pred_i)

# Plot K value vs accuracy for KNN
plt.figure(0, figsize=(24, 5))
plt.plot(range(1, n+1), err, marker='o', markerfacecolor='red', markersize=5)
plt.xlabel('K value', size=18), plt.ylabel('% Accuracy', size=18)
plt.xticks(range(1, 102, 5))
print('From the graph, best value of K seems to be: 42')<jupyter_output>From the graph, best value of K seems to be: 42
{'fraction_non_alphanumeric': 0.12078059071729957, 'fraction_numerical': 0.022151898734177215, 'mean_word_length': 6.131578947368421, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '406069', 'license': 'MIT', 'n_tokens_mistral': 726, 'n_tokens_neox': 682, 'n_words': 191, 'path': 'K Nearest Neighbours/KNN.ipynb', 'repo_name': 'relaxxpls/DS303', 'size': '1896'}