我可以在spark ML Pipelines中设置阶段名称吗?

时间:2018-03-19 10:22:42

标签: apache-spark pyspark pipeline

我开始创建更复杂的ML管道并多次使用相同类型的管道阶段。有没有办法设置阶段的名称,以便其他人可以轻松查询已保存的管道并找出正在发生的事情?例如

vecAssembler1 = VectorAssembler(inputCols = ["P1", "P2"], outputCol="features1")
vecAssembler2 = VectorAssembler(inputCols = ["P3", "P4"], outputCol="features2")
lr_1 = LogisticRegression(labelCol = "L1")
lr_2 = LogisticRegression(labelCol = "L2")
pipeline = Pipeline(stages=[vecAssembler1, vecAssembler2, lr_1, lr_2])
print pipeline.stages

这将返回如下内容:

[VectorAssembler_4205a9d090177e9c54ba, VectorAssembler_42b8aa29277b380a8513, LogisticRegression_42d78f81ae072747f88d, LogisticRegression_4d4dae2729edc37dc1f3]

但我想要做的事情是:

pipeline = Pipeline(stages=[vecAssembler1, vecAssembler2, lr_1, lr_2], names=["VectorAssembler for predicting L1","VectorAssembler for predicting L1","LogisticRegression for L1","LogisticRegression for L2")

这样一个保存的管道模型可以由第三方加载,它们将得到很好的描述:

print pipeline.stages
# [VectorAssembler for predicting L1,VectorAssembler for predicting L2,LogisticRegression for L1,LogisticRegression for L2]

1 个答案:

答案 0 :(得分:1)

您可以使用_resetUid方法重命名每个转换器/估算器:

vecAssembler1 = VectorAssembler(inputCols = ["P1", "P2"], outputCol="features1")
vecAssembler1._resetUid("VectorAssembler for predicting L1")

默认情况下,它使用java的UID随机生成器。