【发布时间】:2021-06-02 00:41:51
【问题描述】:
我有一个数据框,它有一个名为“address”的列和一个名为“vecor”的列,该列有一个长度为 700 的向量。我想按向量列对我的数据框进行聚类,但是当我尝试使用 KMeans 算法时,
from sklearn.cluster import KMeans
x = np.array(train['vector'].values).astype('float64')
ms = KMeans(n_clusters=3,n_init=10,max_iter=300,random_state=42)
ms.fit(x)
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-9-919881bf72d4> in <module>
1 from sklearn.cluster import KMeans
----> 2 x = np.array(train['vector'].values).astype('float64')
3 ms = KMeans(n_clusters=3,n_init=10,max_iter=300,random_state=42)
4 ms.fit(x)
5 cluster_centers = ms.cluster_centers_
ValueError: setting an array element with a sequence.
我不能使用向量作为特征(添加 700 列而不是一个向量列),因为它会变得很多。我不知道该怎么做。 那么如何通过向量对我的数据框进行聚类?如何?
【问题讨论】:
标签: python machine-learning scikit-learn cluster-analysis k-means