标签:and ESS cal dataset standard 归一化 size rom int
一:所在包
from sklearn.preprocessing import StandardScaler。
二:步骤
a.将训练集进行fit操作
b.在将训练集进行transform操作,得到均值为0,方差为1的数据集。
c.对测试集进行transform操作,但是不需要在进行fit,应使用训练集fit后得出的参数。
三:代码
import numpy as np from sklearn import datasets from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split iris = datasets.load_iris() x = iris.data y = iris.target x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=666) standard = StandardScaler() standard.fit(x_train) x_train = standard.transform(x_train) x_test_standard = standard.transform(x_test) knn = KNeighborsClassifier(n_neighbors=3,n_jobs=-1) knn.fit(x_train,y_train) score = knn.score(x_test_standard,y_test) print(score)
标签:and ESS cal dataset standard 归一化 size rom int
原文地址:https://www.cnblogs.com/lyr999736/p/10682682.html