wk7 K-means Clustering and Principal Component Analysis
最近正在学习MOOC上的经典课程:Machine learning (by Andrew Ng), 具体课程链接:MACHINE LEARNING
根据进度将作业的关键代码部分贴上,仅供交流与讨论。
- findClosestCentroids
len = zeros(K, 1);
for i = 1:size(X,1)
for j = 1:K
len(j) = norm(X(i, :) - centroids(j, :))^2;
end
[~,idx(i)] = min(len); %% return the row of the min(len), that is return the exact cluster this training example belongs to
end
- computeCentroids
for k = 1:K
ind = find(idx == k); %% return the row number, that is the 'ith' training number
centroids(k, :) = mean(X(ind, :));
end
- pca
sigma=(X'*X)/m;
[U,S,~]=svd(sigma);
- projectData
Ureduced=U(:,1:K); %%%n*k
Z=X*Ureduced; %%%X:m*n
- recoverData
X_rec = Z * U(:, 1:K)';
——转载请注明出处