Home > front end >  How to loop through a dictionary and assign values to a variable based on keys?
How to loop through a dictionary and assign values to a variable based on keys?

Time:11-07

This is driving me nuts, I've been searching for a couple of hours and am having trouble making heads or tails of this.

Usually I would do something like this in SQL, but it's part of a python model and I am having a hard time wrapping my head around how the variable assignment works.

I have a dictionary (used the syntax from Facebook Prophet's 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]
              }

Then I create the dictionary of all parameter permutations:

# 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

Which looks like this (for reference):


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.,]

Then, what I want to do is pass each value to it's corresponding model component:

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.,

I know the syntax must be wildly off, but I can't figure out how to assign the value of the dictionary to the model variable.

As an example, I would want the fist model loop to run this:

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.,

I'm sure this is python 101, hoping someone can help point me in the right direction.

CodePudding user response:

you're close! look here

for params in all_params:
    m = Prophet(
        changepoint_prior_scale = all_params['changepoint_prior_scale'],
        changepoint_range = all_params['changepoint_range'],
        seasonality_mode = 'multiplicative',...

notice that you made params but are still using all_params for access! change it like this:

for params in all_params:
        m = Prophet(
            changepoint_prior_scale = params['changepoint_prior_scale'],
            changepoint_range = params['changepoint_range'],
            seasonality_mode = 'multiplicative',...
  • Related