0

我正在寻找一种在 sklearn中绘制图表cv_results_的方法。GridSearchCV但是示例中的代码使用了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]

我改变grid_scores_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包括 ['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}

我有错误

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]"

我必须提取['mean_test_score']良好的条件:

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

2 回答 2

1

我有点困惑,所以请举一个最小的例子。请显示您使用的示例。它奏效了吗?

在 Traceback 中我只看到一个步骤,所以错误不在库中,而是在您的代码中,对吗?

失败的线路是这一行:

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

请将 listcomprehention(您在这里的一行中执行多个步骤,这很复杂)展开到 for 循环中,以准确查看列表的外观以及缺少键的位置。

希望这可以帮助!

于 2019-05-12T13:05:01.567 回答
0
log_likelyhoods_5 = [round(result['mean_test_score'][index]) for index in range(len(result['params'])) if result['params'][index]['learning_decay']==0.5]
log_likelyhoods_7 = [round(result['mean_test_score'][index]) for index in range(len(result['params'])) if result['params'][index]['learning_decay']==0.7]
log_likelyhoods_9 = [round(result['mean_test_score'][index]) for index in range(len(result['params'])) if result['params'][index]['learning_decay']==0.9]
于 2021-09-11T02:01:11.263 回答