火花 RowMatrix 的逆
Inverse of a spark RowMatrix
我正在尝试反转火花行矩阵。我正在使用的功能如下。
def computeInverse(matrix: RowMatrix): BlockMatrix = {
val numCoefficients = matrix.numCols.toInt
val svd = matrix.computeSVD(numCoefficients, computeU = true)
val indexed_U = new IndexedRowMatrix(svd.U.rows.zipWithIndex.map(r => new IndexedRow(r._2, r._1)))
val invS = DenseMatrix.diag(new DenseVector(svd.s.toArray.map(x => if(x == 0) 0 else math.pow(x,-1))))
val V_inv = svd.V.multiply(invS)
val inverse = indexed_U.multiply(V_inv.transpose)
inverse.toBlockMatrix.transpose
}
我实现的逻辑是通过SVD。该过程的解释是
U, Σ, V = svd(A)
A = U * Σ * V.transpose
A.inverse = (U * Σ * V.transpose).inverse
= (V.transpose).inverse * Σ.inverse * U.inverse
Now U and V are orthogonal matrix
Therefore,
M * M.transpose = 1
Applying the above,
A.inverse = V * Σ.inverse * U.transpose
Let V * Σ.inverse be X
A.inverse = X * U.transpose
Now, A * B = ((A * B).transpose).transpose
= (B.transpose * A.transpose).transpose
Applying the same, to keep U as a row matrix, not a local matrix
A.inverse = X * U.transpose
= (U.transpose.transpose * X.transpose).transpose
= (U * X.transpose).transpose
输入行矩阵有问题。例如
1, 2, 3
4, 5, 6
7, 8, 9
10,11,12
上面代码片段的逆和使用 python numpy 是不同的。我无法找出为什么会这样?是因为在 svd 计算过程中做了一些潜在的假设吗?任何帮助将不胜感激。谢谢
以上代码运行正常。我收到此错误的原因是我使用 RDD[Vector] 创建了 RowMatrix。现在,在 spark 中,事物按列排序以形成矩阵,而在 numpy 的情况下,数组按行转换为矩阵
Array(1,2,3,4,5,6,7,8,9)
在火花中
1 4 7
2 5 8
3 6 9
在python中解释为
1 2 3
4 5 6
7 8 9
所以,测试用例失败了:|
我正在尝试反转火花行矩阵。我正在使用的功能如下。
def computeInverse(matrix: RowMatrix): BlockMatrix = {
val numCoefficients = matrix.numCols.toInt
val svd = matrix.computeSVD(numCoefficients, computeU = true)
val indexed_U = new IndexedRowMatrix(svd.U.rows.zipWithIndex.map(r => new IndexedRow(r._2, r._1)))
val invS = DenseMatrix.diag(new DenseVector(svd.s.toArray.map(x => if(x == 0) 0 else math.pow(x,-1))))
val V_inv = svd.V.multiply(invS)
val inverse = indexed_U.multiply(V_inv.transpose)
inverse.toBlockMatrix.transpose
}
我实现的逻辑是通过SVD。该过程的解释是
U, Σ, V = svd(A)
A = U * Σ * V.transpose
A.inverse = (U * Σ * V.transpose).inverse
= (V.transpose).inverse * Σ.inverse * U.inverse
Now U and V are orthogonal matrix
Therefore,
M * M.transpose = 1
Applying the above,
A.inverse = V * Σ.inverse * U.transpose
Let V * Σ.inverse be X
A.inverse = X * U.transpose
Now, A * B = ((A * B).transpose).transpose
= (B.transpose * A.transpose).transpose
Applying the same, to keep U as a row matrix, not a local matrix
A.inverse = X * U.transpose
= (U.transpose.transpose * X.transpose).transpose
= (U * X.transpose).transpose
输入行矩阵有问题。例如
1, 2, 3
4, 5, 6
7, 8, 9
10,11,12
上面代码片段的逆和使用 python numpy 是不同的。我无法找出为什么会这样?是因为在 svd 计算过程中做了一些潜在的假设吗?任何帮助将不胜感激。谢谢
以上代码运行正常。我收到此错误的原因是我使用 RDD[Vector] 创建了 RowMatrix。现在,在 spark 中,事物按列排序以形成矩阵,而在 numpy 的情况下,数组按行转换为矩阵
Array(1,2,3,4,5,6,7,8,9)
在火花中
1 4 7
2 5 8
3 6 9
在python中解释为
1 2 3
4 5 6
7 8 9
所以,测试用例失败了:|