knn算法预测癌症肿瘤

项目地址

https://gitee.com/lxgzhw/sklearn_study

源码

import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# 导入数据
cancer = load_breast_cancer()

# 划分数据
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=66)

# 创建模型
clf = KNeighborsClassifier(n_neighbors=3)

# 训练模型
training_accuracy = []
test_accuracy = []
# n_neighbors的取值从1到10
neighbors_settings = range(1, 11)

for n_neighbors in neighbors_settings:
    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_settings, training_accuracy, label="training accuracy")
plt.plot(neighbors_settings, test_accuracy, label="test accuracy")
plt.xlabel("n_neighbors")
plt.ylabel("Accuracy")
plt.legend()
plt.show()

你可能感兴趣的:(python,人工智能)