MOOC Machine Learning 作业交流帖7

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)';

——转载请注明出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值