吴恩达机器学习作业7

pca

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

吴恩达机器学习作业7_第1张图片

projectData

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

在这里插入图片描述

recoverData

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

在这里插入图片描述

findClosestCentroids

m = size(X,1);
for i = 1:m
  ind_k = zeros(K,1);
  for j = 1:K
    ind_k(j) = sum((X(i,:) - centroids(j,:)) .^ 2);
  endfor
  [value,index] = min(ind_k);
  idx(i) = index;
endfor

吴恩达机器学习作业7_第2张图片

computeCentroids

for i = 1:K
  centroids(i,:) = ((idx == i)' * X) / sum(idx == i);
endfor

吴恩达机器学习作业7_第3张图片

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