现在我有一个 2 x 2 numpy 数组。通过使用 RobustScaler,它一次标准化每一列,而我希望一次标准化所有内容。有办法做到这一点吗?
最佳答案
从文档中 RobustScaler :
removes the median and scales the data according to the quantile range
因此您需要计算整个数组的中位数和分位数范围,为此您可以使用 np.median和 np.percentile函数,这就是 sklearn 在幕后所做的事情。代码:
import numpy as np
from sklearn.preprocessing import robust_scale
data = np.array([[3, 6],
[9, 12]], dtype=np.float64)
result = robust_scale(data, axis=0)
print(result)
reshape = data.reshape((1, 4))
result = robust_scale(reshape, axis=1)
me = np.median(data.flat) # 7.5
percentiles = np.percentile(data, (25.0, 75.0)) # 5.25 9.75
data -= me
data /= (percentiles[1] - percentiles[0])
print(data)
输出
[[-1. -1.]
[ 1. 1.]]
[[-1. -0.33333333]
[ 0.33333333 1. ]]
在示例中,我使用了 (25.0, 75.0)
因为这是分位数范围的默认值,也是函数 robust_scale 的默认值。相当于 RobustScaler 的功能(文档中的“另请参阅”部分)。
关于python - 如何在所有列上使用 RobustScaler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52500971/