代码详解:
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier import matplotlib.pyplot as plt from pylab import mpl # 设置显示中文字体 mpl.rcParams["font.sans-serif"] = ["SimHei"] # 设置正常显示符号 mpl.rcParams["axes.unicode_minus"] = False #读取数据 iris = load_iris() #分出训练集和测试集 x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.3,random_state=22) #数据标准化,防止异常点的影响 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.transform(x_test) #创建画布 plt.figure() plt.title("准确率随k值的变化") #打开交互 plt.ion() #网格 plt.grid() #x轴和y轴标注 plt.ylabel("准确率") plt.xlabel("k值") #循环k的取值从1到50 for k in range(1,50): # plt.cla() #定义一个k分类算法对象 estimator = KNeighborsClassifier(n_neighbors=k) #训练 estimator.fit(x_train,y_train) #用测试集测试准确率 y_predict = estimator.predict(x_test) score = estimator.score(x_test, y_test) #画散点图 plt.scatter(k,score) plt.pause(0.1) print("预测结果为:",y_predict) print("对比真实值和预测值:",y_test) print("准确率:",score) #关闭交互模式,并最后显示图像 plt.ioff() plt.show()