Spark Distributed matrix 分布式矩阵
RowMatrix行矩阵
import org.apache.spark.rdd.RDD
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.linalg.distributed.RowMatrix val df1 = Seq(
| (1.0, 2.0, 3.0),
| (1.1, 2.1, 3.1),
| (1.2, 2.2, 3.2)).toDF("c1", "c2", "c3")
df1: org.apache.spark.sql.DataFrame = [c1: double, c2: double ... 1 more field] df1.show
+---+---+---+
| c1| c2| c3|
+---+---+---+
|1.0|2.0|3.0|
|1.1|2.1|3.1|
|1.2|2.2|3.2|
+---+---+---+ // DataFrame转换成RDD[Vector]
val rowsVector= df1.rdd.map {
| x =>
| Vectors.dense(
| x(0).toString().toDouble,
| x(1).toString().toDouble,
| x(2).toString().toDouble)
| }
rowsVector: org.apache.spark.rdd.RDD[org.apache.spark.mllib.linalg.Vector] = MapPartitionsRDD[4] at map // Create a RowMatrix from an RDD[Vector].
val mat1: RowMatrix = new RowMatrix(rowsVector)
mat1: org.apache.spark.mllib.linalg.distributed.RowMatrix = org.apache.spark.mllib.linalg.distributed.RowMatrix@7ba821ef // Get its size.
val m = mat1.numRows()
m: Long = 3 val n = mat1.numCols()
n: Long = 3 // 将RowMatrix转换成DataFrame
val resDF = mat1.rows.map {
| x =>
| (x(0).toDouble,
| x(1).toDouble,
| x(2).toDouble)
| }.toDF("c1", "c2", "c3")
resDF: org.apache.spark.sql.DataFrame = [c1: double, c2: double ... 1 more field] resDF.show
+---+---+---+
| c1| c2| c3|
+---+---+---+
|1.0|2.0|3.0|
|1.1|2.1|3.1|
|1.2|2.2|3.2|
+---+---+---+ mat1.rows.collect().take(10)
res3: Array[org.apache.spark.mllib.linalg.Vector] = Array([1.0,2.0,3.0], [1.1,2.1,3.1], [1.2,2.2,3.2])
CoordinateMatrix坐标矩阵
import org.apache.spark.rdd.RDD
import org.apache.spark.mllib.linalg.distributed.{CoordinateMatrix, MatrixEntry} // 第一列:行坐标;第二列:列坐标;第三列:矩阵元素
val df = Seq(
| (0, 0, 1.1), (0, 1, 1.2), (0, 2, 1.3),
| (1, 0, 2.1), (1, 1, 2.2), (1, 2, 2.3),
| (2, 0, 3.1), (2, 1, 3.2), (2, 2, 3.3),
| (3, 0, 4.1), (3, 1, 4.2), (3, 2, 4.3)).toDF("row", "col", "value")
df: org.apache.spark.sql.DataFrame = [row: int, col: int ... 1 more field] df.show
+---+---+-----+
|row|col|value|
+---+---+-----+
| 0| 0| 1.1|
| 0| 1| 1.2|
| 0| 2| 1.3|
| 1| 0| 2.1|
| 1| 1| 2.2|
| 1| 2| 2.3|
| 2| 0| 3.1|
| 2| 1| 3.2|
| 2| 2| 3.3|
| 3| 0| 4.1|
| 3| 1| 4.2|
| 3| 2| 4.3|
+---+---+-----+ // 生成入口矩阵
val entr = df.rdd.map { x =>
| val a = x(0).toString().toLong
| val b = x(1).toString().toLong
| val c = x(2).toString().toDouble
| MatrixEntry(a, b, c)
| }
entr: org.apache.spark.rdd.RDD[org.apache.spark.mllib.linalg.distributed.MatrixEntry] = MapPartitionsRDD[20] at map // 生成坐标矩阵
val mat: CoordinateMatrix = new CoordinateMatrix(entr)
mat: org.apache.spark.mllib.linalg.distributed.CoordinateMatrix = org.apache.spark.mllib.linalg.distributed.CoordinateMatrix@5381deec mat.numRows()
res5: Long = 4 mat.numCols()
res6: Long = 3 mat.entries.collect().take(10)
res7: Array[org.apache.spark.mllib.linalg.distributed.MatrixEntry] = Array(MatrixEntry(0,0,1.1), MatrixEntry(0,1,1.2), MatrixEntry(0,2,1.3), MatrixEntry(1,0,2.1), MatrixEntry(1,1,2.2), MatrixEntry(1,2,2.3), MatrixEntry(2,0,3.1), MatrixEntry(2,1,3.2), MatrixEntry(2,2,3.3), MatrixEntry(3,0,4.1)) // 坐标矩阵转成,带行索引的DataFrame,行索引为行坐标
val t = mat.toIndexedRowMatrix().rows.map { x =>
| val v=x.vector
| (x.index,v(0).toDouble, v(1).toDouble, v(2).toDouble)
| }
t: org.apache.spark.rdd.RDD[(Long, Double, Double, Double)] = MapPartitionsRDD[33] at map t.toDF().show
+---+---+---+---+
| _1| _2| _3| _4|
+---+---+---+---+
| 0|1.1|1.2|1.3|
| 1|2.1|2.2|2.3|
| 2|3.1|3.2|3.3|
| 3|4.1|4.2|4.3|
+---+---+---+---+ // 坐标矩阵转成DataFrame
val t1 = mat.toRowMatrix().rows.map { x =>
| (x(0).toDouble, x(1).toDouble, x(2).toDouble)
| }
t1: org.apache.spark.rdd.RDD[(Double, Double, Double)] = MapPartitionsRDD[26] at map t1.toDF().show
+---+---+---+
| _1| _2| _3|
+---+---+---+
|1.1|1.2|1.3|
|3.1|3.2|3.3|
|2.1|2.2|2.3|
|4.1|4.2|4.3|
+---+---+---+
Spark Distributed matrix 分布式矩阵的更多相关文章
- Spark Mllib里的分布式矩阵(行矩阵、带有行索引的行矩阵、坐标矩阵和块矩阵概念、构成)(图文详解)
不多说,直接上干货! Distributed matrix : 分布式矩阵 一般能采用分布式矩阵,说明这数据存储下来,量还是有一定的.在Spark Mllib里,提供了四种分布式矩阵存储形式,均由支 ...
- Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API
Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API 关键词:Local vector,Labeled point,Local matrix,Distrib ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- A Practical Guide to Distributed Scrum - 分布式Scrum的实用指南 - 读书笔记
最近读了这本IBM出的<A Practical Guide to Distributed Scrum>(分布式Scrum的实用指南),书中的章节结构比较清楚,是针对Scrum项目进行,一个 ...
- 在Hadoop2.2基础上安装Spark(伪分布式)
没想到,在我的hadoop2.2.0小集群上上安装传说中的Spark竟然如此顺利,可能是因为和搭建Hadoop时比较像,更多需要学习的地方还是scala编程和RDD机制吧 总之,开个好头 原来的集群: ...
- css3 matrix 2D矩阵和canvas transform 2D矩阵
一看到“2D矩阵”这个高大上的名词,有的同学可能会有种畏惧感,“矩阵”,看起来好高深的样子,我还是看点简单的吧.其实本文就很简单,你只需要有一点点css3 transform的基础就好. 没有前戏,直 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- 【Distributed】分布式解决方案【汇总】
一.问题引出 二.分布式Session问题 三.网站跨域问题 四.分布式任务调度平台 五.分布式配置中心 六.分布式锁解决方案 七.缓存技术 一.问题引出 [Distributed]分布式系统中遇到的 ...
随机推荐
- Mac 10.12下安装python3环境
python3感觉用虚拟环境会比较好操作一些,也不用直接卸载python2. 一.基于brew快速安装 # 安装python3 brew install python3 # 安装pip(好像3自带pi ...
- OpenXC : Any updates on plans for IOS?
OpenXC : Any updates on plans for IOS? Hi Thomas, We're actively investigating this as we'd love to ...
- window Form中使用Font Awesome z
图标字体是矢量的,矢量图意味着每个图标都能在所有大小的屏幕上完美呈现,可以随时更改大小和颜色,而且不失真,真心给人一种“高大上”的感觉.由于Font Awesome是完全免费的,无论个人还是商业使用, ...
- Unity3D性能优化最佳实践(四)资源审查
Asset auditing - 资源审查 许多项目发生效能问题的真正原因只是由于人员操作不当或是试东试西,而不小心改到导入设定影响到导入的资源.(例如最近的gitlab惨案) 对于较大规模的项目,最 ...
- SpringBoot无废话入门02:SpringBoot启动分析
1.核心注解 在上文中,我们讲到了@SpringBootApplication是SpringBoot的核心注解. 可以很方便的在idea中下载源码来查看该注解的源码,如下: 可以看到,该注解本身又被其 ...
- Node.js模板引擎的深入探讨
每次当我想用 node.js 来写一个 web 相关项目的时候.我总是会陷入无比的纠结.原因是 JavaScript 生态圈里的模板引擎实在太多了,但那么多却实在找不出一个接近完美的,所谓完美的概念就 ...
- Java 基础【18】 反射与内省
1.概念定义 Java 反射机制(Reflect)容许程序在运行时加载.探知.使用编译期间完全未知的 class,核心类 java.lang.Class. 通过把指定类中各种元素映射成 java.la ...
- docker 端口映射 及外部无法访问问题
docker容器内提供服务并监听8888端口,要使外部能够访问,需要做端口映射. docker run -it --rm -p : server:v1 此时出现问题,在虚机A上部署后,在A内能够访问8 ...
- c/c++字节序转换(转)
字节序(byte order)关系到多字节整数(short/int16.int/int32,int64)和浮点数的各字节在内存中的存放顺序.字节序分为两种:小端字节序(little endian)和大 ...
- iostat各字段的来源和真实含义
The primary tool for inspecting Linux disk performance is iostat. The output includes many important ...