MATLAB - Normalize Data
MATLAB - A Practical Guide to Data Normalization
Section titled “MATLAB - A Practical Guide to Data Normalization”Data normalization (or feature scaling) is a critical preprocessing step in data analysis and machine learning. It involves transforming the features of a dataset to a common scale without distorting differences in the ranges of values. MATLAB’s normalize function provides a powerful and flexible way to perform this task.
Why Normalize Data?
Section titled “Why Normalize Data?”Normalizing data is essential for several reasons:
- Algorithm Performance: Many machine learning algorithms (like Support Vector Machines, k-Nearest Neighbors, and gradient-descent based algorithms like linear regression and neural networks) perform poorly or converge slowly if features are on vastly different scales. For example, a feature ranging from 0 to 100,000 will dominate a feature ranging from 0 to 1.
- Feature Comparability: Normalization puts all features on a level playing field, allowing for meaningful comparisons and combinations.
- Improved Visualization: Scaling data can make it easier to visualize relationships between variables on a single plot.
Common Normalization Methods
Section titled “Common Normalization Methods”Let’s explore the most common methods using the normalize function.
% Let's create a sample dataset with two features on different scales% Feature 1: 'Age' (e.g., 20-65)% Feature 2: 'Salary' (e.g., 50k-150k)data = [25, 60000; 45, 90000; 60, 125000; 22, 52000; 38, 75000];1. Z-score Standardization
Section titled “1. Z-score Standardization”This method rescales the data to have a mean (μ) of 0 and a standard deviation (σ) of 1. The formula is z = (x - μ) / σ.
- Use Case: This is the most common method and works well when your data follows a Gaussian (normal) distribution. It is less affected by outliers than Min-Max scaling.
- MATLAB Syntax:
normalize(data, 'zscore')
% Standardize the data using Z-scorenormalized_zscore = normalize(data, 'zscore');
disp('Original Data:');disp(data);disp('Z-score Normalized Data:');disp(normalized_zscore);
% Verify the mean and std of the new data (should be ~0 and ~1)fprintf('Mean of normalized data: [%.2f, %.2f]\n', mean(normalized_zscore));fprintf('Std of normalized data: [%.2f, %.2f]\n', std(normalized_zscore));2. Min-Max Scaling
Section titled “2. Min-Max Scaling”This method rescales the data to a fixed range, typically [0, 1]. The formula is x_scaled = (x - min(x)) / (max(x) - min(x)).
- Use Case: Useful for algorithms that expect inputs in a bounded interval, such as some neural networks. It is also common in image processing where pixel values are scaled to [0, 1]. Be aware that it is sensitive to outliers.
- MATLAB Syntax:
normalize(data, 'range')
% Scale the data to the range [0, 1]normalized_range = normalize(data, 'range');
disp('Min-Max Scaled Data:');disp(normalized_range);You can also specify a custom range, for example [-1, 1]:
% Scale to the range [-1, 1]normalized_custom_range = normalize(data, 'range', [-1, 1]);3. Scaling by Norm
Section titled “3. Scaling by Norm”This method scales each sample (row) so that it has a unit norm (e.g., L2-norm or Euclidean length of 1). This is often used in text analysis or when the direction of the data vector is more important than its magnitude.
- Use Case: Text classification with TF-IDF, clustering algorithms.
- MATLAB Syntax:
normalize(data, 'norm', p)wherepis the norm (e.g., 2 for L2).
% Note: we need to specify the dimension to operate on rows (dim=2)normalized_norm = normalize(data, 2, 'norm', 2);
disp('L2-Norm Scaled Data (by row):');disp(normalized_norm);
% Verify the L2 norm of each row (should be 1)
disp('L2 Norms of each row:');disp(vecnorm(normalized_norm, 2, 2));Best Practice: Handling Train and Test Sets
Section titled “Best Practice: Handling Train and Test Sets”A common mistake is to normalize the entire dataset before splitting it into training and testing sets. This causes data leakage, where information from the test set (e.g., its min/max values) leaks into the training process, leading to overly optimistic performance estimates.
The correct approach is to calculate the scaling parameters (mean/std, min/max) from the training data only and then apply this same transformation to the test data. The normalize function is not designed for this two-step process, so we do it manually.
Example: Correct Train/Test Normalization
Section titled “Example: Correct Train/Test Normalization”% 1. Split data (for demonstration)trainData = data(1:3, :);testData = data(4:5, :);
% 2. Calculate scaling parameters FROM TRAINING DATA ONLYmu = mean(trainData);sigma = std(trainData);
% 3. Apply the transformation to both train and test setstrainData_norm = (trainData - mu) ./ sigma;testData_norm = (testData - mu) ./ sigma; % Use mu/sigma from train data!
disp('Correctly Normalized Training Data:');disp(trainData_norm);disp('Correctly Normalized Test Data:');disp(testData_norm);