Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting multiplication of list and float. #16

Closed
ghost opened this issue Feb 15, 2021 · 4 comments
Closed

Attempting multiplication of list and float. #16

ghost opened this issue Feb 15, 2021 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 15, 2021

In the function below we save hill_model['data']['y'] as a list of floats.

def train_hill_model(df, mc_df, adstock_params, media, sm):
    data, sc = create_hill_model_data(df, mc_df, adstock_params, media)
    fit = sm.sampling(data=data, iter=2000, chains=4)
    fit_result = fit.extract()
    hill_model = {
        'beta_hill_list': fit_result['beta_hill'].tolist(),
        'ec_list': fit_result['ec'].tolist(),
        'slope_list': fit_result['slope'].tolist(),
        'sc': sc,
        'data': {
            'X': data['X'].tolist(),
            'y': data['y'].tolist(),
        }
    }
    return hill_model

However later on we try to multiply hill_model['data']['y'] by a float. Since it is a list it can't legally be multiplied by a float, and multiplication by an integer just increases the size of the list.

def evaluate_hill_model(hill_model, hill_model_params):
    x = hill_model['data']['X']
    y_true = hill_model['data']['y'] * hill_model['sc']['y']
    print(y_true)
    y_pred = hill_model_predict(hill_model_params, x) * hill_model['sc']['y']
    print(y_pred)
    print('mape on original data: ', 
         mean_absolute_percentage_error(y_true, y_pred))
    return y_true, y_pred

To be able to preform the multiplication hill_model['data']['y'] * hill_model['sc']['y'] then hill_model['data']['y'] needs to be a numpy array, not a list.

@ghost
Copy link
Author

ghost commented Feb 15, 2021

The simple solution is to just remove the .tolist() method from data['X'] and data['y]

def train_hill_model(df, mc_df, adstock_params, media, sm):
    data, sc = create_hill_model_data(df, mc_df, adstock_params, media)
    fit = sm.sampling(data=data, iter=2000, chains=4)
    fit_result = fit.extract()
    hill_model = {
        'beta_hill_list': fit_result['beta_hill'].tolist(),
        'ec_list': fit_result['ec'].tolist(),
        'slope_list': fit_result['slope'].tolist(),
        'sc': sc,
        'data': {
            'X': data['X'],
            'y': data['y'],
        }
    }
    return hill_model

@sibylhe
Copy link
Owner

sibylhe commented Feb 15, 2021

Thanks for pointing out this! Your solution is correct.
tolist() is used to save the model as json file, because arrays cannot be saved as json. I will revise those model saving and loading functions later this weekend.

@ghost
Copy link
Author

ghost commented Feb 15, 2021

No problem! Thanks for putting this together; I've found it quite helpful.

@sibylhe
Copy link
Owner

sibylhe commented Feb 22, 2021

For the sake of model storage, I still prefer to store data['X'] and data['y'] as lists, so that the hill model can be saved as json using save_json(). I modified evaluate_hill_model(), converting them to arrays.

@ghost ghost closed this as completed Feb 23, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant