如何沿着矩阵Spark Scala的行找到最大值的索引? [英] How can I find the index of the maximum values along rows of matrix Spark Scala?
问题描述
我有一个问题,想找到沿着矩阵行的最大值的索引.如何在Spark Scala中做到这一点?此功能类似于Python中numpy中的argmax.
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.
推荐答案
矩阵的类型是什么?如果它是RowMatrix
,则可以使用rows
访问其行向量的RDD
.
What's the type of your matrix ? If it's a RowMatrix
, you can access the RDD
of its row vectors using rows
.
然后,如果我理解正确,那么找到此RDD[Vector]
的每个向量的最大值就很简单.因此,您可以myMatrix.rows.map{_.toArray.max}
.
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}
.
如果有DenseMatrix
,则可以将其转换为Array
,在此阶段,您将在
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}
这篇关于如何沿着矩阵Spark Scala的行找到最大值的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!