1
$\begingroup$

I am using scikit-learn MinMaxScaler() to normalize to $[0, 1]$, but I want to normalize to $[-1, 1].$ What function do I have to use to normalize this way?

$\endgroup$
1
  • 5
    $\begingroup$ You can pass feature_range parameter in MinMaxScaler to transform data to any range you want. $\endgroup$
    – Ankit Seth
    Sep 3, 2018 at 4:54

2 Answers 2

2
$\begingroup$

Scaling between 0 and 1 is simply written for an array of values arr = $[x_{1}, x_{2}, ...., x_{n}]$ as

scaled_array = (arr-arr.min())/(arr.max()-arr.min())

But scaling between two values can be inherited from normalisation in Images when values are scaled between 0 to 255 (normally).

You can write scaling as

scaled_values = ((val - min)*(new_max - new_min)/(max - min)) + new_min

where new_min, new_max are the maximum and minimum values you are scaling in between and max and min are the maximum and minimum values in your array.

For example if you want to scale values between -1 and 1 for an array [2, 5, 0], it reduces to

((arr - 0)*(1-(-1))/(5-0)) - 1 = [-0.2, 1, -1]

Check this wiki page.

Edit:

As pointed out rightly in comments by @AnkitSeth, MinMaxScalar do have feature_range argument you can pass on. But I am sure the internal job done by feature_range is the same thing as I had mentioned above.

$\endgroup$
-2
$\begingroup$

Try using StandardScaler() from sklearn.preprocessing.

This is the proper normalization. But we cannot give any range. Only mean is zero, that is for sure.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.