How can I find the index of the maximum values alo

2019-08-01 10:22发布

问题:

I have a question about finding index of the maximum values along rows of matrix. How can I do this in Spark Scala? This function would be like argmax in numpy in Python.

回答1:

What's the type of your matrix ? If it's a RowMatrix, you can access the RDD of its row vectors using rows.

Then it's a simple matter of finding the maximum of each vector of this RDD[Vector], if I understand correctly. You can therefore myMatrix.rows.map{_.toArray.max}.

If you have a DenseMatrix you can convert it to an Array, at which stage you'll have a list of elements in row-major form. You can also access the number of columns of your matrix with numCols, and then use the collections method grouped to obtain rows.

myMatrix.toArray.grouped(myMatrix.numCols).map{_.max}


回答2:

I think you will have to get the values as an array to get the maximum value.

    val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))

    val result = dm.toArray.max

    println(result)