使用 Apache Spark 提取 kmeans 集群信息
Extract kmeans cluster information using Apache Spark
我已经在
实现了 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))
哪个returns
[9.1,9.1,9.1]
[0.10000000000000002,0.10000000000000002,0.10000000000000002]
但有些项目我不确定,API 似乎不支持这些项目:
如何提取已添加到两个聚类中的每一个的点?
如何为每个数据点添加标签,以便在查看每个聚类中有哪些点的同时也可以确定每个点的标签?我是否需要更新 Spark Kmeans 实现才能实现此目的?
您要查找的方法是predict(),但不属于KMeans.scala。是 class KMeansModel.scala 的一部分(这是 KMeans.train(...) 的 return 类型 )
用途是:
clusters.predict(data_to_cluster)
如果您正在使用 java,
javaRDD cluster_indices = clusters.predict(parsedData);
因为预测过载。
我已经在
实现了 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))
哪个returns
[9.1,9.1,9.1]
[0.10000000000000002,0.10000000000000002,0.10000000000000002]
但有些项目我不确定,API 似乎不支持这些项目:
如何提取已添加到两个聚类中的每一个的点?
如何为每个数据点添加标签,以便在查看每个聚类中有哪些点的同时也可以确定每个点的标签?我是否需要更新 Spark Kmeans 实现才能实现此目的?
您要查找的方法是predict(),但不属于KMeans.scala。是 class KMeansModel.scala 的一部分(这是 KMeans.train(...) 的 return 类型 )
用途是:
clusters.predict(data_to_cluster)
如果您正在使用 java,
javaRDD cluster_indices = clusters.predict(parsedData);
因为预测过载。