gpt4 book ai didi

scikit-learn - 属性错误 : 'str' object has no attribute 'parameters' due to new version of sklearn

转载 作者:行者123 更新时间:2023-12-04 02:40:50 24 4
gpt4 key购买 nike

我正在使用 sklearn 进行主题建模。在尝试从网格搜索输出中获取对数似然时,出现以下错误:

AttributeError: 'str' object has no attribute '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()

提前致谢!

最佳答案

有一个键“params”,用于存储所有候选参数的参数设置字典列表。您可以查看 GridSearchCv 文档 here来自 sklearn 文档。

在您的代码中,gscorecv_results_ 的字符串键值。

cv_results_ 的输出是字符串键的字典,如 'params'、'split0_test_score' 等(您可以引用文档)及其值为 list数组

因此,您需要对代码进行以下更改:

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]

关于scikit-learn - 属性错误 : 'str' object has no attribute 'parameters' due to new version of sklearn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com