【发布时间】:2018-09-25 22:47:47
【问题描述】:
我正在尝试使用 Python 在 Spark 中创建一个“功能”列,以供机器学习库使用。但是,我在生成“特征”列的 VectorAssembler 中遇到了包括数值变量和分类变量在内的问题。
cat_cols = ["cat_1", "cat_2", "cat_3"]
num_cols = ["num_1", "num_2", "num_3", "num_4"]
indexers = [StringIndexer(inputCol = c, outputCol="{0}_indexed".format(c)) for c in cat_cols]
encoders = [StringIndexer(inputCol = indexer.getOutputCol(), outputCol = "{0}_encoded".format(indexer.getOutputCol()))
for indexer in indexers]
assembler = VectorAssembler(inputCols = [encoder.getOutputCol() for encoder in encoders], outputCol = "features")
pipeline = Pipeline(stages = indexers + encoders + [assembler])
df = pipeline.fit(df).transform(df)
到目前为止构建的管道可以创建一个仅包含分类变量的“特征”列,但我不知道如何扩展它以使“特征”列同时包含分类变量和数值变量。
请注意,我将 Spark 2.3 与 Python 3 一起使用。
【问题讨论】:
标签: python apache-spark pyspark