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)