MLlib 支持存放在单机上的本地向量和矩阵,也支持通过多个RDD实现的分布式矩阵。因此MLlib的数据类型主要分为两大类:一个是本地单机向量;另一个是分布式矩阵。下面分别介绍一下这两大类都有哪些类型:

1、Local vector(本地向量)

(1)Vector

  最基本的类型是Vector,该类型索引是从0开始的整型类型,值类型是double类型。并提供了两个实现:DenseVector and SparseVector。但是一把情况下都是推荐使用工厂方法来创建Vector。如下所示:

import org.apache.spark.mllib.linalg.{Vector, Vectors}

// Create a dense vector (1.0, 0.0, 3.0).
val dv: Vector = Vectors.dense(1.0, 0.0, 3.0)
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.
val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0)))

(2)LabeledPoint

  LabeledPoint类型一般用于有监督的学习算法当中,因为该类型会标记对应的标签。并且第一个参数就是标签,第二个参数是一个vector类型的数据。

val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))

(3)Local matrix

  Local matrix是有一个int类型的行索引和列索引,和double类型的值。并且存储在单机。Local matrix最基本的类型是Matrix ,也提供了两个实现类型:DenseMatrix, and SparseMatrix。但是依伴推荐使用工厂方法:Matrices 。 如下所示:

    val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))
val sm: Matrix = Matrices.sparse(3, 2, Array(0, 1, 3), Array(0, 2, 1), Array(9, 6, 8))

2、Distributed matrix(分布式矩阵)

(1)RowMatrix

  RowMatrix矩阵是一个基于行的,且没有索引的一个分布式矩阵,它的所有行组成一个RDD,它的每一行是一个local Vector。由于它的行类型是Local Vector,所以它的列应该是有限的。因为它必须能保证能够存储在一台机器内。如下所示:

    val rows = sc.textFile("/user/liujiyu/spark/mldata1.txt")
.map(_.split(' ') // 转换为RDD[Array[String]]类型
.map(_.toDouble)) // 转换为RDD[Array[Double]]类型
.map(line => Vectors.dense(line)) //转换为RDD[Vector]类型 // Create a RowMatrix from an RDD[Vector].
val mat: RowMatrix = new RowMatrix(rows)

(2)IndexedRowMatrix

  IndexedRowMatrix类型与RowMatrix类型相似,但是IndexedRowMatrix拥有强大的行索引。IndexedRowMatrix能够由RDD[IndexedRow]创建,而IndexedRow是由(Long,Vector)封装。

val rows1 = sc.textFile("/user/liujiyu/spark/mldata1.txt")
.map(_.split(' ') // 转换为RDD[Array[String]]类型
.map(_.toDouble)) // 转换为RDD[Array[Double]]类型
.map(line => Vectors.dense(line)) //转换为RDD[Vector]类型
.map((vc) => new IndexedRow(vc.size, vc)) //IndexedRow 带有行索引的矩阵,初始化的参数,列数和每一行的vector val irm = new IndexedRowMatrix(rows1)

(3)CoordinateMatrix(坐标矩阵)

  CoordinateMatrix是一个分布式矩阵,它是由Entry组成的一个RDD,每一个Entry是由(i:Long,j:Long,value:Double)封装。这里的i表示的是行索引,j表示的是列索引,value表示的对应的值。CoordinateMatrix能够通过RDD[MatrixEntry]来创建。如果矩阵是非常大的而且稀疏,坐标矩阵一定是最好的选择。坐标矩阵则是通过RDD[MatrixEntry]实例创建,MatrixEntry是(long,long.Double)封装形式。如下所示:

对应的矩阵文件mldata1.txt:
                  1 1 4
                  2 6 2
                  1 3 4
                  2 3 4
                  2 8 1
                  3 2 4
                  5 1 3
读取该文件,并初始化为CoordinateMatrix:

val rows2 = sc.textFile("/user/liujiyu/spark/mldata1.txt")
.map(_.split(' ') // 转换为RDD[Array[String]]类型
// .map(_.toDouble)) // 转换为RDD[Array[Double]]类型
.map(m => (m(0).toLong, m(1).toLong, m(2).toDouble))
.map((vc) => new MatrixEntry(vc._1, vc._2, vc._3)) //IndexedRow 带有行索引的矩阵,初始化的参数,列数和每一行的vector val cm = new CoordinateMatrix(rows2)

(4)BlockMatrix

  BlockMatrix是一个分布式矩阵,它是由MatrixBlocks组成的一个RDD 。这里的MatrixBlocks是由字典类型((Int,Int),Matrix)组成。这里(Int,Int)是block的索引,Matrix是这个给定的尺寸rowsPerBlock x colsPerBlock的子矩阵。

  BlockMatrix能够容易通过IndexedRowMatrix or CoordinateMatrixtoBlockMatrix方法来创建。toBlockMatrix方法默认创建的blocks的大小是1024*1024。用户可以通过传递参数的方式来改变这个blocks的大小,如:toBlockMatrix(rowsPerBlock, colsPerBlock)

    //A BlockMatrix can be most easily created from an IndexedRowMatrix or CoordinateMatrix by calling toBlockMatrix.

    val matA: BlockMatrix = cm.toBlockMatrix().cache()

    // Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.
// Nothing happens if it is valid.
matA.validate() // Calculate A^T A.
val ata = matA.transpose.multiply(matA)

     

Spark MLlib Data Type的更多相关文章

  1. Spark MLlib之线性回归源代码分析

    1.理论基础 线性回归(Linear Regression)问题属于监督学习(Supervised Learning)范畴,又称分类(Classification)或归纳学习(Inductive Le ...

  2. Spark MLlib - Decision Tree源码分析

    http://spark.apache.org/docs/latest/mllib-decision-tree.html 以决策树作为开始,因为简单,而且也比较容易用到,当前的boosting或ran ...

  3. Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1

    3.Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1 http://blog.csdn.net/sunbow0 ...

  4. Spark Mllib框架1

    1. 概述 1.1 功能 MLlib是Spark的机器学习(machine learing)库,其目标是使得机器学习的使用更加方便和简单,其具有如下功能: ML算法:常用的学习算法,包括分类.回归.聚 ...

  5. spark MLlib Classification and regression 学习

    二分类:SVMs,logistic regression,decision trees,random forests,gradient-boosted trees,naive Bayes 多分类:  ...

  6. RandomForest in Spark MLLib

    决策树类模型 ml中的classification和regression主要基于以下几类: classification:决策树及其相关的集成算法,Logistics回归,多层感知模型: regres ...

  7. Spark Mllib源码分析

    1. Param Spark ML使用一个自定义的Map(ParmaMap类型),其实该类内部使用了mutable.Map容器来存储数据. 如下所示其定义: Class ParamMap privat ...

  8. Spark MLlib框架详解

    1. 概述 1.1 功能 MLlib是Spark的机器学习(machine learing)库,其目标是使得机器学习的使用更加方便和简单,其具有如下功能: ML算法:常用的学习算法,包括分类.回归.聚 ...

  9. Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1

    Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1 http://blog.csdn.net/sunbow0 Spark ML ...

随机推荐

  1. json_encode 中文乱码

    用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似"\u***"的格式, 还会在一定程度上增加传输的数据量. 而在PHP5.4, 这个问题终 ...

  2. Apache Commons Chain

    http://commons.apache.org/proper/commons-chain/ http://commons.apache.org/proper/commons-chain/cookb ...

  3. jenkins+git+maven搭建自动化部署项目环境

    简介    折腾了两个晚上,趁着今晚比较有空,把jenkins+git+maven搭建自动化部署项目环境搭建的过程记录一下,这里我把github作为git的远程仓库(https://github.co ...

  4. Python正则式的基本用法

    Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...

  5. ps切图抠图详解-web前端(转)

    网页设计在技术层面上,第一步是美工做出网页效果图,第二步就是网页前端进行网页切图.网页切图工具常用的有fireworks.PS,这里使用PS进行网页切图. 我们通过设计稿,得到我们想要的产出物(如.p ...

  6. poj2965 The Pilots Brothers' refrigerator

    题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...

  7. Python中文乱码

    1,注意:请使用智慧型浏览器 "CHROME" 配合理解和运作本文中提到的程序. 2,提示:谷歌的CHROME浏览器是迄今为止最智慧的浏览器,没有之一,只有第一. 3,谷歌的CHR ...

  8. mac OS.NE开发环境搭建

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入,申请备注填写姓名+技术+工作年限) Q  Q:408365330     E-Mail:eg ...

  9. Ubuntu搭建svn服务器

    一,安装必须的软件包. sudo apt-getinstall subversion 二,基本的SVN服务器配置        1,新建一个目录用于存储SVN所有文件                # ...

  10. 2016年最佳Linux发行版排行榜

    2015年,不管在企业市场还是个人消费市场都是 Linux 非常重要的一年. 最好的回归发行版:openSUSE openSUSE 背后的 SUSE 公司是最老的 Linux 企业,它成立于 Linu ...