X_train X_test y_train y_test train_test_split cancer data cancer targ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=66)
training_accuracy = []
test_accuracy = []
neighbors_setting = range(1, 11)
for n_neighbors in neighbors_setting:
clf = KNeighborsClassifier(n_neighbors=n_neighbors)
clf.fit(X_train, y_train)
training_accuracy.append(clf.score(X_train, y_train))
test_accuracy.append(clf.score(X_test, y_test))
plt.plot(neighbors_setting, training_accuracy, label='правильность на обучающей выборке')
plt.plot(neighbors_setting, test_accuracy, label='правильность на тестовой выборке')
plt.ylabel('Правильность')
plt.xlabel('количество соседей')
plt.legend()
plt.show()