我有一个数据框,其中有一个名为“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 列而不是一个向量列),因为它会变得太多。我不知道该怎么做。 那么我如何通过向量对数据框进行聚类呢?
我假设
train
是一个数据框?难道不应该使用价值观吗?
ms.fit(train['vector'].values)