我正在对一些统计数据进行K均值分析。我的矩阵大小是[192x31634]。 K-Means的表现很好,创造了7个质心的数量,这是我想要的。所以我的结果是[192x7]
作为一些自检,我将在K-Means运行中获得的索引值存储到字典中。你知道吗
centroids,idx = runkMeans(X_train, initial_centroids, max_iters)
resultDict.update({'centroid' : centroids})
resultDict.update({'idx' : idx})
然后我用我用来寻找质心的相同数据来测试我的K-均值。奇怪的是,我的结果不同:
dict= pickle.load(open("MyDictionary.p", "rb"))
currentIdx = findClosestCentroids(X_train, dict['centroid'])
print("idx Differs: ",np.count_nonzero(currentIdx != dict['idx']))
输出:
idx Differs: 189
有人能给我解释一下这种区别吗?我把算法的最大迭代次数调到了50次,这似乎太多了。@乔哈利韦尔指出,K-均值是不确定的。findClosestCentroids被runkMeans调用。我不明白,为什么两个idx的结果会不同。谢谢你的建议。你知道吗
这是我的密码:
def findClosestCentroids(X, centroids):
K = centroids.shape[0]
m = X.shape[0]
dist = np.zeros((K,1))
idx = np.zeros((m,1), dtype=int)
#number of columns defines my number of data points
for i in range(m):
#Every column is one data point
x = X[i,:]
#number of rows defines my number of centroids
for j in range(K):
#Every row is one centroid
c = centroids[j,:]
#distance of the two points c and x
dist[j] = np.linalg.norm(c-x)
#if last centroid is processed
if (j == K-1):
#the Result idx is set with the index of the centroid with minimal distance
idx[i] = np.argmin(dist)
return idx
def runkMeans(X, initial_centroids, max_iters):
#Initialize values
m,n = X.shape
K = initial_centroids.shape[0]
centroids = initial_centroids
previous_centroids = centroids
for i in range(max_iters):
print("K_Means iteration:",i)
#For each example in X, assign it to the closest centroid
idx = findClosestCentroids(X, centroids)
#Given the memberships, compute new centroids
centroids = computeCentroids(X, idx, K)
return centroids,idx
编辑:我把我的最大值调到了60,得到了一个
idx Differs: 0
看来这就是问题所在。你知道吗
K-means是一种非确定性算法。通常通过设置随机种子来控制。例如,SciKit Learn的实现为此提供了
random_state
参数:参见https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html上的文档
相关问题 更多 >
编程相关推荐