Spark が K-Means アルゴリズムを実装するコード例 - K-Means アルゴリズムは、反復法を使用して K クラスター中心を計算し、いくつかの点を K カテゴリにクラスター化する距離ベースのクラスタリング アルゴリズムです。MLlib は K-Means アルゴリズムの原理を実装します
K-Means アルゴリズムは、反復手法を使用して K 個のクラスター中心を計算し、いくつかの点を K 個のカテゴリーにクラスター化する距離ベースのクラスタリング アルゴリズムです。
K-Means アルゴリズムを実装する MLlib の原理は、それぞれを実行と呼ばれる複数の K-Means アルゴリズムを実行して、最適なクラスターのクラスター中心を返すことです。初期クラスター中心はランダムであるか、KMean|| から取得できます。アルゴリズムは、反復が特定の回数に達するか、すべての実行が収束すると終了します。
Spark を使用して K-Means アルゴリズムを実装します。まず pom ファイルを変更し、機械学習 MLlib パッケージを導入します。
org.apache.spark
spark-mllib_2.10
1.6.0
コード:
import org.apache.log4j.{Level,Logger}
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.mllib.clustering.KMeans
import org.apache.spark.mllib.linalg.Vectors
object Kmeans {
def main(args:Array[String]) = {
// 屏蔽日志
Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
Logger.getLogger("org.apache.jetty.server").setLevel(Level.OFF)
// 设置运行环境
val cOnf= new SparkConf().setAppName("K-Means").setMaster("spark://master:7077")
.setJars(Seq("E:\\Intellij\\Projects\\SimpleGraphX\\SimpleGraphX.jar"))
val sc = new SparkContext(conf)
// 装载数据集
val data = sc.textFile("hdfs://master:9000/kmeans_data.txt", 1)
val parsedData = data.map(s => Vectors.dense(s.split(" ").map(_.toDouble)))
// 将数据集聚类,2个类,20次迭代,形成数据模型
val numClusters = 2
val numIteratiOns= 20
val model = KMeans.train(parsedData, numClusters, numIterations)
// 数据模型的中心点
println("Cluster centres:")
for(c <- model.clusterCenters) {
println(" " + c.toString)
}
// 使用误差平方之和来评估数据模型
val cost = model.computeCost(parsedData)
println("Within Set Sum of Squared Errors = " + cost)
// 使用模型测试单点数据
println("Vectors 7.3 1.5 10.9 is belong to cluster:" + model.predict(Vectors.dense("7.3 1.5 10.9".split(" ")
.map(_.toDouble))))
println("Vectors 4.2 11.2 2.7 is belong to cluster:" + model.predict(Vectors.dense("4.2 11.2 2.7".split(" ")
.map(_.toDouble))))
println("Vectors 18.0 4.5 3.8 is belong to cluster:" + model.predict(Vectors.dense("1.0 14.5 73.8".split(" ")
.map(_.toDouble))))
// 返回数据集和结果
val result = data.map {
line =>
val linevectore = Vectors.dense(line.split(" ").map(_.toDouble))
val prediction = model.predict(linevectore)
line + " " + prediction
}.collect.foreach(println)
sc.stop
}
}
textFile() メソッドを使用してデータ セットをロードし、RDD を取得します。次に、KMeans.train() メソッドを使用して、RDD、K 値、および反復回数に基づいて KMeans モデルを取得します。KMeans モデルを取得した後、データのセットがどのクラスに属しているかを判断できます。具体的な方法は、Vectors.dense() メソッドを使用して Vector を生成し、次に KMeans.predict() メソッドを使用してそれが属するクラスを返すことです。
操作結果:
Cluster centres:
[6.062499999999999,6.7124999999999995,11.5]
[3.5,12.2,60.0]
Within Set Sum of Squared Errors = 943.2074999999998
Vectors 7.3 1.5 10.9 is belong to cluster:0
Vectors 4.2 11.2 2.7 is belong to cluster:0
Vectors 18.0 4.5 3.8 is belong to cluster:1
0.0 0.0 5.0 0
0.1 10.1 0.1 0
1.2 5.2 13.5 0
9.5 9.0 9.0 0
9.1 9.1 9.1 0
19.2 9.4 29.2 0
5.8 3.0 18.0 0
3.5 12.2 60.0 1
3.6 7.9 8.1 0