MLlib:Machine Learning Library。主要内容包括:

  • 数据类型
  • 统计工具
    • summary statistics
    • correlations
    • stratified sampling
    • hypothesis testing
    • random data generation
  • 分类和回归
    • 线性模型(SVM,逻辑回归,线性回归)
    • 朴素贝叶斯
    • 决策树
    • ensembles of trees(随机森林和Gradient-Boosted Trees)
    • isotonic regression
  • 协同过滤
    • ALS(alternating least squares)     
  • 聚类
    • k-means
    • 高斯混合模型
    • power iteration clustering(PIC)
    • LDA(latent Dirichlet allocation)
    • 流式k-means 
  • 降维
    • SVD
    • PCA
  • 特征提取和转换
  • Frequent pattern mining
    • FP-growth
  • 优化  
    • stochastic gradient descent
    • limited-memory BFGS (L-BFGS)

    

I.数据类型

MLlib的数据类型主要是local vectors和local matrices,潜在的代数操作由Breeze和jblas提供。

1.local vector 有int型和double型,下标从0开始,分为dense和sparse 两种。

Local vector的基本类型是Vector,包括:DenseVector和SparseVector。

  1. import org.apache.spark.mllib.linalg.{Vector, Vectors}
  2.  
  3. // Create a dense vector (1.0, 0.0, 3.0).
  4. val dv: Vector = Vectors.dense(1.0, 0.0, 3.0)
  5. // Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
  6. val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))
  7. // Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.
  8. val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0)))

Scala imports scala.collection.immutable.Vector by default, so you have to import org.apache.spark.mllib.linalg.Vector explicitly to use MLlib’s Vector.

  • MLlib中一个监督学习的训练样本被称为“labeled point”。
  1. import org.apache.spark.mllib.linalg.Vectors
  2. import org.apache.spark.mllib.regression.LabeledPoint
  3.  
  4. // Create a labeled point with a positive label and a dense feature vector.
  5. val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
  6.  
  7. // Create a labeled point with a negative label and a sparse feature vector.
  8. val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)))
  • MLlib supports reading training examples stored in LIBSVM format, which is the default format used by LIBSVM and LIBLINEAR.
  1. import org.apache.spark.mllib.regression.LabeledPoint
  2. import org.apache.spark.mllib.util.MLUtils
  3. import org.apache.spark.rdd.RDD
  4.  
  5. val examples: RDD[LabeledPoint] = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")

2.local matrix

A local matrix has integer-typed row and column indices and double-typed values, stored on a single machine. MLlib supports dense matrices, whose entry values are stored in a single double array in column major.

  1. import org.apache.spark.mllib.linalg.{Matrix, Matrices}
  2.  
  3. // Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
  4. val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))
  • A distributed matrix has long-typed row and column indices and double-typed values, stored distributively in one or more RDDs.It is very important to choose the right format to store large and distributed matrices.  A RowMatrix is a row-oriented distributed matrix without meaningful row indices, e.g., a collection of feature vectors. It is backed by an RDD of its rows, where each row is a local vector. We assume that the number of columns is not huge. An IndexedRowMatrix is similar to a RowMatrix but with row indices, which can be used for identifying rows and executing joins. A CoordinateMatrix is a distributed matrix stored in coordinate list (COO) format, backed by an RDD of its entries.
  1. import org.apache.spark.mllib.linalg.Vector
  2. import org.apache.spark.mllib.linalg.distributed.RowMatrix
  3.  
  4. val rows: RDD[Vector] = ... // an RDD of local vectors
  5. // Create a RowMatrix from an RDD[Vector].
  6. val mat: RowMatrix = new RowMatrix(rows)
  7.  
  8. // Get its size.
  9. val m = mat.numRows()
  10. val n = mat.numCols()
  11.  
  12. import org.apache.spark.mllib.linalg.distributed.{IndexedRow, IndexedRowMatrix, RowMatrix}
  13.  
  14. val rows: RDD[IndexedRow] = ... // an RDD of indexed rows
  15. // Create an IndexedRowMatrix from an RDD[IndexedRow].
  16. val mat: IndexedRowMatrix = new IndexedRowMatrix(rows)
  17. // Drop its row indices.
  18. val rowMat: RowMatrix = mat.toRowMatrix()
  19.  
  20. import org.apache.spark.mllib.linalg.distributed.{CoordinateMatrix, MatrixEntry}
  21.  
  22. val entries: RDD[MatrixEntry] = ... // an RDD of matrix entries
  23. // Create a CoordinateMatrix from an RDD[MatrixEntry].
  24. val mat: CoordinateMatrix = new CoordinateMatrix(entries)// Convert it to an IndexRowMatrix whose rows are sparse vectors.
  25. val indexedRowMatrix = mat.toIndexedRowMatrix()
  • BlockMatrix is a distributed matrix backed by an RDD of MatrixBlocks, where a MatrixBlock is a tuple of ((Int, Int), Matrix), where the (Int, Int) is the index of the block, and Matrix is the sub-matrix at the given index. BlockMatrix supports methods such as add and multiply with another BlockMatrix.A BlockMatrix can be most easily created from an IndexedRowMatrix or CoordinateMatrix by calling toBlockMatrix.
  1. import org.apache.spark.mllib.linalg.distributed.{BlockMatrix, CoordinateMatrix, MatrixEntry}
  2.  
  3. val entries: RDD[MatrixEntry] = ... // an RDD of (i, j, v) matrix entries
  4. // Create a CoordinateMatrix from an RDD[MatrixEntry].
  5. val coordMat: CoordinateMatrix = new CoordinateMatrix(entries)
  6. // Transform the CoordinateMatrix to a BlockMatrix
  7. val matA: BlockMatrix = coordMat.toBlockMatrix().cache()
  8.  
  9. // Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.
  10. // Nothing happens if it is valid.
  11. matA.validate()
  12.  
  13. // Calculate A^T A.
  14. val ata = matA.transpose.multiply(matA)

用到什么model先看介绍,再查API doc:  https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.package

Spark 学习笔记:(四)MLlib基础的更多相关文章

  1. Java基础学习笔记四 Java基础语法

    数组 数组的需求 现在需要统计某公司员工的工资情况,例如计算平均工资.最高工资等.假设该公司有50名员工,用前面所学的知识完成,那么程序首先需要声明50个变量来分别记住每位员工的工资,这样做会显得很麻 ...

  2. Spark学习笔记——基于MLlib的机器学习

    使用MLlib库中的机器学习算法对垃圾邮件进行分类 分类的垃圾邮件的如图中分成4个文件夹,两个文件夹是训练集合,两个文件夹是测试集合 build.sbt文件 name := "spark-f ...

  3. Java IO学习笔记四:Socket基础

    作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...

  4. spark学习笔记总结-spark入门资料精化

    Spark学习笔记 Spark简介 spark 可以很容易和yarn结合,直接调用HDFS.Hbase上面的数据,和hadoop结合.配置很容易. spark发展迅猛,框架比hadoop更加灵活实用. ...

  5. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  6. Spark学习笔记之SparkRDD

    Spark学习笔记之SparkRDD 一.   基本概念 RDD(resilient distributed datasets)弹性分布式数据集. 来自于两方面 ①   内存集合和外部存储系统 ②   ...

  7. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  8. Spark学习笔记0——简单了解和技术架构

    目录 Spark学习笔记0--简单了解和技术架构 什么是Spark 技术架构和软件栈 Spark Core Spark SQL Spark Streaming MLlib GraphX 集群管理器 受 ...

  9. Java学习笔记:语言基础

    Java学习笔记:语言基础 2014-1-31   最近开始学习Java,目的倒不在于想深入的掌握Java开发,而是想了解Java的基本语法,可以阅读Java源代码,从而拓展一些知识面.同时为学习An ...

  10. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

随机推荐

  1. 在使用Cocos2d-JS 开发过程中需要用到的单体设计模式

    JavaScript 单体模式的一种实现 T.getInstance = (function () { var instance = null; return function () { return ...

  2. ubuntu 转换图片格式的方法(sam2p, imagemagick)

    (1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...

  3. [uiautomator篇]uiwatcher 的使用场景

    http://www.yangyanxing.com/article/use-watcher-in-uiautomator.html   今天在uiautomator中实践了watcher的用法,这个 ...

  4. 九度oj 题目1534:数组中第K小的数字

    题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6]. 现在给你数组A和B ...

  5. shell的echo命令

    echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串.命令格式: echo arg 您可以使用echo实现更复杂的输出格式控制. 显示转义字符 echo "\"It ...

  6. BZOJ 4161 Shlw loves matrixI ——特征多项式

    矩阵乘法递推的新姿势. 叉姐论文里有讲到 利用特征多项式进行递推,然后可以做到k^2logn #include <cstdio> #include <cstring> #inc ...

  7. [luoguP2805] [NOI2009]植物大战僵尸(网络流)

    传送门 结论:这是最大权闭合图的模型 因为可能A保护B,B保护A,出现环. 所以由植物A向植物A保护的植物连边,然后拓扑排序,将环去掉. 然后将拓扑排序的边反向连,建立最大权闭合图的模型. 跑最大流( ...

  8. ORACLE:除去回车符,换行符

    ORACLE:除去回车符,换行符 replace(fa,chr(),'') ; --- 除去回车符 replace(fa,chr(),'') ; --- 除去换行符  

  9. Codevs 3111 CYD啃骨头

    时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold 题目描述 Description: CYD吃饭时有N个骨头可以啃,但CYD要午睡了,所以他只有M分钟吃饭,已知 ...

  10. centos7 搭建hadoop

    参考文档:http://blog.csdn.net/xiaoxiangzi222/article/details/52757168 https://waylau.com/centos-7-instal ...