Unsupervised Learning

1.1.1 Finding closest centroids

1
2
3
4
5
v = [];
for j = 1:K
v = [v, sum((X - centroids(j,:)).^2, 2)];
end
[v, idx] = min(v, [], 2);

The symbol means Norm not Absolute value the first time as I thought. 😂

1.1.2 Computing centroid means

$$\mu k := \dfrac{1}{\left | C_k \right |}\sum{i\in C_k}^{ }x^{(i)}$$

1
2
3
4
5
6
for i = 1:K
j = (idx == i);
v = X(j, :);
n = size(v, 1);
centroids(i, :) = sum(v)/n;
end

2.2 Implementing PCA

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

2.3.1 Projecting the data onto the principal components

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

2.3.2 Reconstructing an approximation of the data

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

Translated by gpt-3.5-turbo