Created
May 26, 2012 23:24
-
-
Save anuvrat/2795602 to your computer and use it in GitHub Desktop.
Normalizes the features in X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [X_norm, mu, sigma] = featureNormalize(X) | |
%FEATURENORMALIZE Normalizes the features in X | |
% FEATURENORMALIZE(X) returns a normalized version of X where | |
% the mean value of each feature is 0 and the standard deviation | |
% is 1. This is often a good preprocessing step to do when | |
% working with learning algorithms. | |
% Initialize some useful values | |
X_norm = X; | |
mu = zeros(1, size(X, 2)); | |
sigma = zeros(1, size(X, 2)); | |
% For each feature dimension, compute the mean | |
% of the feature and subtract it from the dataset, | |
% storing the mean value in mu. Next, compute the | |
% standard deviation of each feature and divide | |
% each feature by it's standard deviation, storing | |
% the standard deviation in sigma. | |
% | |
% Note that X is a matrix where each column is a | |
% feature and each row is an example. We need | |
% to perform the normalization separately for | |
% each feature. | |
mu = sum(X) / length(X); | |
sigma = std(X); | |
for i=1:length(X), | |
X_norm(i,:) = (X(i,:) - mu) ./ sigma; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just in case someone find these snippet of code in the future, both mu and the for loop should use rows(X) instead of length(X).