数据归一化 scikit-learn中的Scaler
1 import numpy as np 2 from sklearn import datasets 3 4 # 获取数据 5 iris = datasets.load_iris() 6 X = iris.data 7 y = iris.target 8 9 # 数据分割 10 from sklearn.model_selection import train_test_split 11 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666) 12 13 # StandardScaler fit 训练集数据 14 from sklearn.preprocessing import StandardScaler 15 standardscaler = StandardScaler() 16 standardscaler.fit(X_train) 17 18 # 对训练集数据归一化 19 X_train = standardscaler.transform(X_train) 20 21 # 对测试集数据归一化 22 X_test_standard = standardscaler.transform(X_test) 23 24 # 实例化分类器 25 from sklearn.neighbors import KNeighborsClassifier 26 knn_clf = KNeighborsClassifier(n_neighbors=3) 27 28 # 分类器 fit 归一化训练集 29 knn_clf.fit(X_train, y_train) 30 31 # 用归一化的测试集数据计算预测准确率 32 knn_clf.score(X_test_standard, y_test)