Skip to content Skip to sidebar Skip to footer

How To Scale Each Column Of A Matrix

This is how I scale a single vector: vector = np.array([-4, -3, -2, -1, 0]) # pass the vector, current range of values, the desired range, and it returns the scaled vector scaledV

Solution 1:

If you want to do it by hand and understand what's going on:

First substract columnwise mins to make each columns have min 0.

Then divide by columnwise amplitude (max - min) to make each column have max 1.

Now each column is between 0 and 1. If you want it to be between -1 and 1, multiply by 2, and substract 1:

In [3]: mins = np.min(matrix, axis=0)

In [4]: maxs = np.max(matrix, axis=0)

In [5]: (matrix - mins[None, :]) / (maxs[None, :] - mins[None, :])
Out[5]: 
array([[ 0.        ,  0.        ,  0.        ,  0.17857143,  0.        ],
       [ 0.25      ,  0.25      ,  0.02      ,  0.        ,  0.        ],
       [ 0.5       ,  0.5       ,  0.16      ,  0.16666667,  0.        ],
       [ 0.75      ,  0.75      ,  0.22      ,  0.32142857,  0.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ]])

In [6]: 2 * _ - 1
Out[6]: 
array([[-1.        , -1.        , -1.        , -0.64285714, -1.        ],
       [-0.5       , -0.5       , -0.96      , -1.        , -1.        ],
       [ 0.        ,  0.        , -0.68      , -0.66666667, -1.        ],
       [ 0.5       ,  0.5       , -0.56      , -0.35714286, -1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ]])

I use [None, :] for numpy to understand that I'm talking about "row vectors", not column ones.

Otherwise, use the wonderful sklearn package, whose preprocessing module has lots of useful transformers:

In [13]: from sklearn.preprocessing import MinMaxScaler

In [14]: scaler = MinMaxScaler(feature_range=(-1, 1))

In [15]: scaler.fit(matrix)
Out[15]: MinMaxScaler(copy=True, feature_range=(-1, 1))

In [16]: scaler.transform(matrix)
Out[16]: 
array([[-1.        , -1.        , -1.        , -0.64285714, -1.        ],
       [-0.5       , -0.5       , -0.96      , -1.        , -1.        ],
       [ 0.        ,  0.        , -0.68      , -0.66666667, -1.        ],
       [ 0.5       ,  0.5       , -0.56      , -0.35714286, -1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ,  1.        ]])

Post a Comment for "How To Scale Each Column Of A Matrix"