下面的代码是将参数添加到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()
最佳答案
您可以使用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()
关于python - 我们可以在pyspark的ParamGridBuilder中使用for循环吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52276923/