# coding: utf-8# In12:import numpy as npimport mathimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib.patches as mpatches# In13:P = np.array(0.8,0.8,1,1,1,1,1.2,0.8,1,0.8,1.2,1,0.2,1.2,1,4,3,2,3.8,2.8,2,4.2,2.8,2,3.8,3.2,2,4.2,3.2,2,4.4,2.8,2,4.4,3.2,2,3.2,0.4,3,3.2,0.7,3,3.8,0.5,3,3.5,1,3,4,1,3,4,7,3)# In14:np.random.shuffle(P)# In15:P# In16:#plotting the datafor i in P:  if i2 == 1:    plt.scatter(i0,i1,s=40,c=’g’,marker=’x’)  elif i2 == 2:    plt.scatter(i0,i1,s=40,c=’b’,marker=’+’)  else:    plt.scatter(i0,i1,s=40,c=’y’,marker=’o’)    green_patch = mpatches.Patch(color=’green’, label=’Class 1′)blue_patch = mpatches.Patch(color=’blue’, label=’Class 2′)yellow_patch = mpatches.Patch(color=’yellow’, label=’Class 3′)plt.legend(handles=green_patch, blue_patch, yellow_patch)plt.show()# In17:#preparing for 3-fold cross validationpartition1 = P:len(P)//3partition2 = Plen(P)//3 : -len(P)//3partition3 = P-len(P)//3 :# In18:train_set = test_set = train_set.append(np.concatenate((partition2, partition3), axis = 0))train_set.append(np.concatenate((partition1, partition3), axis = 0))train_set.append(np.concatenate((partition1, partition2), axis = 0))test_set.append(partition1)test_set.append(partition2)test_set.append(partition3)# In19:#defining k_nearest_neighbourdef knn(train, test, k):    distances =     for point_i in train:      dist = math.sqrt((point_i0 – test0)**2 + (point_i1 – test1)**2)       distances.append(dist, point_i2)        distances.sort()    classvotes = {}    for i in distances0:k:        if i1 in classvotes:            classvotesi1 += 1        else:            classvotesi1 = 1    return max(classvotes.keys(), key=(lambda k: classvotesk))  # In20:fold = 3avg_accuracy =     k_value = 3,5,7,9for k in k_value:      #looping k value  accuracy =   for i in range(fold):     #looping for cross-validation    correct = 0    total = 0    for j in range(len(test_seti)):      pred = knn(train_seti, test_setij, k)      if pred == test_setij2:        correct += 1      total += 1    accuracy.append(correct/total)      print(“k = “,k,”  Avg Accuracy :”, round(sum(accuracy)/len(accuracy), 3) )  avg_accuracy.append(sum(accuracy)/len(accuracy), k)print(“Best value of k: “, max(avg_accuracy)1, “,  Highest Accuracy =”,round(max(avg_accuracy)0, 3))# In21:#Classifying the new test point  T = (3,2)  print(“Test point (3,2) is predicted to be of class: “, knn(P, np.array((3,2)) , max(avg_accuracy)1))

x

Hi!
I'm Ruth!

Would you like to get a custom essay? How about receiving a customized one?

Check it out