pca.m
sigma = (X'*X)./m; %计算Sigma
[U,S,V] = svd(sigma); %进行奇异值分解
projectData.m
Ureduce = U(:, 1:K); %提取前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 %遍历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)); %生成一个从1到k的整数的随机排列。
centroids = X(randidx(1:K), :); %随机选取样本点进行初始化