# coding=utf-8 from sklearn.datasets import load_iris from sklearn.preprocessing import StandardScaler,MinMaxScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split def knc(): # 水仙花数据 # li.data 即为数据的特征值,是二维的 # li.target 即为数据的目标值 li = load_iris() # 准备数据 x_train,x_test,y_train,y_test = train_test_split(li.data,li.target,test_size=0.25) # 标准化 ss = StandardScaler() x_train = ss.fit_transform(x_train) # 需要用训练集的标准来标准化测试集 x_test = ss.transform(x_test) print(x_train[:10]) # 预测 knn = KNeighborsClassifier(n_neighbors=4) knn.fit(x_train,y_train) print("预测的值为:",knn.predict(x_test)) print("实际的值为:",y_test) print("预测的准确率为:",knn.score(x_test,y_test)) return None if __name__ == '__main__': knc()