Skip to main content
 首页 » 编程设计

python之Spark 矩阵的基本线性代数

2024年08月25日36TianFang

我正尝试在存储为 spark RowMatrix 的矩阵上运行一些基本的线性代数运算(特别是转置、点积和逆),如此处所述 here (使用 Python API)。按照文档中的示例(对于我的情况,矩阵中会有更多行,因此需要 Spark),假设我有这样的东西:

from pyspark.mllib.linalg.distributed import RowMatrix 
# Create an RDD of vectors. 
rows = sc.parallelize([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) 
# Create a RowMatrix from an RDD of vectors. 
mat = RowMatrix(rows) 

给定这样一个分布式矩阵,是否存在进行矩阵转置和点积的现有例程,例如:

dot(mat.T,mat) 

还是逆矩阵?

inverse(mat) 

我似乎无法在文档中找到任何关于此的内容。寻找 (a) 指向相关文档的指针或 (b) 自己实现它的方法。

请您参考如下方法:

至于现在(Spark 1.6.0)pyspark.mllib.linalg.distributed API 仅限于基本操作,例如计算行/列和类型之间的转换。

Scala API 支持更广泛的方法集,包括乘法(RowMatrix.multiply、Indexed.RowMatrix.multiply)、转置、SVD(IndexedRowMatrix.computeSVD), QR 分解 (RowMatrix.tallSkinnyQR), Grammian 矩阵计算 (computeGramianMatrix), PCA (RowMatrix.computePrincipalComponents) 这可以是用于实现更复杂的线性代数函数。