StandardScaler类
常用的数据归一化方式:减去平均值,然后通过标准差映射到均至为0的空间内。系统会记录每个输入参数的平均数和标准差,以便数据可以还原。
sklearn.preprocessing.StandardScaler能够轻松的实现上述功能,得到的是一个均值为0,方差为1的正态分布的结果
其中包含的方法有:
- fit(X[, y])Compute the mean and std to be used for later scaling.
- 计算用于后续缩放的平均值和标准差
- transform(X[, y, copy])Perform standardization by centering and scaling
- 通过居中和缩放执行标准化
- fit_transform(X[, y])Fit to data, then transform it.
- 直接计算平均值和std,并执行转换
- get_params([deep])Get parameters for this estimator.
- 获取此估计器的参数
- inverse_transform(X[, copy])Scale back the data to the original representation
- 将数据缩小到原始表示
- partial_fit(X[, y])Online computation of mean and std on X for later scaling.
- 在线计算X上的平均值和标准值,以便以后缩放
- set_params(**params)Set the parameters of this estimator.
- 设置此估计器的参数
引入StandardScaler类:
from sklearn.preprocessing import StandardScaler
fit
使用fit函数可以计算数据的平均值和std,用于后续transformer,一般和transformer一起使用
transform
transform函数的作用是根据类中保存的均值和标准差,对数据进行变换
在使用transformer前要保证类中已保存了均值和标准差数据
fit_transform
fit_transform方法是fit和transform的结合,fit_transform(X_train) 意思是找出X_train的均值和标准差,并应用在X_train上
对于X_test,可以直接使用transform方法。因为此时StandardScaler已经保存了X_train的均值和标准差
使用方法
随机生成一组数据
import numpy as np
df_data = np.arange(20).reshape(4, 5)
print(df_data)
print(df_data[0])
print(df_data[</