Skip to content

Instantly share code, notes, and snippets.

@grohith327
Created June 13, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grohith327/c64745c333fda45563420af0d2960af9 to your computer and use it in GitHub Desktop.
Save grohith327/c64745c333fda45563420af0d2960af9 to your computer and use it in GitHub Desktop.
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
## We take a range of values for K(1 to 20) and find the accuracy
## so that we can visualize how accuracy changes based on value of K
accuracy = []
for n in range(1,21):
clf = KNeighborsClassifier(n_neighbors = n)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
accuracy.append(accuracy_score(y_test,y_pred))
## Plotting the accuracies for different values of K
plt.figure(figsize=(16,9))
plt.plot(range(1,21),accuracy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment