Reputation: 19
trying to convert a RowMatrix into BDM (Breeze Dense Matrix), not sure how to proceed need to implement
def getDenseMatrix(A: RowMatrix): BDM[Double] = {
//write code here
}
additional questions:
How to access a particular Row in the RowMatrix?
for(i <- 0 to (RowM.numCols().toInt-1)){ //How to access RowM.rows(i) }
How to access a particular column in the RowMatrix?
for(i <- 0 to (RowM.numCols().toInt-1)){ //How to access RowM.rows.map(f=>f(i)) }
Upvotes: 2
Views: 770
Reputation: 3952
If you don't have foreachActive available use this:
def toBreeze(X: RowMatrix): BDM[Double]{
val m = X.numRows().toInt
val n = X.numCols().toInt
val mat = BDM.zeros[Double](m, n)
var i = 0
X.rows.collect().map{
case sp: SparseVector => (sp.indices, sp.values)
case dp: DenseVector => (Range(0,n).toArray, dp.values)
}.foreach {
case (indices, values) => indices.zip(values).foreach { case (j, v) =>mat(i, j) = v }
i += 1
}
mat
}
This is parallel up to a certain point and do the same.
Upvotes: 2
Reputation: 481
Read the source code in rowMatrix, you want the source code, it is in private method. The code is below:
def toBreeze(mat:RowMatrix):BDM[Double] = {
val m = mat.numRows()
val n = mat.numCols()
val result = BDM.zeros[Double](m.toInt,n.toInt)
var i = 0
mat.rows.collect().foreach{Vector =>
Vector.foreachActive { case(index,value) =>
result(i,index) = value
}
i+=1
}
result
}
Upvotes: 4