【问题标题】:How to cluster a dataframe by column of long vectors如何按长向量列对数据帧进行聚类
【发布时间】: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


    【解决方案1】:

    我假设train 是一个数据框?你不应该使用价值观吗?

    ms.fit(train['vector'].values)
    

    【讨论】:

    • 是的 train 是我的训练数据框。使用值没有任何区别。仍然会出现错误
    • 你把dtype改成float了吗?
    • 我编辑了这个问题。你会看吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2019-10-22
    • 2017-07-19
    • 2018-02-20
    • 2013-02-10
    • 2017-08-20
    相关资源
    最近更新 更多