Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # initialize the values of k for our k-Nearest Neighbor classifier along with the
- # list of accuracies for each value of k
- kVals = range(1, 30, 2)
- accuracies = []
- # loop over various values of `k` for the k-Nearest Neighbor classifier
- for k in range(1, 30, 2):
- # train the k-Nearest Neighbor classifier with the current value of `k`
- model = KNeighborsClassifier(n_neighbors=k)
- model.fit(trainData, trainLabels)
- # evaluate the model and update the accuracies list
- score = model.score(valData, valLabels)
- print("k=%d, accuracy=%.2f%%" % (k, score * 100))
- accuracies.append(score)
- # find the value of k that has the largest accuracy
- i = int(np.argmax(accuracies))
- print("k=%d achieved highest accuracy of %.2f%% on validation data" % (kVals[i],
- accuracies[i] * 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement