Spark MLlib

炼数成金 课程

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

Distributed Matrix

在分布式矩阵中, 行列参数都为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数的数量

IndexedRowMatrix

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()


CoordinateMatrix

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()


3、kmeans 算法演示

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()
  }
}






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值