我们可以在pyspark的ParamGridBuilder中使用for循环吗?

时间:2018-09-11 13:21:11

标签: python pyspark apache-spark-mllib

下面的代码是将参数添加到paramGridBuilder中,而pyspark中没有任何循环。

from pyspark.ml.tuning import ParamGridBuilder
paramGrid = ParamGridBuilder()\
        .addGrid(lr.regParam, [0.1, 0.01]) \
        .addGrid(lr.fitIntercept, [False, True])\
        .addGrid(lr.elasticNetParam, [0.0, 0.5, 1.0])\
        .build()

我有这样的字典

 dict =   {lr.regParam : [0.1,0.01],lr.fitIntercept:[False,True],lr.elasticNetParam:[0.0,0.5,1.0]

我们可以使用循环来构建ParamgridBuilder吗?

for k,v in dict.items():
    paramGrid = ParamGridBuilder().addGrid(k,v).build()

1 个答案:

答案 0 :(得分:1)

您可以使用reduce函数:

from functools import reduce
paramGrid  = reduce(
    lambda a,b: a.addGrid(*b),
    dict.items(),
    ParamGridBuilder(),
).build

或带有for循环

paramGrid = ParamGridBuilder()
for k,v in dict.items():
    paramGrid = paramGrid.addGrid(k,v)
paramGrid = paramGrid.build()