通过实施K均值的形状问题

时间:2019-02-27 18:52:47

标签: python pandas numpy k-means

当前我正在从事与K-Means相关的一个项目,但是我的代码中出现以下错误:

TypeError: cannot unpack non-iterable int object

我进行了搜索,但找不到任何解决方案。如果有人知道解决方法,请提供帮助。

我的代码是:

import pandas as pd
import numpy as np

pathtofile = "k-means.csv"
data = pd.read_csv(pathtofile, delimiter=",")

X = data.iloc[:,0]
print(X.head)
1      6
2      2
3      3
4      1
print(type(X))
<class 'pandas.core.series.Series'>

print(X.shape)
(299,)

K = 3
init_centro = np.array([[5,2],[3,1],[4,7]])

def ClosestCentro(X, centro):
    K = centro.shape[0]
    idx = np.zeros((X.shape[0],1), dtype=np.int8)

    for i in range(X.shape[0]):
    dist = np.linalg.norm(X[i] - centro, axis=1)
    min_dist = np.argmin(dist)
    idx[i] = min_dist

    return idx

idx = ClosestCentro(X, init_centro)
print("Closest centroids for the first 300 examples:\n")
print(idx[:300])
[[0]
[2]
[0]
[0]


def ComputeCentro(X, idx, K):
    m, n = X.shape
    centro = np.zeros((K,n))
    for k in range(K):
        centro[k,:] = np.mean(X[idx.ravel()==k,:], axis=0)
    return centro

centro = ComputeCentro(X, idx,K)
print(centro)

错误是m, n = X.shape引起的,我看了其他问题,但看不到.shape有任何解决方案。

0 个答案:

没有答案