Skip to content

Instantly share code, notes, and snippets.

@jurand71
Created November 16, 2022 08:56
Show Gist options
  • Save jurand71/f834de1d267d2a59e0516d4ff59b4944 to your computer and use it in GitHub Desktop.
Save jurand71/f834de1d267d2a59e0516d4ff59b4944 to your computer and use it in GitHub Desktop.
print("Klasyfikator implementujący metode k-najbliższych sąsiadów z biblioteki scikit-learn")
lix = []
liy = []
index=0
acc=0
from sklearn.neighbors import KNeighborsClassifier
for k in range(1, 100):
neigh = KNeighborsClassifier(n_neighbors=k)
neigh.fit(X_train, y_train)
# Oblicza dokładność modelu dla danych testowych
liy.append(neigh.score(X_test, y_test))
if liy[k-1]>acc:
acc=liy[k-1]
index=k-1
lix.append(k)
plt.plot(lix, liy)
plt.show()
print("Najwięsza dokładność uzyskana została dla k="+str(index+1)+" i wynosi "+str(acc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment