/exercises/mlclass-ex1/featureNormalize.m

http://github.com/jneira/machine-learning-course · Objective C · 39 lines · 35 code · 4 blank · 0 comment · 3 complexity · aaf20bd98e6320e582f1652bdcb2e8f8 MD5 · raw file

  1. function [X_norm, mu, sigma] = featureNormalize(X)
  2. %FEATURENORMALIZE Normalizes the features in X
  3. % FEATURENORMALIZE(X) returns a normalized version of X where
  4. % the mean value of each feature is 0 and the standard deviation
  5. % is 1. This is often a good preprocessing step to do when
  6. % working with learning algorithms.
  7. % You need to set these values correctly
  8. X_norm = X;
  9. mu = zeros(1, size(X, 2));
  10. sigma = zeros(1, size(X, 2));
  11. % by @dnene
  12. % x_size = length(X);
  13. % X_norm = (X_norm - repmat(mu,x_size,1)) ./ repmat(sigma,x_size,1);
  14. % ====================== YOUR CODE HERE ======================
  15. % Instructions: First, for each feature dimension, compute the mean
  16. % of the feature and subtract it from the dataset,
  17. % storing the mean value in mu. Next, compute the
  18. % standard deviation of each feature and divide
  19. % each feature by it's standard deviation, storing
  20. % the standard deviation in sigma.
  21. %
  22. % Note that X is a matrix where each column is a
  23. % feature and each row is an example. You need
  24. % to perform the normalization separately for
  25. % each feature.
  26. %
  27. % Hint: You might find the 'mean' and 'std' functions useful.
  28. %
  29. mu=mean(X);
  30. sigma=std(X);
  31. for i=1:size(X,2)
  32. X_norm(:,i)=(X(:,i).-mu(i))./sigma(i);
  33. end
  34. % ============================================================
  35. end