私は、次のような構造を持つスパークデータフレームを持っている:ベクトル列の最大値のインデックスを見つける方法は?
root
|-- distribution: vector (nullable = true)
+--------------------+
| topicDistribution|
+--------------------+
| [0.1, 0.2] |
| [0.3, 0.2] |
| [0.5, 0.2] |
| [0.1, 0.7] |
| [0.1, 0.8] |
| [0.1, 0.9] |
+--------------------+
私の質問は:各行の最大値のインデックスを持つ列を追加する方法は?
それはこのようなものでなければなりません:
root
|-- distribution: vector (nullable = true)
|-- max_index: integer (nullable = true)
+--------------------+-----------+
| topicDistribution| max_index |
+--------------------+-----------+
| [0.1, 0.2] | 1 |
| [0.3, 0.2] | 0 |
| [0.5, 0.2] | 0 |
| [0.1, 0.7] | 1 |
| [0.1, 0.8] | 1 |
| [0.1, 0.9] | 1 |
+--------------------+-----------+
どうもありがとう
- - - - 更新 - - - - - :
私は、次の方法を試してみましたが、私は得ましたエラー:
import org.apache.spark.sql.functions.udf
val func = udf((x: Vector[Double]) => x.indices.maxBy(x))
df.withColumn("max_idx",func(($"topicDistribution"))).show()
エラー:
Exception in thread "main" org.apache.spark.sql.AnalysisException:
cannot resolve 'UDF(topicDistribution)' due to data type mismatch:
argument 1 requires array<double> type, however, '`topicDistribution`'
is of vector type.;;
ありがとうございます。ただし、データ型の不一致によりUDF(トピック)を解決できません。引数1には配列型が必要ですが、 '' topics' 'はベクトル型です。 –
Ippon