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. Python之路 day2 文件基础操作

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...

  2. 自动生成build.xml文件

    使用Eclipse 自动生成 Ant的Build.xml 配置文件,选择要生成Build.xml文件的项目,鼠标右键, Export-> General -> Ant Buildfiles ...

  3. 移动端重构实战系列2——line list

    这个line list的名字是我自己起的(大概的意思是单行列表),要实现的东西为sheral的line list,对应的scss组件为_line-list.scss,下图为line-list的一个缩影 ...

  4. css实现定高的元素在不定高的容器中水平垂直居中(兼容IE8及以上)

    容器设置相对定位 元素设置宽高,并使用绝对定位,上下左右值均为0,margin:auto 如下所示: <!DOCTYPE html> <html> <head lang= ...

  5. express+gulp构建项目(四)env环境变量

    这里的文件的作用是负责设置env环境变量和日志. index.js try { require('dotenv').load({silent: true}); //dotenv从一个.env文件中读取 ...

  6. C#基础

    .net/dotnet:一般指.NetFramework框架,一种平台,一种技术. c#(sharp):一种编程语言,可以开发基于.net平台的应用. Java:是一种技术,又是一门语言: .net应 ...

  7. python——SQL基本使用

    终于学到数据库操作了,这意味着什么?以后再也不用从文件里读写数据了,过程实在太复杂了~~~为了纪念这个激动人心的时刻,一定要写一篇博客! 使用mysql数据库——增 插入一条数据 首先,还是先解释一下 ...

  8. android intent和intent action大全

    1.Intent的用法:(1)用Action跳转1,使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了 ...

  9. 转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数

    关于处理小数点位数的几个oracle函数() 1. 取四舍五入的几位小数 select round(1.2345, 3) from dual; 结果:1.235 2. 保留两位小数,只舍 select ...

  10. JS学习笔记--仿手机发送内容交互

    学习JS笔记----记录上课中学习的知识点,分享下老师教的内容: 1.html内容 <div id="box"> <div id="message&qu ...