我创建了一个函数,该函数具有多个输入,例如开始日期、期间、变量名称等。目前我必须手动将值输入函数以允许它开始运行,但我想尝试将其自动化。一些输入是不变的,不需要改变,而另一些则需要改变结果。
我想使用迭代器更改的输入是:
train_period = [1, 4, 16, 39]
###The different test periods
test_start = ['2014-01-01 00:00', '2014-07-01 00:00']
###The different response variables
test_var = ['Temperature']
###Different step-ahead
step_ahead = [1, 4, 16, 96]
###Whether to consider smoothing or not
smoothing = [True, False]
###Define the grid of parameters to search
hyper_grid = {'train_period': train_period,
'test_start': test_start,
'test_var': test_var,
'step_ahead': step_ahead,
'smoothing': smoothing}
from sklearn.model_selection import ParameterGrid
我确实尝试过使用 parametergrid 来使用 forloop 进行更改,但不幸的是它不起作用
grid = ParameterGrid(hyper_grid)
for params in grid:
results dataframe format based on for loop index= Function(params['train_period'], params['test_start'], params['test_var'], params['step_ahead'], params['smoothing'])
结果应该替换下面代码中函数端的值而不是固定值。
result1, result2, result3 = Function(fixedvalue1, fixedvalue2, train_period, test_start, test_period, test_var, step_ahead, smoothing = False)
请您参考如下方法:
基于 the documentation ,您可以在 grid 上调用 list() 并且您的 for 循环会起作用,但是您需要为结果编制索引或对它们做一些事情(例如,保存性能指标)在循环的下一次迭代之前。
grid = list(ParameterGrid(hyper_grid))
for params in grid:
results = Function(params['train_period'], params['test_start'], params['test_var'], params['step_ahead'], params['smoothing'])
