我已经在以下位置实现了 Apache Spark 示例
https://spark.apache.org/docs/1.1.0/mllib-clustering.html#examples
来源如下:
import org.apache.spark.mllib.clustering.KMeans
import org.apache.spark.mllib.linalg.Vectors
// Load and parse the data
val data = sc.textFile("data/mllib/kmeans_data.txt")
val parsedData = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble)))
// Cluster the data into two classes using KMeans
val numClusters = 2
val numIterations = 20
val clusters = KMeans.train(parsedData, numClusters, numIterations)
// Evaluate clustering by computing Within Set Sum of Squared Errors
val WSSSE = clusters.computeCost(parsedData)
println("Within Set Sum of Squared Errors = " + WSSSE)
使用数据集:
0.0 0.0 0.0
0.1 0.1 0.1
0.2 0.2 0.2
9.0 9.0 9.0
9.1 9.1 9.1
9.2 9.2 9.2
我可以使用以下方法提取聚类中心:
println(clusters.clusterCenters.apply(0))
println(clusters.clusterCenters.apply(1))
返回
[9.1,9.1,9.1]
[0.10000000000000002,0.10000000000000002,0.10000000000000002]
但是有一些我不确定的项目,API 似乎不支持:
如何提取已添加到两个簇中的每个点?
如何为每个数据点添加标签,以便在查看每个簇中有哪些点的同时也可以确定每个点的标签?我是否需要更新 Spark Kmeans 实现才能实现此目的?
最佳答案
如果您使用的是java,
javaRDD cluster_indices = clusters.predict(parsedData);
因为预测重载。
关于scala - 使用 Apache Spark 提取 kmeans 集群信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743313/