gpt4 book ai didi

python - Scikit Scaler 和 Inversion 不会产生相同的数字?

转载 作者:行者123 更新时间:2023-12-01 01:59:20 25 4
gpt4 key购买 nike

对 Sci-kit 来说有点新,但注意到最小最大归一化和反转的一些行为似乎有点奇怪(但也许这是预期的行为??)。我以为在反转时我会重新生成原始数据点 - 但相反,反转生成的数字“接近”但不完全相等......这是应该发生的,还是我搞砸了?

#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]])

最佳答案

这个问题很可能是由 float 的性质引起的,导致如下影响:

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)

关于python - Scikit Scaler 和 Inversion 不会产生相同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49840087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com