【发布时间】:2019-01-14 05:29:53
【问题描述】:
我试图检查 pyspark 中 OneHotEncoder 的输出。我在编码器的论坛和文档中读到,编码向量的大小将等于正在编码的列中不同值的数量。
from pyspark.ml.feature import OneHotEncoder, StringIndexer
df = sqlContext.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()
以下是上面代码的结果
+---+--------+--------------+-------------+
| id|category|categoryIndex| categoryVec|
+---+--------+--------------+-------------+
| 0| a| 0.0|(2,[0],[1.0])|
| 1| b| 2.0| (2,[],[])|
| 2| c| 1.0|(2,[1],[1.0])|
| 3| a| 0.0|(2,[0],[1.0])|
| 4| a| 0.0|(2,[0],[1.0])|
| 5| c| 1.0|(2,[1],[1.0])|
+---+--------+--------------+-------------+
根据 categoryVec 列的解释,向量的大小为 2。而“类别”列中不同值的数量为 3,即 a、b 和 c。请让我了解我在这里缺少什么。
【问题讨论】:
标签: python pyspark apache-spark-ml one-hot-encoding