'OneHotEncoder' 对象没有属性 'transform'

'OneHotEncoder' object has no attribute 'transform'

我正在使用 Spark v3.0.0。我的数据框是:

indexer.show()
+------+--------+-----+
|row_id|    city|index|
+------+--------+-----+
|     0|New York|  0.0|
|     1|  Moscow|  3.0|
|     2| Beijing|  1.0|
|     3|New York|  0.0|
|     4|   Paris|  2.0|
|     5|   Paris|  2.0|
|     6|New York|  0.0|
|     7| Beijing|  1.0|
+------+--------+-----+

然后我想对数据帧的列“索引”使用一种热编码并得到这个错误。

encoder = OneHotEncoder(inputCol="index", outputCol="encoding")
encoder.setDropLast(False)
indexer = encoder.transform(indexer)
----------------------------------------
AttributeErrorTraceback (most recent call last)
<ipython-input-32-70bbd67e6679> in <module>
      1 encoder = OneHotEncoder(inputCol="index", outputCol="encoding")
      2 encoder.setDropLast(False)
----> 3 indexer = encoder.transform(indexer)

AttributeError: 'OneHotEncoder' object has no attribute 'transform'

需要先拟合-拟合之前,该属性确实不存在:

encoder = OneHotEncoder(inputCol="index", outputCol="encoding")
encoder.setDropLast(False)
ohe = encoder.fit(indexer) # indexer is the existing dataframe, see the question
indexer = ohe.transform(indexer)

有关用法的更多详细信息,请参阅文档中的 example