【问题标题】:Change PySpark StringIndexer input_col param when wrapped in a Pipeline object包装在 Pipeline 对象中时更改 PySpark StringIndexer input_col 参数
【发布时间】:2021-04-25 01:05:27
【问题描述】:

我正在构建一个Pipeline 对象来使用StringIndexer 对象对我的类别列进行编码。

indexers = [StringIndexer(inputCol='FirstName',
                                  outputCol='FirstName_new',
                                  handleInvalid='keep',
                                  stringOrderType='frequencyDesc').fit(df)]

pipeline = Pipeline(stages=indexers)

pipeline.write().overwrite().save(path)

我想在另一列上使用相同的管道对象(我有一个需要它的特定用例)。有什么办法可以更改input_col 参数?

【问题讨论】:

    标签: apache-spark pyspark apache-spark-mllib apache-spark-ml


    【解决方案1】:

    您可以使用setInputCol方法设置更改输入列名称。

    indexers = [StringIndexer(inputCol='FirstName',
                                      outputCol='FirstName_new',
                                      handleInvalid='keep',
                                      stringOrderType='frequencyDesc')]
    
    pipeline = Pipeline(stages=indexers)
    
    >>> print(pipeline.getStages()[0].getInputCol())
    FirstName
    
    pipeline.getStages()[0].setInputCol('test')
    
    >>> print(pipeline.getStages()[0].getInputCol())
    'test'
    

    请注意,您不应将 fit(df) 放入管道中 - 您应该使用管道适应数据,例如pipeline.fit(df).

    【讨论】:

    • 是的,这是单个阶段的正确用法。但是如果你使用的是管道,那么用法就不同了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2014-06-03
    • 2021-12-14
    • 2022-12-23
    • 1970-01-01
    相关资源
    最近更新 更多