sklearn.preprocessing数据标准化实现流程

python中对于训练集一般需要标准化,即将原数据的均值变为0,方差变为1

有两种方式:

from sklearn import preprocessing

第一种:使用scale模块直接计算标准化,将标准化的array放在x_scale中,同时可以查看均值和标准差,但是该方式的一个不足是当存在新的样本到来时,无法利用已有的模块直接复用,需要利用mean和std自己计算。

x_scale = preprocessing.scale(DatMat)  
x_scale.mean(axis=0)
x_scale.std(axis=0)

第二种:使用StandardScaler模块计算标准化,可以利用训练集数据建立一个转化的类,类似于实现将mean和std存储在该类中,将数据输入,就可以直接求出结果。

scaler = preprocessing.StandardScaler().fit(datingDatMat)
datingDatMat = scaler.transform(datingDatMat)
new_date = numpy.array([1, 2, 3])
new_date_std = scaler.transform(new_date.reshape(1, -1))

这里的scaler更象是扮演一个计算器的角色,本身并不存储数据。

数据datingDatMat的格式为:

[      [1_sample, features],

            .

            .

       [n_sample, features],

            .

            .

]

即每一行为一个样本,元素为特征,列为样本总数。

官方文档 点击打开链接

参考1 点击打开链接

参考2 点击打开链接

猜你喜欢

转载自blog.csdn.net/z2539329562/article/details/80045481
今日推荐