K-Means算法相关:
findClosestCentroids:
function idx = findClosestCentroids(X, centroids)
K = size(centroids, 1);
idx = zeros(size(X,1), 1);
% ====================== YOUR CODE HERE ======================
m = size(X, 1);
for i = 1:m
distance_vec = zeros(K, 1);
for k = 1:K
distance_vec(k) = sum((X(i, :) - centroids(k, :)) .^ 2);
endfor
[min_dist, min_idx] = min(distance_vec);
idx(i) = min_idx;
endfor
% =============================================================
end
computeCentroids:
function centroids = computeCentroids(X, idx, K)
[m n] = size(X);
centroids = zeros(K, n);
% ====================== YOUR CODE HERE ======================
total_vec = zeros(K, n);
count_vec = zeros(K, 1);
for i = 1:m
k = idx(i);
total_vec(k, :) += X(i, :);
count_vec(k) += 1;
endfor
for k = 1:K
centroids(k, :) = total_vec(k, :) / count_vec(k);
endfor
% =============================================================
end
PCA主成分析法相关:
pca:
function [U, S] = pca(X)
[m, n] = size(X);
U = zeros(n);
S = zeros(n);
% ====================== YOUR CODE HERE ======================
Sigma = (1 / m) * (X' * X);
[U, S, V] = svd(Sigma);
% =========================================================================
end
projectData:
function Z = projectData(X, U, K)
Z = zeros(size(X, 1), K);
% ====================== YOUR CODE HERE ======================
U_reduce = U(:, 1:K);
Z = X * U_reduce;
% =============================================================
end
recoverData:
function X_rec = recoverData(Z, U, K)
X_rec = zeros(size(Z, 1), size(U, 1));
% ====================== YOUR CODE HERE ======================
X_rec = Z * U(:, 1:K)';
% =============================================================
end
网友评论