gpt4 book ai didi

python - 使用来自 StringIndexer 的标签进行 IndexToString 转换

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:09 24 4
gpt4 key购买 nike

如何通过从 labelIndexer 获取标签,使用 IndexToString 进行转换?

labelIndexer = StringIndexer(inputCol="shutdown_reason", outputCol="label")

idx_to_string = IndexToString(inputCol="prediction", outputCol="predictedValue")

最佳答案

How can I convert using IndexToString by taking the labels from labelIndexer?

你不能。 labelIndexer 是一个 StringIndexer,要获取标签,您需要 StringIndexerModel拟合模型:

from pyspark.ml.feature import *

df = spark.createDataFrame([
("foo", ), ("bar", )
]).toDF("shutdown_reason")

labelIndexerModel = labelIndexer.fit(df)

使用标签:

idx_to_string.setLabels(labelIndexerModel.labels)
idx_to_string.getLabels()
# ['foo', 'bar']

转换:

df_with_prediction = labelIndexerModel.transform(df).withColumnRenamed(
"label", "prediction"
)

idx_to_string.transform(df_with_prediction).show()
# +---------------+----------+--------------+
# |shutdown_reason|prediction|predictedValue|
# +---------------+----------+--------------+
# | foo| 0.0| foo|
# | bar| 1.0| bar|
# +---------------+----------+--------------+

关于python - 使用来自 StringIndexer 的标签进行 IndexToString 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48093364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com