K-Means算法是一種基于距離的聚類算法,采用迭代的方法,計算出K個聚類中心,把若干個點聚成K類。
MLlib實現(xiàn)K-Means算法的原理是,運行多個K-Means算法,每個稱為run,返回最好的那個聚類的類簇中心。初始的類簇中心,可以是隨機的,也可以是KMean||得來的,迭代達到一定的次數(shù),或者所有run都收斂時,算法就結(jié)束。
用Spark實現(xiàn)K-Means算法,首先修改pom文件,引入機器學(xué)習(xí)MLlib包:
1
2
3
4
5
|
< dependency > < groupId >org.apache.spark</ groupId > < artifactId >spark-mllib_2.10</ artifactId > < version >1.6.0</ version > </ dependency > |
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import org.apache.log 4 j.{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) // 設(shè)置運行環(huán)境 val conf = new SparkConf().setAppName( "K-Means" ).setMaster( "spark://master:7077" ) .setJars(Seq( "E:\\Intellij\\Projects\\SimpleGraphX\\SimpleGraphX.jar" )) val sc = new SparkContext(conf) // 裝載數(shù)據(jù)集 val data = sc.textFile( "hdfs://master:9000/kmeans_data.txt" , 1 ) val parsedData = data.map(s = > Vectors.dense(s.split( " " ).map( _ .toDouble))) // 將數(shù)據(jù)集聚類,2個類,20次迭代,形成數(shù)據(jù)模型 val numClusters = 2 val numIterations = 20 val model = KMeans.train(parsedData, numClusters, numIterations) // 數(shù)據(jù)模型的中心點 println( "Cluster centres:" ) for (c <- model.clusterCenters) { println( " " + c.toString) } // 使用誤差平方之和來評估數(shù)據(jù)模型 val cost = model.computeCost(parsedData) println( "Within Set Sum of Squared Errors = " + cost) // 使用模型測試單點數(shù)據(jù) 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)))) // 返回數(shù)據(jù)集和結(jié)果 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()方法裝載數(shù)據(jù)集,獲得RDD,再使用KMeans.train()方法根據(jù)RDD、K值和迭代次數(shù)得到一個KMeans模型。得到KMeans模型以后,可以判斷一組數(shù)據(jù)屬于哪一個類。具體方法是用Vectors.dense()方法生成一個Vector,然后用KMeans.predict()方法就可以返回屬于哪一個類。
運行結(jié)果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 |
總結(jié)
本文關(guān)于Spark實現(xiàn)K-Means算法代碼示例的全部內(nèi)容就到這里,希望對大家有所幫助。感如有不足之處,歡迎留言指出,小編會及時回復(fù)大家并更正,希望朋友們對本站多多支持!
原文鏈接:http://www.cnblogs.com/mstk/p/6925736.html