【问题标题】:can some one help to fit the array in kmeans clustering有人可以帮助将数组拟合到 kmeans 聚类中吗
【发布时间】:2020-04-22 13:55:43
【问题描述】:

当我尝试将其放入 kmeans 聚类中时,它会抛出错误“ValueError: setting an array element with a sequence。”

from sklearn.cluster import KMeans    
kmeans = KMeans(n_clusters=5)
kmeans.fit(df)

数组说明。 名称:向量,长度:179,数据类型:对象

0 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

1 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

10 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

100 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

101 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

【问题讨论】:

    标签: python-3.x pandas numpy machine-learning k-means


    【解决方案1】:

    您的专栏中有一个列表。在将其传递给 KMeans 之前,需要将其打开为多个列。

    df = pd.read_json('/Users/roshansk/Downloads/NewsArticles.json')
    
    #Extracting the vectors into columns
    vectors = df.Vector.apply(pd.Seriesies)
    
    from sklearn.cluster import KMeans    
    kmeans = KMeans(n_clusters=5)
    kmeans.fit(vectors)
    
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多