如何使用ParameterGrid将多个列表作为输入,遍历所有组合,并将结果输入到函数以测试所有选项

2024-03-28 11:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个我创建的函数,它有很多输入,比如开始日期,周期,变量名等。目前我必须手动输入值到函数中,以允许它开始运行,但我想尝试并自动执行。有些输入是恒定的,不需要改变,而有些则需要改变结果。你知道吗

我想用迭代器改变的输入是:

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

我确实尝试使用参数网格来改变使用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)

Tags: theto函数testfalsevarsteptrain
1条回答
网友
1楼 · 发布于 2024-03-28 11:26:59

基于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'])

相关问题 更多 >