Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Fast way of MinMax scaling an array

I use the following way to scale an n-dimensional array between 0 and 1:

x_scaled = (x-np.amin(x))/(np.amax(x)-np.amin(x))

But it's very slow for large datasets. I have thousands of relatively large arrays which I need to process. Is there a faster method to this in python?

Edit: My arrays are in shape (24,24,24,9). For MinMax scaler in scikit, the input array has to have a certain shape which mine doesn't so I can't use it. In the documentation it says:

Parameters: 
X : array-like, shape [n_samples, n_features]
like image 581
Wise Avatar asked Nov 04 '25 22:11

Wise


1 Answers

It's risky to use ptp, i.e. max - min, as it can in theory be 0, leading to an exception. It's safer to use minmax_scale as it doesn't have this issue. First, pip install scikit-learn.

from sklearn.preprocessing import minmax_scale

minmax_scale(array)

If using an sklearn Pipeline, use MinMaxScaler instead.

like image 135
Asclepius Avatar answered Nov 06 '25 13:11

Asclepius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!