Stanford 机器学习 Week8 作业:K-means Clustering and Principal Component Analysis

FindClosestCentroids

for i = 1:size(X,1)
    dis = sum((centroids - X(i,:)) .^ 2, 2);
    [t, idx(i)] = min(dis);
end

ComputeCentroids

for i = 1:K
    id = find(idx == i);
    tot = X(id,:);
    centroids(i,:) = mean(tot);
end

RandomInitialization


id = randperm(size(X,1));
id = id(1:K);
centroids = X(id);

PCA

sigma = 1/m * X' * X; [U,S,V] = svd(sigma);

ProjectionData(PCA)

Z = X * U(:,1:K);

RecoverData(PCA)

X_rec = Z * U(:,1:K)' 

你可能感兴趣的:(机器学习,octave)