【发布时间】:2020-07-23 17:59:54
【问题描述】:
我的模型输出了一个 DenseVector 列,我想找到 argmax。 This page 建议这个函数应该可用,但我不确定语法应该是什么。
是df.select("mycolumn").argmax()吗?
【问题讨论】:
标签: apache-spark pyspark apache-spark-ml
我的模型输出了一个 DenseVector 列,我想找到 argmax。 This page 建议这个函数应该可用,但我不确定语法应该是什么。
是df.select("mycolumn").argmax()吗?
【问题讨论】:
标签: apache-spark pyspark apache-spark-ml
我在 python 中找不到 argmax 操作的文档。但是您可以通过将它们转换为数组来完成它们
对于 pyspark 3.0.0
from pyspark.ml.functions import vector_to_array
tst_arr = tst_df.withColumn("arr",vector_to_array(F.col('vector_column')))
tst_max=tst_arr.withColumn("max_value",F.array_max("arr"))
tst_max_exp = tst_max.select('*',F.posexplode("arr"))
tst_fin = tst_max_exp.where('col==max_value')
对于 pyspark
from pyspark.sql.functions import udf
@udf
def vect_argmax(row):
row_arr = row.toArray()
max_pos = np.argmax(row_arr)
return(int(max_pos))
tst_fin = tst_df.withColumn("argmax",vect_argmax(F.col('probability')))
【讨论】:
你试过了吗
from pyspark.sql.functions import col
df.select(col("mycolumn").argmax())
【讨论】: