k-Nearest Neighbor (kNN) exercise
Start forecast
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Now implement the function predict_labels and run the code below: 2 # We use k = 1 (which is Nearest Neighbor). 3 y_test_pred = classifier.predict_labels(dists, k=1) 4 5 # Compute and print the fraction of correctly predicted examples 6 num_correct = np.sum(y_test_pred == y_test) 7 accuracy = float(num_correct) / num_test 8 print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))
Prediction method call is as follows:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def predict_labels(self, dists, k=1): 2 """ 3 Given a matrix of distances between test points and training points, 4 predict a label for each test point. 5 6 Inputs: 7 - dists: A numpy array of shape (num_test, num_train) where dists[i, j] 8 gives the distance betwen the ith test point and the jth training point. 9 10 Returns: 11 - y: A numpy array of shape (num_test,) containing predicted labels for the 12 test data, where y[i] is the predicted label for the test point X[i]. 13 """ 14 num_test = dists.shape[0] 15 y_pred = np.zeros(num_test) 16 for i in range(num_test): 17 # A list of length k storing the labels of the k nearest neighbors to 18 # the ith test point. 19 closest_y = [] 20 ######################################################################### 21 # TODO: # 22 #The Matrix at The Distance to the Find the Use at The k Nearest neighbors at The Ith of # 23 # Testing Point, and to use the Find self.y_train at The Labels of THESE # 24- # neighbors Store THESE Labels in closest_y #.. 25 # Hint: Look up at The function numpy.argsort. # 26 # ########################################### ############################# 27 # ***** the START OF YOUR CODE (the NOT DO DELETE / MODIFY THIS LINE) * **** 28 # argsort function return value is an array index value from small to large, and then remove these index values from 0 to k sections 29 closest_y = self.y_train [np.argsort (dists [I]) [0: k ]] 30 31 # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** 32 ######################################################################### 33 # TODO: # 34 # Now that you have found the labels of the k nearest neighbors, you # 35 # need to find the most common label in the list closest_y of labels. # 36 # Store this label in y_pred[i]. Break ties by choosing the smaller # 37 # label. # 38 ################################################### ###################### 39 # ***** the START OF YOUR CODE (the NOT DO DELETE / MODIFY THIS LINE) ***** 40 # to the MOST Common Element in Find List, you CAN use np.bincount 41 is # number x.bincount than the largest value x 1, each bincount given number of times it appears in the index value x. 42 is # a.argmax () taken as a maximum index element corresponding to 43 is y_pred [I] = np.bincount (closest_y) .argmax () 44 is 45 # *****. OF YOUR the END CODE (the NOT the DELETE the DO / THIS the LINE the MODIFY) ***** 46 is 47 return y_pred
The results are as follows:
Got 137 / 500 correct => accuracy: 0.274000