Wednesday, May 7, 2014

Feature Normalization Algorithm


 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.  
 %      
 features = size(X,2);  
 % the given values of mu and sigma as initial zeros  
 mu = zeros(1, features);  
 sigma = zeros(1, features);  
 %iterating through all the features, like we have 3 features follows-  
 % 1 2045 3  
 % 1 5678 2  
 for iter = 1 : features  
      %Mean mean(X..........)  
      mu(1, iter) = mean(X(:,iter));  
      X(:,iter) = X(:,iter) .- mu(1, iter);  
      %Now derive the following formula-  
      %Xi = (Xi – mean)/Sigma  
      %Standard Deviation can be made by just calling std(......)  
      sigma(1, iter) = std(X(:,iter));  
      X(:,iter) = X(:,iter) ./ sigma(1, iter);       
 end;  
      %display(X);  
      X_norm = X;       
 % ============================================================  
 end  


No comments: