AttributeError: 'str' object has no attribute 'parameters' due to new version of sklearn
Asked Answered
P

1

4

I am doing topic modeling using sklearn. While trying to get the log-likelihood from Grid Search output, I am getting the below error:

AttributeError: 'str' object has no attribute 'parameters'

I think I understand the issue which is: 'parameters' is used in the older version and I am using the new version (0.22) of sklearn and that is giving error. I also search for the term which is used in the new version but couldn't find it. Below is the code:

# Get Log Likelyhoods from Grid Search Output
n_components = [10, 15, 20, 25, 30]
log_likelyhoods_5 = [round(gscore.mean_validation_score) for gscore in model.cv_results_ if gscore.parameters['learning_decay']==0.5]
log_likelyhoods_7 = [round(gscore.mean_validation_score) for gscore in model.cv_results_ if gscore.parameters['learning_decay']==0.7]
log_likelyhoods_9 = [round(gscore.mean_validation_score) for gscore in model.cv_results_ if gscore.parameters['learning_decay']==0.9]

# Show graph
plt.figure(figsize=(12, 8))
plt.plot(n_components, log_likelyhoods_5, label='0.5')
plt.plot(n_components, log_likelyhoods_7, label='0.7')
plt.plot(n_components, log_likelyhoods_9, label='0.9')
plt.title("Choosing Optimal LDA Model")
plt.xlabel("Num Topics")
plt.ylabel("Log Likelyhood Scores")
plt.legend(title='Learning decay', loc='best')
plt.show()

Thanks in advance!

Photobathic answered 27/12, 2019 at 2:56 Comment(0)
S
6

There is key 'params' which is used to store a list of parameter settings dicts for all the parameter candidates. You can see the GridSearchCv doc here from sklearn documentation.

In your code, gscore is a string key value of cv_results_.

Output of cv_results_ is a dictionary of string key like 'params','split0_test_score' etc(you can refer the doc) and their value as list or array etc.

So, you need to make following change to your code :

log_likelyhoods_5 = [round(model.cv_results_['mean_test_score'][index]) for index, gscore in enumerate(model.cv_results_['params']) if gscore['learning_decay']==0.5]
Sarabia answered 27/12, 2019 at 6:12 Comment(3)
I am still getting an error: NameError: name 'model' is not defined. Also, I even tried 'best_lda_model' instead of 'model' but still getting NameError: name 'best_lda_model' is not defined.Photobathic
@PiyushGhasiya, model is your classification model that you used in your code for GridSearchCV. Can you show the code where you are applying GridSearchCv code?Sarabia
"mean_validation_score" is depricated now it's "mean_test_score". Use "mean_test_score". For your confirmation you can check-it-out gridA.cv_results_.keys() After running the above comment you can see there is no "mean_validation_score".Whitehurst

© 2022 - 2024 — McMap. All rights reserved.