TensorFlow2.x——数据归一化(sklearn.preprocessing.StandardScaler)

数据归一化(sklearn.preprocessing.StandardScaler)

数据归一化能够很好的加速模型的训练,并提高模型的准确度,本篇主要介绍sklearn中的数据预处理方法,当然TensorFlow中也有预处理方法,tf.nn.l2_normalize。

本文介绍的skleran中数据预处理的公式为: x ‾ = x − μ δ \overline{x} = \cfrac{x-\mu}{\delta} x=δxμ ,其中 μ \mu μ为整个数据的平均数, δ \delta δ为方差。

代码示例:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
#对数据进行归一化处理

#由于transform处理处理数据为二维数组,所以要将数据转化一下
#x_train: [none, 28, 28] -> [none, 784]
#对于使用fit_transform 和transform 请参考我的python中的博客(https://blog.csdn.net/qq_40913465/article/details/104274256)
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)  
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)  
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)  

#注意在归一化数据后,之后使用的数据要使用新的归一化数据

以上就是sklearn中数据预处理的方法,注意在归一化数据后,之后使用的数据要使用新的归一化数据。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值