Skip to content

Instantly share code, notes, and snippets.

@denzilc
Created November 2, 2011 12:08
Show Gist options
  • Save denzilc/1333474 to your computer and use it in GitHub Desktop.
Save denzilc/1333474 to your computer and use it in GitHub Desktop.
Feature Normalization
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.
% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
% ====================== YOUR CODE HERE ======================
% Instructions: First, 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. You need
% to perform the normalization separately for
% each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
for col = 1:columns(X)
mu(1,col) = mean(X(:,col));
sigma(1,col) = std(X(:,col));
end
for row = 1:rows(X)
for col = 1:columns(X)
X_norm(row,col) = (X(row,col)-mu(1,col))/sigma(1,col);
end
end
% ============================================================
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment