Code

Read about **KNN**…

First of all import all the useful libraries which are needed in program.

`import math`

import numpy as np

import matplotlib.pyplot as plt

import operator

import random

import pandas as pd

For working with arrays, **numpy** is used. Also **matplotlib** is a library that is used to plot graphs. Similar to numpy, **pandas** provides high-performance, easy to use structures and data analysis tools.

After importing libraries, second step is to load the dataset. You can find the dataset which I have used **here**.

#load train data of respective class

loaddata_train1=np.loadtxt('.../group4/class1_train.txt')

loaddata_train2=np.loadtxt('.../group4/class2_train.txt')#load test data of respective class

loaddata_test1=np.loadtxt('.../group4/class1_test.txt')

loaddata_test2=np.loadtxt('.../group4/class2_test.txt')#concatenate train and test data of different classes in one variable with axis = 0 (row wise)

loaddata_train=np.concatenate((loaddata_train1, loaddata_train2), axis=0)

loaddata_test=np.concatenate((loaddata_test1, loaddata_test2), axis=0)

After loading the data, it’s time to label the data as our original data is unlabeled

`#label the data - class1 as 1 and class2 = -1 `

label1=np.ones((loaddata_train1.shape[0], 1))

label2=np.ones((loaddata_train2.shape[0], 1)) * -1

r1=np.append(label1, loaddata_train1, axis=1)

r2=np.append(label2, loaddata_train2, axis=1)

#concatenate whole train data

train_data=np.concatenate((r1,r2))

Similarly for test data..

`#label the data - class1 as 1 and class2 = -1`

label3=np.ones((loaddata_test1.shape[0], 1))

label4=np.ones((loaddata_test2.shape[0], 1)) * -1

r3=np.append(label3, loaddata_test1, axis=1)

r4=np.append(label4, loaddata_test2, axis=1)

#concatenate whole test data

test_data=np.concatenate((r3,r4))

Scatter plot of the dataset. Library used for plotting purpose is matplotlib. We use marker ‘o’ for class1 and marker ‘x’ for class2.

`plt.scatter(loaddata_train1[:,0], loaddata_train1[:,1], marker='o', label='Class1')`

plt.scatter(loaddata_train2[:,0], loaddata_train2[:,1], marker='x', label='Class2')

plt.legend()

plt.show()