如何将 RowMatrix 转换为本地矩阵?
How to convert RowMatrix to local Matrix?
我对 Spark 中的矩阵有疑问。
假设我有一个名为 X 的 RowMatrix
,如下所示:
0.5 0.5
0.25 0.0625
0.125 0.125
0.0625 0.0625
0.0625 0.25
现在我要做的是将此 RowMatrix
与 RowMatrix
X 的转置版本相乘。
0.5 0.25 0.125 0.0625 0.0625
0.5 0.0625 0.125 0.0625 0.25
现在,据我所知,我不能将 RowMatrix
与另一个 RowMatrix
相乘,它必须是 RowMatrix
和局部矩阵。因此,我尝试使用以下代码将 RowMatrix
转换为局部密集矩阵:
val arr = X.rows.map(x=>x.toArray).collect.flatten
val Xlocal = Matrices.dense(X.numRows.toInt,X.numCols.toInt,arr)
但它没有正确转换,因为我认为 RowMatrix
是基于行的?我不太确定,局部密集矩阵是以列优先顺序存储的,所以顺序很乱。
谁能帮我实现这个?
A RowMatrix
do not have any row indices and should only be used when the row order does not matter. If the order does matter use an IndexedRowMatrix
代替。
可以将 RowMatrix
转换为 IndexedRowMatrix
,但请注意 顺序无法保证 ,最好使用 IndexedRowMatrix
直接地。假设 rowMat
是要转换的矩阵:
val indRowMax = new IndexedRowMatrix(rowMat.rows.zipWithIndex().map{ case (v, id) => IndexedRow(id, v)})
一个IndexedRowMatrix
可以很容易地转换为局部矩阵:
val localMat = indRowMax.toBlockMatrix().toLocalMatrix()
并乘以转置可以按如下方式完成:
indRowMax.multiply(localMat.transpose)
我对 Spark 中的矩阵有疑问。
假设我有一个名为 X 的 RowMatrix
,如下所示:
0.5 0.5
0.25 0.0625
0.125 0.125
0.0625 0.0625
0.0625 0.25
现在我要做的是将此 RowMatrix
与 RowMatrix
X 的转置版本相乘。
0.5 0.25 0.125 0.0625 0.0625
0.5 0.0625 0.125 0.0625 0.25
现在,据我所知,我不能将 RowMatrix
与另一个 RowMatrix
相乘,它必须是 RowMatrix
和局部矩阵。因此,我尝试使用以下代码将 RowMatrix
转换为局部密集矩阵:
val arr = X.rows.map(x=>x.toArray).collect.flatten
val Xlocal = Matrices.dense(X.numRows.toInt,X.numCols.toInt,arr)
但它没有正确转换,因为我认为 RowMatrix
是基于行的?我不太确定,局部密集矩阵是以列优先顺序存储的,所以顺序很乱。
谁能帮我实现这个?
A RowMatrix
do not have any row indices and should only be used when the row order does not matter. If the order does matter use an IndexedRowMatrix
代替。
可以将 RowMatrix
转换为 IndexedRowMatrix
,但请注意 顺序无法保证 ,最好使用 IndexedRowMatrix
直接地。假设 rowMat
是要转换的矩阵:
val indRowMax = new IndexedRowMatrix(rowMat.rows.zipWithIndex().map{ case (v, id) => IndexedRow(id, v)})
一个IndexedRowMatrix
可以很容易地转换为局部矩阵:
val localMat = indRowMax.toBlockMatrix().toLocalMatrix()
并乘以转置可以按如下方式完成:
indRowMax.multiply(localMat.transpose)