【发布时间】:2018-09-12 22:37:21
【问题描述】:
Spark 文档包含 a PySpark example 的 OneHotEncoder:
from pyspark.ml.feature import OneHotEncoder, StringIndexer
df = spark.createDataFrame([
(0, "a"),
(1, "b"),
(2, "c"),
(3, "a"),
(4, "a"),
(5, "c")
], ["id", "category"])
stringIndexer = StringIndexer(inputCol="category", outputCol="categoryIndex")
model = stringIndexer.fit(df)
indexed = model.transform(df)
encoder = OneHotEncoder(inputCol="categoryIndex", outputCol="categoryVec")
encoded = encoder.transform(indexed)
encoded.show()
我希望列 categoryVec 看起来像这样:
[0.0, 0.0]
[1.0, 0.0]
[0.0, 1.0]
[0.0, 0.0]
[0.0, 0.0]
[0.0, 1.0]
但categoryVec实际上是这样的:
(2, [0], [1.0])
(2, [], [])
(2, [1], [1.0])
(2, [0], [1.0])
(2, [0], [1.0])
(2, [1], [1.0])
这是什么意思?我应该如何阅读这个输出,这种有点奇怪的格式背后的原因是什么?
【问题讨论】:
标签: apache-spark pyspark apache-spark-mllib one-hot-encoding