# まず読み込む
from sklearn.datasets import load_breast_cancer
# インスタンス生成、データセット分割処理
cancer = load_breast_cancer()
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 = []
# 1-10まででとりあえずやってみる
neigbors_settings = range(1, 11)
# n_neighborsをループ処理して各精度を追加
for n_neighbors in neigbors_settings:
clf = KNeighborsClassifier(n_neighbors=n_neighbors).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(neigbors_settings, training_accuracy, label="training accuracy")
plt.plot(neigbors_settings, test_accuracy, label="test accuracy")
plt.ylabel("Accuracy")
plt.xlabel("n_neigbors")
plt.legend();
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme