# Purposely rescale and add negative sign to mimic Sklearn's PCA return -(X_PC - X_PC.mean(axis=0))/X_PC.std(axis=0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defmyKMeans (X: np.ndarray, k: int, iterations=100, tol=0.001): N, d = X.shape mu = X[np.random.choice(range(N), size=k)] c = np.zeros(N)
for step in tqdm(range(iterations)): prev_mu = mu.copy()
for i in range(N): c[i] = np.argmin(np.linalg.norm(X[i] - mu, axis=1)**2) for j in range(k): mu[j] = np.mean(X[np.arange(N)[c==j]], axis=0)
if (np.sum(np.linalg.norm(prev_mu - mu,axis=1)) < tol): break distortion = np.sum([np.sum(np.linalg.norm(X[np.arange(N)[c==i]] - mu[i], axis=1)**2) for i in range(k)]) print(f"distortion: {distortion}")