<jupyter_start><jupyter_text>Nearest neighbor classifierImplement a function that realizes an n-nearest neighbor classifier, i.e. afunction that decides the class label of a test data point from the majorityof the labels among the n nearest training data.Download the files data2-train.dat and data2-test.dat of labeled2D data and determine the recognition accuracy (percentage of correctlyclassified data points) of your classifier for n ∈ {1, 3, 5}.Determine the overall run time for computing the 1-nearest neighbor ofevery data in data2-test.dat.<jupyter_code>import pandas as pd
import numpy as np

from matplotlib import pyplot as plt, rcParams as params
import seaborn as sns

from time import time,clock
from heapq import nsmallest
import matplotlib.patches as mpatches



%matplotlib inline

params['figure.figsize'] = (10.0, 7.0)

# reading test and train data
train_data = pd.read_csv("data2-train.dat",delimiter=' ',header=None)
test_data = pd.read_csv("data2-test.dat",delimiter=' ',header=None)
# giving names to columns
column_names={0:"Attr 1",1:"Attr 2",2:"Class"}
train_data.rename(columns=column_names,inplace=True)
test_data.rename(columns=column_names,inplace=True)
sns.countplot(train_data['Class'])
sns.countplot(test_data['Class'])
sns.scatterplot(x="Attr 1",y="Attr 2",hue="Class",data=train_data)
sns.scatterplot(x="Attr 1",y="Attr 2",hue="Class",data=test_data)
def knn(X_train,y,X_test,neighbours=1,plot=True,print_time=True):
    labels = np.empty(X_test.shape[0])
    
    #  code for plotting the decision boundary
    if plot:        
        # step size for the plot
        # increase h to 1 for having less coffee
        h = 0.1
        
        # range of the visualisation
        x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
        y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
        
        #generating data for plotting
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))
        
        # predicting the values for the test data
        Z = knn(X_train,y_train,np.c_[xx.ravel(), yy.ravel()],neighbours,False,False)
        Z = Z.reshape(xx.shape)
        
        #plotting the mesh
        plt.pcolormesh(xx, yy, Z, cmap=plt.cm.tab10)
        
        # Plot training points
        plt.scatter(X_train[:, 0], X_train[:, 1], c=y, cmap=plt.cm.tab10, edgecolor = 'black')
        plt.xlim(xx.min(), xx.max())
        plt.ylim(yy.min(), yy.max())
        plt.xlabel('Feature 1')
        plt.ylabel('Feature 2')
        plt.title("Binary classification (k = %i)"
                   % (neighbours))    
        plt.show()
    
    #  starting the timer
    start = clock()
    for i,data_point in enumerate(X_test):
        
        # calculating euclidean distance
        diff = X_train- data_point
        sqr = np.square(diff)
        sqr_sum = np.sum(sqr,axis=1)
        
        # sorting the distances and returning the indexes of the points
        nearest_neighbours_idx = np.argpartition(sqr_sum, neighbours)[:neighbours]
        
        # setting the label
        labels[i] = y[nearest_neighbours_idx].mode()[0]
    
    #  stopping the timer
    stop = clock()

    if print_time:
        print("Time taken for %d points: %.4f seconds" % (X_test.shape[0],stop-start) )
    return labels
# method to return percentage accuracy
def accuracy(y_pred,y_actual):
    correct_lables = y_pred == y_actual
    return 100*correct_lables.sum()/correct_lables.shape[0]

X_train = train_data.drop(columns=["Class"]).as_matrix()
y_train = train_data["Class"]
                          
X_test = test_data.drop(columns=["Class"]).as_matrix()
y_test = test_data["Class"]

for neighbour in [1,3,5]:
    y_pred = knn(X_train,y_train,X_train,neighbour)
    print("Traing Accuracy \t for k = %d is %.2f%%" % (neighbour,accuracy(y_pred,y_train)))
    
    y_pred = knn(X_train,y_train,X_test,neighbour,False)
    print("Testing Accuracy\t for k = %d is %.2f%%" % (neighbour,accuracy(y_pred,y_test)))
    
    print("------------------------------------------------")
from sklearn.neighbors import KNeighborsClassifier

for neighbour in [1,3,5]:
    start = clock()
    knn = KNeighborsClassifier(n_neighbors=neighbour)
    knn.fit(train_data.drop(columns=["Class"]),train_data["Class"])
    y_train = knn.predict(train_data.drop(columns=["Class"]))
    
    print("Time taken: %.4f seconds" % (clock()-start) )

    print("Traing Accuracy \t for k = %d is %.2f%%" % (neighbour,accuracy(y_train,train_data["Class"])))
    
    y_test = knn.predict(test_data.drop(columns=["Class"]))
    print("Testing Accuracy\t for k = %d is %.2f%%" % (neighbour,accuracy(y_test,test_data["Class"])))
    
    print("------------------------------------------------")<jupyter_output>Time taken: 0.0147 seconds
Traing Accuracy 	 for k = 1 is 100.00%
Testing Accuracy	 for k = 1 is 88.54%
------------------------------------------------
Time taken: 0.0115 seconds
Traing Accuracy 	 for k = 3 is 94.53%
Testing Accuracy	 for k = 3 is 89.58%
------------------------------------------------
Time taken: 0.0115 seconds
Traing Accuracy 	 for k = 5 is 93.36%
Testing Accuracy	 for k = 5 is 93.75%
------------------------------------------------
{'fraction_non_alphanumeric': 0.16660327120578167, 'fraction_numerical': 0.02092050209205021, 'mean_word_length': 2.7139830508474576, 'pattern_counts': {'":': 0, '<': 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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '636552', 'license': 'Apache-2.0', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1731, 'n_words': 435, 'path': 'Project2/task2_4_2/Nearest Neighbour.ipynb', 'repo_name': 'saikat-roy/Uni-Bonn-Pattern-Recognition', 'size': '5258'}