Spark实现K-Means算法代码示例

2025-05-27 0 82

K-Means算法是一种基于距离的聚类算法,采用迭代的方法,计算出K个聚类中心,把若干个点聚成K类。

MLlib实现K-Means算法的原理是,运行多个K-Means算法,每个称为run,返回最好的那个聚类的类簇中心。初始的类簇中心,可以是随机的,也可以是KMean||得来的,迭代达到一定的次数,或者所有run都收敛时,算法就结束。

用Spark实现K-Means算法,首先修改pom文件,引入机器学习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.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()方法就可以返回属于哪一个类。

运行结果:

?

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

总结

本文关于Spark实现K-Means算法代码示例的全部内容就到这里,希望对大家有所帮助。感如有不足之处,欢迎留言指出,小编会及时回复大家并更正,希望朋友们对本站多多支持!

原文链接:http://www.cnblogs.com/mstk/p/6925736.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 Spark实现K-Means算法代码示例 https://www.kuaiidc.com/67949.html

相关文章

发表评论
暂无评论