LoginSignup
0
1

More than 5 years have passed since last update.

K-近傍法による訓練/テストセットに対する性能評価

Posted at
# まず読み込む
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();
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1