Normalizing data to [-1, 1] range
Show older comments
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?
Accepted Answer
More Answers (1)
Steven Lord
on 1 Jun 2018
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 Comments
Yi Wen Yvonne Lim
on 2 Oct 2020
Hi, what should I do if I want to normalise row by row instead of normalising the entire set of x values?
Steven Lord
on 2 Oct 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
on 19 May 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.
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!