gpt4 book ai didi

python - 如何解释 Spark OneHotEncoder 的结果

转载 作者:太空狗 更新时间:2023-10-29 21:18:07 35 4
gpt4 key购买 nike

我从 Spark 文档中阅读了 OHE 条目,

One-hot encoding maps a column of label indices to a column of binary vectors, with at most a single one-value. This encoding allows algorithms which expect continuous features, such as Logistic Regression, to use categorical features.

但遗憾的是他们没有对 OHE 结果给出完整的解释。所以运行给定的代码:

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])|
+---+--------+-------------+-------------+

我如何解释 OHE(最后一列)的结果?

最佳答案

One-hot 编码将 categoryIndex 中的值转换为二进制向量。 (恰好有一个为1,其他为0)由于有3个值,向量长度为​​2,映射如下:

0  -> 10
1 -> 01
2 -> 00

(为什么映射是这样的?请参阅 this question 关于单热编码器删除最后一个类别。)

categoryVec 列中的值正是这些值,但以稀疏格式表示。在这种格式中,不打印向量的零点。第一个值 (2) 显示向量的长度,第二个值是一个数组,其中列出了零个或多个找到非零条目的索引。第三个值是另一个数组,它告诉我们在这些索引处找到了哪些数字。所以 (2,[0],[1.0]) 表示长度为 2 的向量,其中 1.0 在位置 0 和 0 其他地方。

参见:https://spark.apache.org/docs/latest/mllib-data-types.html#local-vector

关于python - 如何解释 Spark OneHotEncoder 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42295001/

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