归一化
from sklearn import datasets
import numpy as np
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)
#sklearn中的StandarScaler
from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
standardScaler.fit(X_train)
standardScaler.mean_
array([5.83416667, 3.08666667, 3.70833333, 1.17 ])
standardScaler.scale_#数据分布范围,方差~
array([0.81019502, 0.44327067, 1.76401924, 0.75317107])
归一化
X_train = standardScaler.transform(X_train)
print(X_train)
部分结果
这个归一化使用的也是训练集中的均值和方差
X_test_standard = standardScaler.transform(X_test)
使用KNN算法看看分类效果
from sklearn.neighbors import KNeighborsClassifier
knn_clf = KNeighborsClassifier(n_neighbors=3
knn_clf.fit(X_train,y_train)
KNeighborsClassifier(n_neighbors=3)
knn_clf.score(X_test,y_test)#没归一化
0.3333333333333333
使用归一化的数据集,正确率100.。。
knn_clf.score(X_test_standard,y_test)
1.0
另外还自己封装了一个类,可以处理ndim=2的矩阵
import numpy as np
class StandardScaler():
def __init__(self):
self.mean_ = None
self.scale_ = None
def fit(self,X):
"""根据训练数据集X获得数据的均值和方差"""
assert X.ndim == 2,"The dimension of X must be 2"
self.mean_ = np.array([np.mean(X[:,i]) for i in range(X.shape[1])])
self.scale__ = np.array([np.std(X[:,i]) for i in range(X.shape[1])])
def transform(self,X):
"""将X根据这个StandardScaler进行均值方差归一化处理"""
assert X.ndim ==2,"The dimension of X must be 2"
assert self.mean_ is not None and self.scale_ is not None,"must fit before transform"
resX = np.empty(shape=X.shape,dtype=float)
for col in range(X.shape[1]):
resX[:,col] = (X[:,col] - self.mean_[col]) / self.scale_[col]
return resX
fit之后就保存了均值和方差…