如何在所有列上使用 MinMaxScaler?

Chr*_*ang 4 python numpy scikit-learn

现在,我的数据位于 2 x 2 numpy 数组中。如果我要在数组上使用 MinMaxScaler fit_transform,它将逐列标准化,而我希望将整个 np 数组一起标准化。有办法做到这一点吗?

小智 5

为什么不直接使用原始的 MinMaxScaler API,如下所示:

  1. 将 X numpy 数组重塑为一列数组,
  2. 规模,
  3. 将结果重塑回 X 数组的形状

    import numpy as np
    
    X = np.array([[-1, 2], [-0.5, 6]])
    scaler = MinMaxScaler()
    X_one_column = X.reshape([-1,1])
    result_one_column = scaler.fit_transform(X_one_column)
    result = result_one_column.reshape(X.shape)
    print(result)
    
    Run Code Online (Sandbox Code Playgroud)

输出

[[ 0.          0.42857143]
 [ 0.07142857  1.        ]]
Run Code Online (Sandbox Code Playgroud)