Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Created September 9, 2020 15:10
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 StephenFordham/239580eb26720fbebf0652edc27cbe4e to your computer and use it in GitHub Desktop.
Save StephenFordham/239580eb26720fbebf0652edc27cbe4e to your computer and use it in GitHub Desktop.
error_rate
error_rate = []
for i in range(1, 40):
knn = KNeighborsClassifier(n_neighbors=i)
knn.fit(X_train, y_train)
pred_i = knn.predict(X_test)
error_rate.append(np.mean(pred_i != y_test))
plt.figure(figsize=(10, 6))
plt.plot(range(1, 40), error_rate, color='blue', linestyle='--',
markersize=10, markerfacecolor='red', marker='o')
plt.title('K versus Error rate')
plt.xlabel('K')
plt.ylabel('Error rate')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment