Clustering
Principal Component Analysis
Q2选2。。。手滑
Quiz
findClosestCentroids.m
temp = zeros(K,1);
for i = 1:size(X,1)
for j = 1:K
temp(j) = sum((X(i,:) - centroids(j,:)).^2);
end
[value,idx(i)] = min(temp,[],1);
end
computeCentroids.m
for i = 1:K
centroids(i,:) = (X' * (idx == i)) / sum(idx == i); %X' * (idx == i) To add x in same center
end
kMeansInitCentroids.m
% Randomly reorder the indices of examples
randidx = randperm(size(X, 1));
% Take the first K examples as centroids
centroids = X(randidx(1:K), :);
pca.m
sigma = X' * X / m; % compute the covariance matrix
[U,S,V] = svd(sigma); % SVD
projectData.m
Z = X * U(:,1:K);
recoverData.m
X_rec = Z * U(:,1:K)';