【问题标题】:Retrieving transformed dataset from pipeline object in pyspark.ml.pipeline从 pyspark.ml.pipeline 中的管道对象中检索转换后的数据集
【发布时间】:2018-04-11 23:30:45
【问题描述】:

我是 Spark ML 的新手。我正在尝试使用 Spark ML Pipelines 来链接数据转换(将其视为 ETL 过程)。也就是说,我想输入一个DataFrame,做一系列的转换(每次给这个dataframe加一列),然后输出转换后的DataFrame。

我正在查看 Python 中 Pipelines 的文档和代码,但我不知道如何将转换后的数据集从 Pipeline 中取出。请参阅以下示例(从文档中复制并修改):

from pyspark.ml import Pipeline
from pyspark.ml.feature import HashingTF, Tokenizer

# Prepare training documents from a list of (id, text, label) tuples.
training = spark.createDataFrame([
   (0, "a b c d e spark", 1.0),
   (1, "b d", 0.0),
   (2, "spark f g h", 1.0),
   (3, "hadoop mapreduce", 0.0)
    ], ["id", "text", "label"])

 # Configure an ML pipeline, which consists of two stages: tokenizer, 
 hashingTF.
 tokenizer = Tokenizer(inputCol="text", outputCol="words")
 hashingTF = HashingTF(inputCol=tokenizer.getOutputCol(), 
 outputCol="features")
 pipeline = Pipeline(stages=[tokenizer, hashingTF])

 training.show()
 pipeline.fit(training)

如何从“管道”对象中获取转换后的数据集(即执行了分词器和散列后的数据集)?

【问题讨论】:

    标签: python apache-spark pyspark spark-dataframe pipeline


    【解决方案1】:

    你不能。而是保留模型

    model = pipeline.fit(training)
    

    并将其用于transform数据:

    training_transformed = model.transform(training)
    

    【讨论】:

    • 非常感谢,这正是我想要的!
    猜你喜欢
    • 2018-12-31
    • 2021-12-30
    • 2016-09-28
    • 2018-08-06
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多