如何循环遍历字典并根据键为变量赋值?
How to loop through a dictionary and assign values to a variable based on keys?
这快把我逼疯了,我已经搜索了几个小时了,但还是找不到头绪。
通常我会在 SQL 中做这样的事情,但它是 python 模型的一部分,我很难理解变量赋值的工作原理。
我有一本字典(使用 Facebook Prophet GitHub 的语法):
param_grid = {
'changepoint_prior_scale': [.01, 0.05],
'changepoint_range': [0.8, 0.9],
'monthly_fourier': [5, 10],
'monthly_prior_scale': [.01, 0.05],
'daily_fourier': [5, 10],
'daily_prior_scale': [.01, 0.05],
'weekly_fourier': [5, 10],
'weekly_prior_scale': [.01, 0.05],
'yearly_fourier': [5],
'yearly_prior_scale': [.01, 0.05]
}
然后我创建所有参数排列的字典:
# Generate all combinations of parameters
all_params = [dict(zip(param_grid.keys(), v)) for v in itertools.product(*param_grid.values())]
mape = [] # Store the RMSEs for each params here for later
看起来像这样(供参考):
print(all_params)
[{'changepoint_prior_scale': 0.01, 'changepoint_range': 0.8, 'monthly_fourier': 5,
'monthly_prior_scale': 0.01, 'daily_fourier': 5, 'daily_prior_scale': 0.01, 'weekly_fourier': 5,
'weekly_prior_scale': 0.01, 'yearly_fourier': 5, 'yearly_prior_scale': 0.01},
{'changepoint_prior_scale': 0.01, 'changepoint_range': 0.8, 'monthly_fourier': 5,
'monthly_prior_scale': 0.01, 'daily_fourier': 5, 'daily_prior_scale': 0.01, 'weekly_fourier': 5,
'weekly_prior_scale': 0.01, 'yearly_fourier': 5, 'yearly_prior_scale': 0.05}....... etc.,]
然后,我要做的是将每个值传递给它对应的模型组件:
for params in all_params:
m = Prophet(
changepoint_prior_scale = all_params['changepoint_prior_scale'],
changepoint_range = all_params['changepoint_range'],
seasonality_mode = 'multiplicative',
growth = 'logistic',
holidays=Holidays,
).add_seasonality(
name='monthly',
period=30.5,
fourier_order = all_params['monthly_fourier'],
prior_scale = all_params['monthly_prior_scale']
).add_seasonality(
name='daily',
period=1,
fourier_order = all_params['daily_fourier'],
prior_scale = all_params['daily_prior_scale']
etc.,
我知道语法肯定有问题,但我不知道如何将字典的值赋给模型变量。
例如,我希望第一个模型循环到 运行 这个:
for params in all_params:
m = Prophet(
changepoint_prior_scale = 0.01,
changepoint_range = 0.8,
seasonality_mode = 'multiplicative',
growth = 'logistic',
holidays=Holidays,
).add_seasonality(
name='monthly',
period=30.5,
fourier_order = 5,
prior_scale = .01
).add_seasonality(
name='daily',
period=1,
fourier_order = 5,
prior_scale = .01
etc.,
我确定这是python101,希望有人能帮我指出正确的方向。
你很接近!看这里
for params in all_params:
m = Prophet(
changepoint_prior_scale = all_params['changepoint_prior_scale'],
changepoint_range = all_params['changepoint_range'],
seasonality_mode = 'multiplicative',...
请注意,您已创建 params
但仍在使用 all_params
进行访问!
像这样更改它:
for params in all_params:
m = Prophet(
changepoint_prior_scale = params['changepoint_prior_scale'],
changepoint_range = params['changepoint_range'],
seasonality_mode = 'multiplicative',...
这快把我逼疯了,我已经搜索了几个小时了,但还是找不到头绪。
通常我会在 SQL 中做这样的事情,但它是 python 模型的一部分,我很难理解变量赋值的工作原理。
我有一本字典(使用 Facebook Prophet GitHub 的语法):
param_grid = {
'changepoint_prior_scale': [.01, 0.05],
'changepoint_range': [0.8, 0.9],
'monthly_fourier': [5, 10],
'monthly_prior_scale': [.01, 0.05],
'daily_fourier': [5, 10],
'daily_prior_scale': [.01, 0.05],
'weekly_fourier': [5, 10],
'weekly_prior_scale': [.01, 0.05],
'yearly_fourier': [5],
'yearly_prior_scale': [.01, 0.05]
}
然后我创建所有参数排列的字典:
# Generate all combinations of parameters
all_params = [dict(zip(param_grid.keys(), v)) for v in itertools.product(*param_grid.values())]
mape = [] # Store the RMSEs for each params here for later
看起来像这样(供参考):
print(all_params)
[{'changepoint_prior_scale': 0.01, 'changepoint_range': 0.8, 'monthly_fourier': 5,
'monthly_prior_scale': 0.01, 'daily_fourier': 5, 'daily_prior_scale': 0.01, 'weekly_fourier': 5,
'weekly_prior_scale': 0.01, 'yearly_fourier': 5, 'yearly_prior_scale': 0.01},
{'changepoint_prior_scale': 0.01, 'changepoint_range': 0.8, 'monthly_fourier': 5,
'monthly_prior_scale': 0.01, 'daily_fourier': 5, 'daily_prior_scale': 0.01, 'weekly_fourier': 5,
'weekly_prior_scale': 0.01, 'yearly_fourier': 5, 'yearly_prior_scale': 0.05}....... etc.,]
然后,我要做的是将每个值传递给它对应的模型组件:
for params in all_params:
m = Prophet(
changepoint_prior_scale = all_params['changepoint_prior_scale'],
changepoint_range = all_params['changepoint_range'],
seasonality_mode = 'multiplicative',
growth = 'logistic',
holidays=Holidays,
).add_seasonality(
name='monthly',
period=30.5,
fourier_order = all_params['monthly_fourier'],
prior_scale = all_params['monthly_prior_scale']
).add_seasonality(
name='daily',
period=1,
fourier_order = all_params['daily_fourier'],
prior_scale = all_params['daily_prior_scale']
etc.,
我知道语法肯定有问题,但我不知道如何将字典的值赋给模型变量。
例如,我希望第一个模型循环到 运行 这个:
for params in all_params:
m = Prophet(
changepoint_prior_scale = 0.01,
changepoint_range = 0.8,
seasonality_mode = 'multiplicative',
growth = 'logistic',
holidays=Holidays,
).add_seasonality(
name='monthly',
period=30.5,
fourier_order = 5,
prior_scale = .01
).add_seasonality(
name='daily',
period=1,
fourier_order = 5,
prior_scale = .01
etc.,
我确定这是python101,希望有人能帮我指出正确的方向。
你很接近!看这里
for params in all_params:
m = Prophet(
changepoint_prior_scale = all_params['changepoint_prior_scale'],
changepoint_range = all_params['changepoint_range'],
seasonality_mode = 'multiplicative',...
请注意,您已创建 params
但仍在使用 all_params
进行访问!
像这样更改它:
for params in all_params:
m = Prophet(
changepoint_prior_scale = params['changepoint_prior_scale'],
changepoint_range = params['changepoint_range'],
seasonality_mode = 'multiplicative',...