内容简介:K-Means算法是一种基于距离的聚类算法,采用迭代的方法,计算出K个聚类中心,把若干个点聚成K类。1. 输出4699
K-Means算法是一种基于距离的聚类算法,采用迭代的方法,计算出K个聚类中心,把若干个点聚成K类。
package com.immooc.spark import org.apache.log4j.{Level, Logger} import org.apache.spark.mllib.clustering.KMeans import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.{SparkConf, SparkContext} object KMeansTest { def main(args:Array[String]): Unit = { val conf = new SparkConf().setAppName("KMeansTest").setMaster("local[2]") val sc = new SparkContext(conf) Logger.getRootLogger.setLevel(Level.WARN) // 读取样本数据1,格式为LIBSVM format val data = sc.textFile("file:///Users/walle/Documents/D3/sparkmlib/kmeans_data.txt") val parsedData = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble))).cache() // 新建KMeans聚类模型,并训练 val initMode = "k-means||" val numClusters = 4 val numIterations = 100 val model = new KMeans(). setInitializationMode(initMode). setK(numClusters). setMaxIterations(numIterations). run(parsedData) val centers = model.clusterCenters println("centers") for (i <- 0 to centers.length - 1) { println(centers(i)(0) + "\t" + centers(i)(1)) } // 误差计算 val WSSSE = model.computeCost(parsedData) println("Within Set Sum of Squared Errors = " + WSSSE) } }
1. 输出
centers 9.05 9.05 0.05 0.05 9.2 9.2 0.2 0.2 Within Set Sum of Squared Errors = 0.03000000000004321
4699
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 监控聚合器系列之: open-falcon新聚合器polymetric
- elasticsearch学习笔记(七)——快速入门案例实战之电商网站商品管理:嵌套聚合,下钻分析,聚合分析
- mongodb高级聚合查询
- MongoDB聚合(aggregate)
- mongodb 聚合管道
- MongoDB指南---16、聚合
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Essential PHP Security
Chris Shiflett / O'Reilly Media / 2005-10-13 / USD 29.95
Being highly flexible in building dynamic, database-driven web applications makes the PHP programming language one of the most popular web development tools in use today. It also works beautifully wit......一起来看看 《Essential PHP Security》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
HTML 编码/解码
HTML 编码/解码