Scikit 缩放器和反演不会产生相同的数字?
Sci-Kit Scaler and Inversion Does Not Yield Identical Numbers?
对 Sci-kit 有点陌生,但注意到一些行为对于 minmax 归一化和反转似乎有点奇怪(但也许这是预期的行为??)。我想在反转时我会重新生成原始数据点 - 但是反转生成的数字是 "close" 但不完全相等......这是应该发生的,还是我搞砸了?
#previously imported data is in a 3 column df
scaler = sklearn.preprocessing.MinMaxScaler()
df_norm = df.copy()
(df_norm == df).all()
#RETURNS TRUE
df_norm = scaler.fit_transform(df_norm)
df_norm = scaler.inverse_transform(df_norm)
(df_norm == df.values).all()
#RETURNS FALSE
所以我有点疑惑为什么我有 2 个相同的数据帧,但在缩放和反转之后数据集不再相等?许多数字是相等的,但也有相当一部分不是。奇怪的是,如下所示,有些甚至看起来相同,但在使用 df_norm == df
进行测试时却没有那样显示
df:
array([[17.21 , 17.21 , 17.23 , 17.16 ],
[17.21 , 17.19 , 17.25 , 17.19 ],
[17.185, 17.21 , 17.23 , 17.18 ],
...,
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ]])
df_norm
array([[17.21 , 17.21 , 17.23 , 17.16 ],
[17.21 , 17.19 , 17.25 , 17.19 ],
[17.185, 17.21 , 17.23 , 17.18 ],
...,
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ]])
df == df_norm
array([[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True],
...,
[ True, True, False, True],
[ True, True, False, True],
[ True, True, False, True]])
这个问题很可能是由浮点数的性质引起的,造成的影响如下:
In [17]: 0.1 + 0.2 == 0.3
Out[17]: False
In [18]: 0.1 + 0.2 - 0.3
Out[18]: 5.551115123125783e-17
尝试使用 np.allclose()
:
比较数组
np.allclose(df_norm, df.values)
对 Sci-kit 有点陌生,但注意到一些行为对于 minmax 归一化和反转似乎有点奇怪(但也许这是预期的行为??)。我想在反转时我会重新生成原始数据点 - 但是反转生成的数字是 "close" 但不完全相等......这是应该发生的,还是我搞砸了?
#previously imported data is in a 3 column df
scaler = sklearn.preprocessing.MinMaxScaler()
df_norm = df.copy()
(df_norm == df).all()
#RETURNS TRUE
df_norm = scaler.fit_transform(df_norm)
df_norm = scaler.inverse_transform(df_norm)
(df_norm == df.values).all()
#RETURNS FALSE
所以我有点疑惑为什么我有 2 个相同的数据帧,但在缩放和反转之后数据集不再相等?许多数字是相等的,但也有相当一部分不是。奇怪的是,如下所示,有些甚至看起来相同,但在使用 df_norm == df
进行测试时却没有那样显示df:
array([[17.21 , 17.21 , 17.23 , 17.16 ],
[17.21 , 17.19 , 17.25 , 17.19 ],
[17.185, 17.21 , 17.23 , 17.18 ],
...,
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ]])
df_norm
array([[17.21 , 17.21 , 17.23 , 17.16 ],
[17.21 , 17.19 , 17.25 , 17.19 ],
[17.185, 17.21 , 17.23 , 17.18 ],
...,
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ],
[12.78 , 12.78 , 12.78 , 12.78 ]])
df == df_norm
array([[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True],
...,
[ True, True, False, True],
[ True, True, False, True],
[ True, True, False, True]])
这个问题很可能是由浮点数的性质引起的,造成的影响如下:
In [17]: 0.1 + 0.2 == 0.3
Out[17]: False
In [18]: 0.1 + 0.2 - 0.3
Out[18]: 5.551115123125783e-17
尝试使用 np.allclose()
:
np.allclose(df_norm, df.values)