如何通过从 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|
# +---------------+----------+--------------+
我是一名优秀的程序员,十分优秀!