Coursera机器学习week8 编程作业

pca.m

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

projectData.m

 Ureduce = U(:, 1:K);
 for i = 1:size(X,1)
     x = X(i,:);
     Z(i,:) = x*Ureduce;
 end

recoverData.m

for i = 1:size(Z,1)
    X_rec(i,:) = Z(i,:) * U(:,1:K)';
end

computeCentroids.m

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

findClosestCentroids.m

k_tmp = zeros(K, 1);  

for i = 1:size(idx, 1)
    for j = 1:K
        k_tmp(j) = sum((X(i,:) - centroids(j,:)).^2);
    end
    [value,idx(i)] = min(k_tmp,[],1);   
end

kMeansInitCentroids.m

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

 

posted @ 2018-02-09 23:03  starry_sky  阅读(743)  评论(0编辑  收藏  举报