我有一个关于沿矩阵的行寻找最大价值的指标问题。 我怎样才能做到这一点星火Scala呢? 此功能会像Python中numpy的argmax。
Answer 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}
Answer 2:
我认为你将获得的值作为一个数组,以获得最大的价值。
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)
文章来源: How can I find the index of the maximum values along rows of matrix Spark Scala?