【发布时间】:2018-10-11 22:01:38
【问题描述】:
我尝试为 MNIST 数据集实现 k-means 算法。但由于结果远非好,可能有一个(或几个)我目前看不到的错误。代码非常简单。这是我到目前为止所做的:
import numpy as np
# Load images
I = np.load("mnist_test_images.npy").astype(float) # (10000,784)
L = np.load("mnist_test_labels.npy").astype(int) # (10000,1)
# Scale
I = 2.0*(I/255.0-0.5)
images = len(I)
# Random initialization of centers for k=10 clusters
M = np.random.randn(10,28*28)
guess = np.zeros((len(I),1))
step = 0
while (True):
# Compute distance of every image i to the center of every cluster k
# image i belongs to cluster with smallest distance
for i in range(images):
d = np.sum((M-I[i])**2,axis=1)
guess[i] = np.argmin(d)
# Update the centers for all clusters
# New center is the mean of all images i which belong to cluster k
for k in range(10):
idx, _ = np.where(guess == k)
if len(idx) > 0:
M[k] = np.mean(I[idx],axis=0)
# Test how good the algorithm works
# Very similar to first step
if (step % 10 == 0):
fitness = 0
for i in range(images):
dist = np.sum((M-I[i])**2,axis=1)
if L[i] == np.argmin(dist):
fitness += 1
print("%d" % fitness, flush=True)
step += 1
代码看起来很简单。但可能某处存在错误。当我测试它时,准确率从大约 10-20% 下降到 5-10% 或几乎立即收敛,不超过 30%。我无法识别任何学习。集群中心的随机初始化会导致这种行为吗?
谢谢!
【问题讨论】:
标签: python cluster-analysis k-means