【问题标题】:Weighted k-means in pythonpython中的加权k均值
【发布时间】:2020-04-17 12:04:09
【问题描述】:

在阅读了这篇关于 k-means 聚类中重复值的帖子后,我意识到我不能简单地使用唯一点进行聚类。

https://stats.stackexchange.com/questions/152808/do-i-need-to-remove-duplicate-objects-for-cluster-analysis-of-objects

我有超过 10000000 点,虽然只有 8000 个独特点。因此,我最初认为为了加快速度,我只会使用独特的点。似乎这是一个坏主意。

为了减少计算时间,这篇文章建议为每个点添加权重。这在python中如何实现?

【问题讨论】:

  • 请添加您尝试过的内容以及在 python 中实现的问题是什么?:

标签: python k-means


【解决方案1】:

使用 Scikit 库中的 K-Means 包,此处对簇数为 11 进行聚类。 数组 Y 包含已作为权重插入的数据,而 X 包含需要聚类的实际点。

from sklearn.cluster import KMeans  #For applying KMeans
##--------------------------------------------------------------------------------------------------------##
#Starting k-means clustering


kmeans = KMeans(n_clusters=11, n_init=10, random_state=0, max_iter=1000)

#Running k-means clustering and enter the ‘X’ array as the input coordinates and ‘Y’ 
array as sample weights
wt_kmeansclus = kmeans.fit(X,sample_weight = Y)
predicted_kmeans = kmeans.predict(X, sample_weight = Y)

#Storing results obtained together with respective city-state labels
kmeans_results = 
pd.DataFrame({"label":data_label,"kmeans_cluster":predicted_kmeans+1})


#Printing count of points alloted to each cluster and then the cluster centers
print(kmeans_results.kmeans_cluster.value_counts())

【讨论】:

    【解决方案2】:

    我认为这篇文章建议使用加权平均值。

    您可以从旧数据集创建一个新数据集,新数据集将为每个点添加一个额外属性,即频率(即权重)。

    每次计算每个聚类的新质心时,取该聚类所有点的加权平均值(而不是计算所有点的简单平均值)。

    PS:操纵数据集是危险的。如果计算成本是主要因素,我会并行化代码。

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 1970-01-01
      • 2015-07-15
      • 2017-07-07
      • 2018-09-01
      • 2019-07-17
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多