Piy*_*iya 3 scikit-learn python-3.7 log-likelihood gridsearchcv
我正在使用 sklearn 进行主题建模。在尝试从网格搜索输出中获取对数似然时,出现以下错误:
AttributeError: 'str' 对象没有属性 'parameters'
我想我明白这个问题是:在旧版本中使用了“参数”,而我正在使用 sklearn 的新版本(0.22),这出现了错误。我还搜索了新版本中使用的术语,但找不到。下面是代码:
# 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()
Run Code Online (Sandbox Code Playgroud)
提前致谢!
有一个键 'params' 用于存储所有候选参数的参数设置字典列表。您可以在sklearn 文档中查看 GridSearchCv文档。
在您的代码中,gscore是cv_results_.
输出cv_results_是字符串键的字典,如“params”、“split0_test_score”等(您可以参考文档)及其值作为list或array等。
因此,您需要对代码进行以下更改:
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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |