Eli*_*hle 0 apache-spark pyspark apache-spark-mllib one-hot-encoding
星火文档包含一个PySpark例如其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()
Run Code Online (Sandbox Code Playgroud)
我希望该列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]
Run Code Online (Sandbox Code Playgroud)
但categoryVec实际上看起来是这样的:
(2, [0], [1.0])
(2, [], [])
(2, [1], [1.0])
(2, [0], [1.0])
(2, [0], [1.0])
(2, [1], [1.0])
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我应该如何阅读这个输出,这种有点奇怪的格式背后的原因是什么?
| 归档时间: |
|
| 查看次数: |
491 次 |
| 最近记录: |