在阅读了这篇关于 k 均值聚类中的重复值的文章后,我意识到我不能简单地使用唯一点进行聚类。
\n\n\n\n我有超过 10000000 点,虽然只有 8000 个独特的点。因此,我最初认为为了加快速度,我\xe2\x80\x99d 仅使用唯一点。看来这是一个坏主意。
\n\n为了减少计算时间,这篇文章建议为每个点添加权重。这在Python中如何实现呢?
\n小智 6
使用 Scikit 库中的 K-Means 包,对簇数执行聚类,此处为 11。\n数组 Y 包含已作为权重插入的数据,其中 X 具有需要聚类的实际点。
\n\nfrom sklearn.cluster import KMeans #For applying KMeans\n##--------------------------------------------------------------------------------------------------------##\n#Starting k-means clustering\n\n\nkmeans = KMeans(n_clusters=11, n_init=10, random_state=0, max_iter=1000)\n\n#Running k-means clustering and enter the \xe2\x80\x98X\xe2\x80\x99 array as the input coordinates and \xe2\x80\x98Y\xe2\x80\x99 \narray as sample weights\nwt_kmeansclus = kmeans.fit(X,sample_weight = Y)\npredicted_kmeans = kmeans.predict(X, sample_weight = Y)\n\n#Storing results obtained together with respective city-state labels\nkmeans_results = \npd.DataFrame({"label":data_label,"kmeans_cluster":predicted_kmeans+1})\n\n\n#Printing count of points alloted to each cluster and then the cluster centers\nprint(kmeans_results.kmeans_cluster.value_counts())\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13314 次 |
| 最近记录: |