Python Sci套件定标器和反转不产生相同的数字?

Python Sci套件定标器和反转不产生相同的数字?,python,scikit-learn,inversion,Python,Scikit Learn,Inversion,Sci工具包有点新,但注意到一些行为,在minmax规范化和反转中似乎有点奇怪(但这可能是预期的行为??)。我想当反转时,我会重新生成原始数据点-但是相反,反转生成的数字“接近”,但不完全相等。。。这是应该发生的,还是我把事情搞砸了 #previously imported data is in a 3 column df scaler = sklearn.preprocessing.MinMaxScaler() df_norm = df.copy() (df_norm == df).all()

Sci工具包有点新,但注意到一些行为,在minmax规范化和反转中似乎有点奇怪(但这可能是预期的行为??)。我想当反转时,我会重新生成原始数据点-但是相反,反转生成的数字“接近”,但不完全相等。。。这是应该发生的,还是我把事情搞砸了

#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
所以我只是有点困惑,为什么我有两个相同的数据帧,但在缩放和反转数据集后,它们不再相等?许多数字是相等的,但也有不少是不相等的。奇怪的是,如下图所示,有些甚至看起来完全相同,但在使用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
尝试使用以下方法比较阵列:


以下公式的输出是什么:
df_norm.sum()-df.values.sum()
?该公式的输出是0.0…np.allclose()=true。谢谢,这很奇怪/出乎意料,但是在读了一些关于浮点的书之后,这就很有道理了@TheRhyno,很高兴我能帮上忙:)
np.allclose(df_norm, df.values)