PySpark,如何简单地统计 Kmeans 模型中每个簇的数量?

PySpark, How to simply count the number of each cluster in Kmeans model?

我训练了一个 Kmeans 模型:

kmeans = KMeans(k=20, seed=1)
df.show()
kmeans_model = kmeans.fit(df)

我只想简单的统计每个簇有多少个元素,但是找不到简单的实现方法

检查了pyspark文档。答案如下:

summary = kmeans_model.summary
print(summary.clusterSizes)

参考:

http://spark.apache.org/docs/2.2.0/api/python/pyspark.ml.html#pyspark.ml.clustering.KMeans