简体   繁体   中英

How to graph cv_results_ replacy grid_scores_ from GridSearchCV?

I'm looking for a way to graph cv_results_ from GridSearchCV in sklearn. But the code from example used grid_scores_

n_topics = [10, 15, 20, 25, 30]
log_likelyhoods_5 = [round(gscore.mean_validation_score) for gscore in 
model.grid_scores_ if gscore.parameters['learning_decay']==0.5]
log_likelyhoods_7 = [round(gscore.mean_validation_score) for gscore in 
model.grid_scores_ if gscore.parameters['learning_decay']==0.7]
log_likelyhoods_9 = [round(gscore.mean_validation_score) for gscore in 
model.grid_scores_ if gscore.parameters['learning_decay']==0.9]

I'm change grid_scores_ on cv_results

results = pd.DataFrame(model.cv_results_)
og_likelyhoods_5 = [round(results['mean_test_score'][gscore]) for gscore in 
results['params'] if results['params'][gscore]['learning_decay']==0.5]
log_likelyhoods_7 = [round(results['mean_test_score'][gscore]) for gscore in 
results['params'] if results['params'][gscore]['learning_decay']==0.7]
log_likelyhoods_9 = [round(results['mean_test_score'][gscore]) for gscore in 
results['params'] if results['params'][gscore]['learning_decay']==0.9]

cv_results include of ['params']

0     {'learning_decay': 0.5, 'n_components': 10}
1     {'learning_decay': 0.5, 'n_components': 15}
2     {'learning_decay': 0.5, 'n_components': 20}
3     {'learning_decay': 0.5, 'n_components': 25}
4     {'learning_decay': 0.5, 'n_components': 30}
5     {'learning_decay': 0.7, 'n_components': 10}
6     {'learning_decay': 0.7, 'n_components': 15}
7     {'learning_decay': 0.7, 'n_components': 20}
8     {'learning_decay': 0.7, 'n_components': 25}
9     {'learning_decay': 0.7, 'n_components': 30}
10    {'learning_decay': 0.9, 'n_components': 10}
11    {'learning_decay': 0.9, 'n_components': 15}
12    {'learning_decay': 0.9, 'n_components': 20}
13    {'learning_decay': 0.9, 'n_components': 25}
14    {'learning_decay': 0.9, 'n_components': 30}

I have error

Traceback (most recent call last):
File "finallda.py", line 134, in <module>
log_likelyhoods_5 = [round(results['mean_test_score'][gscore]) for gscore 
in len(results['params']) if results['params'][gscore] 
['learning_decay']==0.5]
KeyError: "None of [['learning_decay', 'n_components']] are in the [index]"

I have to extract ['mean_test_score'] well condition:

gscore.parameters['learning_decay']==0.5
gscore.parameters['learning_decay']==0.7
gscore.parameters['learning_decay']==0.9

I am a bit confused so please make a minimal example. Please show which example you used. Did it work?

In the Traceback I only see one step, so the error is not in a library but in your code, right?

The line failing is this one:

og_likelyhoods_5 = [round(results['mean_test_score'][gscore]) for gscore in 
results['params'] if results['params'][gscore]['learning_decay']==0.5]

Please unfold the listcomprehention (you are doing several steps in one line here which is complex) into a for-loop to see exactly how the list looks like and where the key is missing.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM