MinMaxScaler详解(inverse_transform)
LSTM中因为需要把数据进行归一化,预测后又要进行逆缩放,所以经常使用MinMaxScaler这个函数,对于逆缩放需要的数据我总是不理解,因此做了以下的工作。#MinMaxScaler常用方法from sklearn.preprocessing import MinMaxScalerscaler = MinMaxScaler(feature_range=(-1, 1))b = scaler.fit
·
LSTM中因为需要把数据进行归一化,预测后又要进行逆缩放,所以经常使用MinMaxScaler这个函数,对于逆缩放需要的数据我总是不理解,因此做了以下的工作。
#MinMaxScaler常用方法
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(-1, 1))
b = scaler.fit_transform(a)
cc=scaler.inverse_transform(c)
我思考的问题:
- MinMaxScaler的归一化原理
- 生成的scaler模型到底记住了什么
- inverse_transform()方法对输入数据的要求是什么
下面就来解决这三个问题!
①原理:
这里提醒两点。①MinMaxScaler模型输入数据必须是二维数据。②axis=0表示是在列中查找最大值/最小值。
The transformation is given by::
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min
where min, max = feature_range.
②关于scaler模型记住了什么,我们可以用具体的实例来观察。首先是代码
a=np.asarray( [[-1, 2], [-0.5, 6], [0, 10], [1, 18]])
c=np.asarray( [[3,7]])
print(c)
scaler = MinMaxScaler(feature_range=(-1, 1))
b = scaler.fit_transform(a)
cc=scaler.inverse_transform(c)
print(b)
print(cc)
输出
[[3 7]]
[[-1. -1. ]
[-0.5 -0.5]
[ 0. 0. ]
[ 1. 1. ]]
[[ 3. 66.]]
详解如下:我们可以发现,模型记住了范围,和原来列的最小值和最大值,即归一化的范围(-1,1),原来第一列的最大值1最小值-1,第二列的最大值18,最小值2.
③. inverse_transform()的输入
根据上面这个实例,我们也应该知道对于逆缩放方法的输入数据应该满足模型所记住的列数。而对于行,也就是样本数,是不限的。还有一种情况是广播数据。
例如创建scaler时输入的是一个维度为(1,5)的矩阵,然后用scaler的inverse_transform
时输入的是(5,1)的矩阵。但这杨也是合理的,因为可以将模型训练的第一列的数据进行广播到逆缩放的5列数据上。
#inverse_transform源码
def inverse_transform(self, X):
"""Undo the scaling of X according to feature_range.
Parameters
----------
X : array-like of shape (n_samples, n_features)
Input data that will be transformed. It cannot be sparse.
更多推荐
已为社区贡献2条内容
所有评论(0)