炼数成金 课程
1、机器学习分类
1)监督学习, 收集特征,把某一类特征归为归为一个目标,目标是由人标注的。如回归分析和统计分类,
二元分类,如 垃圾邮件判断。
多远分类,如网页归为体育,新闻,政治等。
2)无监督学习,没有认为标注,常见无监督学习有 聚类。
3)半监督学习,介于监督与无监督之间。
4)增强学习,通过观察来学习学习做成如何的动作,每个动作都会对环境有所影响,学习对象根据观察到的周围环境的反馈做出判断。
2、MLlib构成
1)基础部分
--Local vector 本地向量
-DenseVector 密集向量
-SparseVector 稀疏向量
--Labeled point 特征标量
--Local matrix 本地矩阵
--Distributed matrix 分布式矩阵
-RowMatrix
- Multivariate summary statistics
-IndexedRowMatrix
-CoordinateMatrix
2)示例演示
Vector
import org.apache.spark.mllib.Linalg.{Vector,Vectors} //(1.0,0.0,3.0) val dv:Vector = Vectors.dense(1.0,0.0,3.0)<span style="white-space:pre"> </span>//密集 val sv1:Vector=Vector.sparse(3,Array(0,2),Array(1.0,3.0))<span style="white-space:pre"> </span>//稀疏,参数1:向量长度,参数2:非0的位置,参数3:非0的值 val sv2:Vector=Vector.sparse(3,Seq((0,1.0),(2,3.0)))<span style="white-space:pre"> </span>//稀疏,参数1:向量长度,参数2:非0值得位置和值稀疏向量,大大减少了存储空间,提升了处理性能。
point
import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint //这里设置了个简单的二元判断 val pos = LabelePoint(1.0, Vectors.dense(1.0,0.0,3.0))<span style="white-space:pre"> </span> val neg = LabelePoint(0.0, Vectors.sparse(3,Array(0,2),Array(1.0,2.0)))
Local Matrix
1.0 2.0
3.0 4.0
5.0 6.0
import org.apache.spark.mllib.linalg.{Martix, Matrices} val dm:Matrix = Matrices.dense(3,2,Array(1.0,3.0,5.0,2.0,4.0,6.0))<span style="white-space:pre"> </span>//3行2列,参数1和2 为Int , Array中的值为Double
在分布式矩阵中, 行列参数都为Long
RowMatrix
import org.apache.spark.mllib.linalg.Vector import org.apache.spark.mllib.linalg.distributed.RowMatrix import org.apache.spark.mllib.stat.MultivarlatesStatisticalSummary val rows:RDD[Vector] = ...//一个本地向量的RDD var mat:RowMatrix = new RowMatrix(rows) var m = mat.numRows(); var n = mat.numCols(); var summary:MultivarlateStatisticalSummary = mat.computeColumnSummaryStatistics() println(summary.mean) //每一列的平均值 println(summary.variance) //每一列的方差 println(summary.numNonzeros) //每一列的非0数的数量
import org.apache.spark.mllib.linalg.distributed.{IndexedRow,IndexedRowMatrix,RowMatrix} var rows:RDD[IndexedRow] = ...//an RDD of indexed row var mat:IndexedRowMatrix = new IndexedRowMatrix(rows) val m = mat.numRow() val n = mat.numCols() val rowMat:RowMatrix = mat.toRowMatrix()
import org.apache.spark.mllib.alg.distributed.{CoordinateMatrix,MatrixEntry} val entries:RDD[MatrixEntry] = ...// an RDD of matrix entries a tuple of (i:Long, j:Long, value: Double) 2个坐标1个值 val mat:CoordinateMatrix = new CoordinateMatrix(entries) val m = mat.numRows() val n = mat.numCols() val indexedRowMatrix = mat.toIndexedRowMatrix()
import org.apache.log4j.{Level, Logger} import org.apache.spark.{SparkConf, SparkContext} 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.eclipse.jetty.server").setLevel(Level.OFF) // 设置运行环境 val conf = new SparkConf().setAppName("Kmeans").setMaster("local[4]") val sc = new SparkContext(conf) //装载数据集 val data = sc.textFile("/home/mmicky/IdeaProjects/machine-learning/kmeans_data.txt", 1) //这里 1 是为了分片1个,1个task,最终产生一个结果文件。 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 centers:") for (c <- model.clusterCenters) { println(" " + c.toString) } //使用误差平方之和来评估数据模型 val cost = model.computeCost(parsedData) println("Within Set Sum of Squared Errors = " + cost) //使用模型测试单点数据 println("Vectors 0.2 0.2 0.2 is belongs to clusters:" + model.predict(Vectors.dense("0.2 0.2 0.2".split(' ').map(_.toDouble)))) println("Vectors 0.25 0.25 0.25 is belongs to clusters:" + model.predict(Vectors.dense("0.25 0.25 0.25".split(' ').map(_.toDouble)))) println("Vectors 8 8 8 is belongs to clusters:" + model.predict(Vectors.dense("8 8 8".split(' ').map(_.toDouble)))) //交叉评估1,只返回结果 val testdata = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble))) val result1 = model.predict(testdata) result1.saveAsTextFile("/home/mmicky/IdeaProjects/machine-learning/result1") //交叉评估2,返回数据集和结果 val result2 = data.map { line => val linevectore = Vectors.dense(line.split(' ').map(_.toDouble)) val prediction = model.predict(linevectore) line + " " + prediction }.saveAsTextFile("/home/mmicky/IdeaProjects/machine-learning/result2") sc.stop() } }