sklearn.cluster.KMeans 报错 ValueError: n_samples=1 should be >= n_clusters=10

from sklearn.cluster import KMeans
k_center_num = 10
yc = [1,2,34,6,8,9,0,5,43,9,3123,5432,6823,0,312]
kcl = KMeans(n_clusters = k_center_num)
cl_obj = kcl.fit(yc)

# Error
ValueError: n_samples=1 should be >= n_clusters=10

是因为 一个 list 会被认为是一个 sample。所以,每一个元素都被转化成一个 list。

from sklearn.cluster import KMeans
k_center_num = 10
yc = [1,2,34,6,8,9,0,5,43,9,3123,5432,6823,0,312]
yc = [[i] for i in yc] # 把每个样本变成一个单独的‘向量’就可以了。
kcl = KMeans(n_clusters = k_center_num)
cl_obj = kcl.fit(yc)

你可能感兴趣的:(Python,机器学习)