在星火MLlib矩阵运算在Java中 [英] Matrix Operation in Spark MLlib in Java

查看:690
本文介绍了在星火MLlib矩阵运算在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是关于MLlib(星火1.2.1 +)。

什么是为操作本地矩阵的最佳方法(大小适中,100×100下,所以不需要分发)。

例如,计算数据集的SVD后,我需要执行一些矩阵运算。
RowMatrix 只提供一个乘法功能。该toBreeze方法返回一个 DenseMatrix<对象> 但是API似乎并不友好的Java:
公共决赛< TT,B,即>这$加(B B,UFunc.UImpl2< OpAdd $,TT,B,即> OP)

在星火+ Java中,如何做到以下任何操作:


  • 矩阵转置

  • 加/减两个矩阵

  • 作物矩阵

  • 执行元素方式的运算


的Javadoc RowMatrix:<一href=\"https://spark.apache.org/docs/latest/api/java/org/apache/spark/mllib/linalg/distributed/RowMatrix.html\" rel=\"nofollow\">https://spark.apache.org/docs/latest/api/java/org/apache/spark/mllib/linalg/distributed/RowMatrix.html

  RDD&LT;向量&GT;数据= ...;
RowMatrix矩阵=新RowMatrix(数据);
SingularValueDecomposition&LT; RowMatrix,矩阵和GT; SVD = matrix.computeSVD(15,真正的,1E-9D);RowMatrix U = svd.U();
向量s = svd.s();
矩阵V = svd.V();
//示例1:如何计算转置(U)*矩阵
//示例2:如何计算转置(U(:,1:K))*矩阵

修改:感谢dlwh指着我在正确的方向,下面的解决方案如下:

 进口no.uib.cipr.matrix.DenseMatrix;
// ...
RowMatrix U = svd.U();
DenseMatrix U_mtj =新DenseMatrix((INT)U.numCols(),(INT)U.numRows(),U.toBreeze()$的toArray MCD $ SP(),真);
//从那里,矩阵运算可在U_mtj


解决方案

微风只是不提供Java友好的API。 (而且,讲作为主要作者,我没有计划:它会加害的API太多了。)

您也许可以利用这一点MTJ使用相同的密集矩阵重新presentation,因为我们做的事实。 (嗯,差不多了。他们的API不公开majorStride,但不应该成为你的问题。)

也就是说,你可以做这样的事情:

 进口no.uib.cipr.matrix.DenseMatrix;// ...breeze.linalg.DenseMatrix [双] Ubreeze = U.toBreeze();
新DenseMatrix(Ubreeze.cols(),Ubreeze.rows(),Ubreeze.data());

This question is about MLlib (Spark 1.2.1+).

What is the best way to manipulate local matrices (moderate size, under 100x100, so does not need to be distributed).

For instance, after computing the SVD of a dataset, I need to perform some matrix operation. The RowMatrix only provide a multiply function. The toBreeze method returns a DenseMatrix<Object> but the API does not seem Java friendly: public final <TT,B,That> That $plus(B b, UFunc.UImpl2<OpAdd$,TT,B,That> op)

In Spark+Java, how to do any of the following operations:

  • transpose a matrix
  • add/subtract two matrices
  • crop a Matrix
  • perform element-wise operations
  • etc

Javadoc RowMatrix: https://spark.apache.org/docs/latest/api/java/org/apache/spark/mllib/linalg/distributed/RowMatrix.html

RDD<Vector> data = ...;
RowMatrix matrix = new RowMatrix(data);
SingularValueDecomposition<RowMatrix, Matrix> svd = matrix.computeSVD(15, true, 1e-9d);

RowMatrix U = svd.U();
Vector s = svd.s();
Matrix V = svd.V();
//Example 1: How to compute transpose(U)*matrix
//Example 2: How to compute transpose(U(:,1:k))*matrix

EDIT: Thanks for dlwh for pointing me in the right direction, the following solution works:

import no.uib.cipr.matrix.DenseMatrix;
// ...
RowMatrix U = svd.U();
DenseMatrix U_mtj = new DenseMatrix((int) U.numCols(), (int) U.numRows(), U.toBreeze().toArray$mcD$sp(), true);
// From there, matrix operations are available on U_mtj

解决方案

Breeze just doesn't provide a Java-friendly API. (And, speaking as the main author, I have no plans to: it would hamstring the API too much.)

You can probably exploit the fact that MTJ uses the same dense matrix representation as we do. (Well, almost. Their API doesn't expose majorStride, but that shouldn't be an issue for you.)

That is, you can do something like this:

import no.uib.cipr.matrix.DenseMatrix;

// ...

breeze.linalg.DenseMatrix[Double] Ubreeze = U.toBreeze();
new DenseMatrix(Ubreeze.cols(), Ubreeze.rows(), Ubreeze.data());

这篇关于在星火MLlib矩阵运算在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆