Data Types

来源:互联网 发布:数据统计与分析 编辑:程序博客网 时间:2024/05/08 13:10

Distributed matrix

分布式矩阵具有long类型的行和列索引和double类型值,分布式存储在一个或多个RDD中。选择正确的格式来存储大型和分布式矩阵是非常重要的。将分布式矩阵转换为不同的格式可能需要全局shuffle,代价非常大的。到目前为止,已经实现了三种类型的分布式矩阵。

Distributed matrix 的基本类型是RowMatrix。A RowMatrix是没有有意义的行索引的行向分布式矩阵,例如特征向量的集合。它由其行的RDD支持,其中每行是局部向量。

我们假设RowMatrix列的数量不是巨大的,因此单个局部向量可以合理地传递给驱动器,并且也可以使用单个节点来存储/操作。
An IndexedRowMatrix类似于RowMatrix但是有行索引索引,可用于标识行和执行连接。A CoordinateMatrix是以坐标列表(COO)格式存储的分布式矩阵,由其条目的RDD支持。

注意

分布式矩阵的底层RDD必须是确定性的,因为我们缓存矩阵大小。一般来说,使用非确定性RDD可能会导致错误。

RowMatrix

A RowMatrix是没有有意义的行索引的行导向分布式矩阵,由行的RDD支持,其中每行是局部向量。由于每行由局部向量表示,所以列数受到整数范围的限制,但实际应该要小得多。

Scala

A RowMatrix可以从RDD[Vector]实例创建。然后我们可以计算其列汇总统计和分解。 QR分解形式为A = QR,其中Q是正交矩阵,R是上三角矩阵。QR分解法是三种将矩阵分解的方式之一。这种方式,把矩阵分解成一个正交矩阵与一个上三角矩阵的积

import org.apache.spark.mllib.linalg.Vectorimport org.apache.spark.mllib.linalg.distributed.RowMatrix    val v0 = Vectors.dense(1.0, 0.0, 3.0)    val v1 = Vectors.sparse(3, Array(1), Array(2.5))    val v2 = Vectors.sparse(3, Seq((0, 1.5), (1, 1.8)))    val rows = sc.parallelize(Seq(v0, v1, v2))    val rowmatrix = new RowMatrix(rows);    println("-----------------------")    println(rowmatrix.numCols())    println("-----------------------")    println(rowmatrix.numRows())    println("-----------------------")    println(rowmatrix.tallSkinnyQR(true))

IndexedRowMatrix

An IndexedRowMatrix类似于RowMatrix但具有有意义的行索引。它由索引行的RDD支持,因此每行都由其索引(long 类型)和局部向量表示。

Scala

一个 IndexedRowMatrix 可以从一个RDD[IndexedRow]实例创建,其中 IndexedRow是一个包装(Long, Vector)。一个IndexedRowMatrix可以被转换为RowMatrix通过降低其行索引。

import org.apache.spark.mllib.linalg.distributed.{IndexedRow, IndexedRowMatrix, RowMatrix}val rows: RDD[IndexedRow] = ... // an RDD of indexed rows// Create an IndexedRowMatrix from an RDD[IndexedRow].val mat: IndexedRowMatrix = new IndexedRowMatrix(rows)// Get its size.val m = mat.numRows()val n = mat.numCols()// Drop its row indices.val rowMat: RowMatrix = mat.toRowMatrix()

CoordinateMatrix

A CoordinateMatrix是由其条目的RDD支持的分布式矩阵。每个条目是一个元组(i: Long, j: Long, value: Double),i行索引在哪里是j列索引,并且 value是条目值。一个CoordinateMatrix只有当矩阵的两个维度是巨大的,矩阵是非常稀疏应该被使用。

Scala

A CoordinateMatrix 可以从RDD[MatrixEntry]实例创建,其中 MatrixEntry是包装(Long, Long, Double)。A CoordinateMatrix可以IndexedRowMatrix 通过调用转换为稀疏行toIndexedRowMatrix。CoordinateMatrix目前不支持其他 计算。

import org.apache.spark.mllib.linalg.distributed.{CoordinateMatrix, MatrixEntry}val entries: RDD[MatrixEntry] = ... // an RDD of matrix entries// Create a CoordinateMatrix from an RDD[MatrixEntry].val mat: CoordinateMatrix = new CoordinateMatrix(entries)// Get its size.val m = mat.numRows()val n = mat.numCols()// Convert it to an IndexRowMatrix whose rows are sparse vectors.val indexedRowMatrix = mat.toIndexedRowMatrix()

BlockMatrix

A BlockMatrix是由MatrixBlocks 的RDD支持的分布式矩阵,其中a MatrixBlock是元组((Int, Int), Matrix),其中(Int, Int)是块的索引,并且Matrix是具有大小rowsPerBlockx 的给定索引处的子矩阵colsPerBlock。 BlockMatrix支持add和multiply另一种方法BlockMatrix。 BlockMatrix还有一个帮助功能validate,可以用来检查是否 BlockMatrix正确设置。

Scala

A BlockMatrix可以通过IndexedRowMatrix或CoordinateMatrix通过调用最容易地创建toBlockMatrix。 toBlockMatrix默认情况下创建大小为1024 x 1024的块。用户可以通过提供值来更改块大小toBlockMatrix(rowsPerBlock, colsPerBlock)。

import org.apache.spark.mllib.linalg.distributed.{BlockMatrix, CoordinateMatrix, MatrixEntry}val entries: RDD[MatrixEntry] = ... // an RDD of (i, j, v) matrix entries// Create a CoordinateMatrix from an RDD[MatrixEntry].val coordMat: CoordinateMatrix = new CoordinateMatrix(entries)// Transform the CoordinateMatrix to a BlockMatrixval matA: BlockMatrix = coordMat.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)
0 0
原创粉丝点击