Matrix Transpose on RowMatrix in Spark

前端 未结 6 1202
梦谈多话
梦谈多话 2020-12-16 15:20

Suppose I have a RowMatrix.

  1. How can I transpose it. The API documentation does not seem to have a transpose method.
  2. The Matrix has the transpose() me
6条回答
  •  旧巷少年郎
    2020-12-16 16:11

    This is a variant of the previous solution but working for sparse row matrix and keeping the transposed sparse too when needed:

      def transpose(X: RowMatrix): RowMatrix = {
        val m = X.numRows ().toInt
        val n = X.numCols ().toInt
    
        val transposed = X.rows.zipWithIndex.flatMap {
          case (sp: SparseVector, i: Long) => sp.indices.zip (sp.values).map {case (j, value) => (i, j, value)}
          case (dp: DenseVector, i: Long) => Range (0, n).toArray.zip (dp.values).map {case (j, value) => (i, j, value)}
        }.sortBy (t => t._1).groupBy (t => t._2).map {case (i, g) =>
          val (indices, values) = g.map {case (i, j, value) => (i.toInt, value)}.unzip
          if (indices.size == m) {
            (i, Vectors.dense (values.toArray) )
          } else {
            (i, Vectors.sparse (m, indices.toArray, values.toArray))
          }
        }.sortBy(t => t._1).map (t => t._2)
    
        new RowMatrix (transposed)
      }
    

    Hope this help!

提交回复
热议问题