Normalizing data to [-1, 1] range

84 visualizaciones (últimos 30 días)
Tom Gerard
Tom Gerard el 26 de Abr. de 2016
Comentada: Abhijit Bhattacharjee el 19 de Mayo de 2022
Hello
I have a training dataset which is of size NxD and a test dataset which is of size AxD. The rows are the data points and the columns are the features.
Now I would like to transform each feature (column) to be in the range [-1, 1]. Moreover, the scaling of the features in the test set should be done with the parameters estimated on the training set. For example, if I do the standardization by subtracting the mean and dividing the standard deviation, I would calculate the mean and standard deviation on the training set and use them to standardize the test set. The same I want to do now for scaling to the range [-1, 1].
How can this be done?

Respuesta aceptada

Image Analyst
Image Analyst el 27 de Abr. de 2016
Why do you think you should divide by the standard deviation????? Just scale to 0-1 like this
range = max(m(:)) - min(m(:));
m01 = (m - min(m(:))) / range;
Then to get to the range of -1 to +1, multiply by 2 and subtract 1:
mOut = 2 * m01 - 1;
If you have the Image Processing Toolbox, you can do it all in just one single line of code because the mat2gray() function does the normalization to the range 0-1 without you having to explicitly find the max and min.
mOut = 2 * mat2gray(m) - 1;
  2 comentarios
Pritha Pande
Pritha Pande el 1 de Jun. de 2018
Can you please explain why you multiplied by 2 and subtracted by 1 to get -1 to +1 range. I want my data to range between < -1.5 to > 1.5. What should be my next step after normalising it between 0 to 1 range.
Walter Roberson
Walter Roberson el 1 de Jun. de 2018
Simple algebra.
If x is in the range 0 to 1, then 2 * x is in the range 0 to 2, and subtracting one from that gives you a range of -1 to +1.
If you want to transform 0 to 1 into -1.5 to +1.5, then use (x*3)-1.5

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 1 de Jun. de 2018
If you're using release R2018a or later, use the normalize function.
rng default;
x = randn(10, 1);
y = normalize(x, 'range', [-1 1]);
Z = [x y]
When you display x and y side-by-side in Z, you can see that the smallest element in x corresponds to the value -1 in y and the largest element in x corresponds to 1 in y.
  3 comentarios
Steven Lord
Steven Lord el 2 de Oct. de 2020
Specify the dim input argument to specify the dimension over which to operate.
>> A = magic(5);
>> dim = 2;
>> B = normalize(A, dim, 'range', [-1 1])
Abhijit Bhattacharjee
Abhijit Bhattacharjee el 19 de Mayo de 2022
In the section part of the question in the OP, it looks like they also want to transfer the centering and scaling values from one dataset (the training set) to the other (testing set). This can be accomplished with one of the extended syntaxes of the normalize function as follows:
[trainingSetNormalized, C, S] = normalize(trainingSet, dim, 'range', [-1 1]);
Now the C and S arrays each contain the centering and scaling values, respectively, which can then be used to "unnormalize" the test set with the same parameters.

Iniciar sesión para comentar.

Categorías

Más información sobre Hypothesis Tests en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by