2017-05-24 3 views
0

두 가지 방법으로, 왜 첫 번째 방법은 (MU와 라인에서 시작 = 평균 (X) 작동하지? 무슨 차이?선형 회귀

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. 
    %  

    %mu=mean(X) 
    %X_norm=X-mu; 
    %sigma=std(X_norm) 
    %X_norm(1)=X_norm(1)/sigma(1) 
    %X_norm(2)=X_norm(2)/sigma(2) 



    % Calculates mean and std dev for each feature 
    for i=1:size(mu,2) 
     mu(1,i) = mean(X(:,i)); 
     sigma(1,i) = std(X(:,i)); 
     X_norm(:,i) = (X(:,i)-mu(1,i))/sigma(1,i); 
    end 







    % ============================================================ 

    end 

답변

1

이유가 있기 때문이다 내가 그것을했다 mean(X)은 X의 열, dimension [1xC]의 평균을 갖는 벡터를 제공하고 X는 차원 [RxC]입니다. 한 개의 oneliner에서 이것을 해결하는 방법은

입니다.
X = (X-repmat(mean(X,1),size(X,1),1))./repmat(std(X,0,1),size(X,1),1) 
+0

'X_norm = (X_norm-mu)./sigma;'이것도 가능합니까? – Tyger