Skip to content

Instantly share code, notes, and snippets.

@leandrocl2005
Created November 30, 2018 16:02
Show Gist options
  • Save leandrocl2005/c108ea1cd6cbb17f1fa465b79edac683 to your computer and use it in GitHub Desktop.
Save leandrocl2005/c108ea1cd6cbb17f1fa465b79edac683 to your computer and use it in GitHub Desktop.
# bibliotecas
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
# remove warnings
import warnings
warnings.filterwarnings("ignore")
# dataset
iris = load_iris()
# features e target
X = iris.data
y = iris.target
mean_scores = []
for k in range(1,51):
scores = []
for i in range(60):
X_train, X_test, y_train, y_test = train_test_split(X,y)
model = KNeighborsClassifier(n_neighbors=k)
model.fit(X_train,y_train)
accuracy = model.score(X_test,y_test)
scores.append(accuracy)
mean_scores.append(np.mean(scores))
import matplotlib.pyplot as plt
import seaborn as sns
plt.plot(np.arange(1,51),mean_scores)
plt.yticks([])
plt.title("Acurácias do k-NN por número de vizinhos")
plt.xlabel("Número de vizinhos")
plt.ylabel("Acurácia")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment