我正在尝试对多维特征进行KMeans Clusterin。我得到值错误:使用序列设置数组元素。
这是我已经尝试过的一个例子:
import pandas as pd
from sklearn.cluster import KMeans
test = pd.DataFrame(np.random.randint(low=0, high=10, size=(30, 4)), columns=['a', 'b', 'c', 'd'])
test["combined1"] = test.loc(axis=1)["a","b"].values.tolist()
test["combined2"] = test.loc(axis=1)["c","d"].values.tolist()
test.drop(['a', 'b', 'c', 'd'],axis=1, inplace=True)
test.head()
kmeans = KMeans(n_clusters=3, random_state=0)
kmeans.fit(test)
KMean 拟合失败
/usr/local/lib/python3.5/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
490
491 """
--> 492 return array(a, dtype, copy=False, order=order)
493
494
ValueError: setting an array element with a sequence.
因此,您将序列传递到 KMeans(如 [8, 1]
),这就是它不起作用的原因。请在这里查看:
https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans.fit
fit()
方法允许您使用:
X : 类数组或稀疏矩阵, shape=(n_samples, n_features)