Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. # initialize the values of k for our k-Nearest Neighbor classifier along with the
  2. # list of accuracies for each value of k
  3. kVals = range(1, 30, 2)
  4. accuracies = []
  5.  
  6. # loop over various values of `k` for the k-Nearest Neighbor classifier
  7. for k in range(1, 30, 2):
  8. # train the k-Nearest Neighbor classifier with the current value of `k`
  9. model = KNeighborsClassifier(n_neighbors=k)
  10. model.fit(trainData, trainLabels)
  11.  
  12. # evaluate the model and update the accuracies list
  13. score = model.score(valData, valLabels)
  14. print("k=%d, accuracy=%.2f%%" % (k, score * 100))
  15. accuracies.append(score)
  16.  
  17. # find the value of k that has the largest accuracy
  18. i = int(np.argmax(accuracies))
  19. print("k=%d achieved highest accuracy of %.2f%% on validation data" % (kVals[i],
  20. accuracies[i] * 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement