<jupyter_start><jupyter_text>KNN (K- Nearest Neighbours)<jupyter_code># import the required libraries!

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
# from sklearn.cross_validation import cross_val_score
from collections import Counter<jupyter_output><empty_output><jupyter_text>data processing<jupyter_code># Load the dataset
dataset = pd.read_csv('ml_python_knn_2.csv')
dataset.head()
# Create Features and Label# Creat 
# Considering only two fields in the feature set.
# design matrix features (X) and target vector label (y)
# numpy is required here! (*values)
features = dataset.iloc[:,[2,3]].values
label = dataset.iloc[:,4].values
type(features)
type(label)
# Train Test Split 67:33
# using model_selection
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(
                                features,
                                label,
                                test_size=0.33,
                                random_state=11)
# instantiate learning model (k = 13)
knn = KNeighborsClassifier(n_neighbors=13)
# fitting the model
knn.fit(X_train, y_train)
# predict the response
pred = knn.predict(X_test)
pred
# evaluate accuracy
acc = accuracy_score(y_test, pred) * 100
print('\nThe accuracy of the knn classifier for k = 3 is %f%%' % acc)<jupyter_output>The accuracy of the knn classifier for k = 3 is 81.060606%<jupyter_text>parameter tuning<jupyter_code># creating odd list of k for KNN
neighbors = list(range(1,50,2))
# empty list that will hold cv scores
cv_scores = []<jupyter_output><empty_output><jupyter_text>cross validation<jupyter_code># perform 10-fold cross validation
for k in neighbors:
    knn = KNeighborsClassifier(n_neighbors=k)
    scores = cross_val_score(knn, X_train, y_train, cv=10, scoring='accuracy')
    cv_scores.append(scores.mean())
cv_scores
# changing to misclassification error
MSE = [1 - x for x in cv_scores]
# determining the ideal value for k
optimal_k = neighbors[MSE.index(min(MSE))]
print('\nThe optimal number of neighbors is %d.' % optimal_k)
# plot misclassification error vs k 
plt.plot(neighbors, MSE)
plt.xlabel('Number of Neighbors K')
plt.ylabel('Misclassification Error')
plt.show()<jupyter_output><empty_output><jupyter_text>create model for k = 23<jupyter_code># Train Test Split 67:33
# using model_selection
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(
                                features,
                                label,
                                test_size=0.33,
                                random_state=11)
# instantiate learning model (k = 23)
knn = KNeighborsClassifier(n_neighbors=23)
# fitting the model
knn.fit(X_train, y_train)
# predict the response
pred = knn.predict(X_test)
pred
# evaluate accuracy
acc = accuracy_score(y_test, pred) * 100
print('\nThe accuracy of the knn classifier for k = 23 is %f%%' % acc)
acc<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08731155778894473, 'fraction_numerical': 0.018530150753768845, 'mean_word_length': 3.9843505477308296, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '588533', 'license': 'MIT', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1010, 'n_words': 282, 'path': 'machinelearning/ml_python_knn_2.ipynb', 'repo_name': 'futureseadev/hgwxx7', 'size': '3184'}