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?
2 Answers
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.
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.
feature_range
parameter inMinMaxScaler
to transform data to any range you want. $\endgroup$