数据归一化 scikit-learn中的Scaler
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)
相关推荐
五小郎的学习笔记 2018-06-25
alanlonglong 2020-11-11
Iamthedoctor 2020-09-08
Site 2020-08-20
zhaorui0 2020-06-09
hnyzyty 2020-06-04
LetItBe 2020-02-18
卖小孩的咖啡 2020-01-19
GhostLWB 2020-01-18
GerwelsJI 2020-01-11
ALLWITHLOVE 2019-12-29
xiaoxue 2019-11-08
xueyuediana 2019-09-21
yishujixiaoxiao 2019-11-01
DataCastle 2013-03-23
santiago00 2012-09-24
hualalalalali 2019-07-01
kinghighbury 2019-06-30
mori 2019-06-27