83327712 2020-07-30
import numpy as np from sklearn import datasets # 获取数据 iris = datasets.load_iris() X = iris.data y = iris.target # 数据分割 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666) # StandardScaler fit 训练集数据 from sklearn.preprocessing import StandardScaler standardscaler = StandardScaler() standardscaler.fit(X_train) # 对训练集数据归一化 X_train = standardscaler.transform(X_train) # 对测试集数据归一化 X_test_standard = standardscaler.transform(X_test) # 实例化分类器 from sklearn.neighbors import KNeighborsClassifier knn_clf = KNeighborsClassifier(n_neighbors=3) # 分类器 fit 归一化训练集 knn_clf.fit(X_train, y_train) # 用归一化的测试集数据计算预测准确率 knn_clf.score(X_test_standard, y_test)