1.什么是MLBase
2.MLbase机器学习算法的流程 Spark将机器学习算法都分成了两个模块:
MLbase提供了函数式编程语言Scala,利用MLlib可以很方便的实现机器学习的常用算法。 1 var X = load("some_data", 2 to 10) 2 var y = load("some_data", 1) 3 var (fn-model, summary) = doClassify(X, y) 代码解释:X是需要分类的数据集,y是从这个数据集里取的一个分类标签,doClassify()分类。 这样的处理有两个主要好处:
MLbase的三大组成部分之一:ML Optimizer,会选择它认为最适合的已经在内部实现好了的机器学习算法和相关参数,来处理用户输入的数据,并返回模型或别的帮助分析的结果。总体上的处理流程如下图:
MLbase不仅仅把结果返回给用户。在LLP、Optimizer,MLbase会存储一些中间结果和特征,然后会继续搜寻和测试结果更好的算法和相关参数,并且会通知用户。LLP内部实现的算法是可以扩充的。 总之,MLbase会自动寻找合适的算法,自动选择和优化,还可以进行扩充。 3.Scala实现KMeans算法 3.1 什么是KMeans算法 3.3 MLlib实现KMeans 以MLlib实现KMeans算法,利用MLlib KMeans训练出来的模型,可以对新的数据作出分类预测,具体见代码和输出结果。 Scala代码: 1 package com.hq 2 3 import org.apache.spark.mllib.clustering.KMeans 4 import org.apache.spark.mllib.linalg.Vectors 5 import org.apache.spark.{SparkContext, SparkConf} 6 7 object KMeansTest { 8 def main(args: Array[String]) { 9 if (args.length < 1) { 10 System.err.println("Usage: <file>") 11 System.exit(1) 12 } 13 14 val conf = new SparkConf() 15 val sc = new SparkContext(conf) 16 val data = sc.textFile(args(0)) 17 val parsedData = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble))) 18 val numClusters = 2 19 val numIterations = 20 20 val clusters = KMeans.train(parsedData,numClusters,numIterations) 21 22 println("------Predict the existing line in the analyzed data file: "+args(0)) 23 println("Vector 1.0 2.1 3.8 belongs to clustering "+ clusters.predict(Vectors.dense("1.0 2.1 3.8".split(' ').map(_.toDouble)))) 24 println("Vector 5.6 7.6 8.9 belongs to clustering "+ clusters.predict(Vectors.dense("5.6 7.6 8.9".split(' ').map(_.toDouble)))) 25 println("Vector 3.2 3.3 6.6 belongs to clustering "+ clusters.predict(Vectors.dense("3.2 3.3 6.6".split(' ').map(_.toDouble)))) 26 println("Vector 8.1 9.2 9.3 belongs to clustering "+ clusters.predict(Vectors.dense("8.1 9.2 9.3".split(' ').map(_.toDouble)))) 27 println("Vector 6.2 6.5 7.3 belongs to clustering "+ clusters.predict(Vectors.dense("6.2 6.5 7.3".split(' ').map(_.toDouble)))) 28 29 println("-------Predict the non-existent line in the analyzed data file: ----------------") 30 println("Vector 1.1 2.2 3.9 belongs to clustering "+ clusters.predict(Vectors.dense("1.1 2.2 3.9".split(' ').map(_.toDouble)))) 31 println("Vector 5.5 7.5 8.8 belongs to clustering "+ clusters.predict(Vectors.dense("5.5 7.5 8.8".split(' ').map(_.toDouble)))) 32 33 println("-------Evaluate clustering by computing Within Set Sum of Squared Errors:-----") 34 val wssse = clusters.computeCost(parsedData) 35 println("Within Set Sum of Squared Errors = "+ wssse) 36 sc.stop() 37 } 38 } 3.4 以Spark集群standalone方式运行 ①在IDEA打成jar包(如果忘记了,参见Spark:用Scala和Java实现WordCount),上传到用户目录下/home/ebupt/test/kmeans.jar ②准备训练样本数据:hdfs://eb170:8020/user/ebupt/kmeansData,内容如下 [ebupt@eb170 ~]$ hadoop fs -cat ./kmeansData 1.0 2.1 3.8
5.6 7.6 8.9
3.2 3.3 6.6
8.1 9.2 9.3
6.2 6.5 7.3 ③spark-submit提交运行 [ebupt@eb174 test]$ spark-submit --master spark://eb174:7077 --name KmeansWithMLib --class com.hq.KMeansTest --executor-memory 2G --total-executor-cores 4 ~/test/kmeans.jar hdfs://eb170:8020/user/ebupt/kmeansData 输出结果摘要: 1 ------Predict the existing line in the analyzed data file: hdfs://eb170:8020/user/ebupt/kmeansData 2 Vector 1.0 2.1 3.8 belongs to clustering 0 3 Vector 5.6 7.6 8.9 belongs to clustering 1 4 Vector 3.2 3.3 6.6 belongs to clustering 0 5 Vector 8.1 9.2 9.3 belongs to clustering 1 6 Vector 6.2 6.5 7.3 belongs to clustering 1 7 -------Predict the non-existent line in the analyzed data file: ---------------- 8 Vector 1.1 2.2 3.9 belongs to clustering 0 9 Vector 5.5 7.5 8.8 belongs to clustering 1 10 -------Evaluate clustering by computing Within Set Sum of Squared Errors:----- 11 Within Set Sum of Squared Errors = 16.393333333333388 4.MLbase总结 本文主要介绍了MLbase如何实现机器学习算法,简单介绍了MLBase的设计思想。总的来说,Mlbase的核心是ML Optimizer,把声明式的任务转化成复杂的学习计划,输出最优的模型和计算结果。
5.参考文献
|
|
来自: 昵称23016082 > 《spark》