码迷,mamicode.com
首页 > 其他好文 > 详细

数据归一化 scikit-learn中的Scaler

时间:2020-07-30 01:46:35      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:stat   dsc   orm   取数据   ESS   targe   nump   mode   state   

 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)

 

数据归一化 scikit-learn中的Scaler

标签:stat   dsc   orm   取数据   ESS   targe   nump   mode   state   

原文地址:https://www.cnblogs.com/waterr/p/13401292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!