AttributeError: 由于 sklearn 的新版本,“str"对象没有属性“参数" [英] AttributeError: 'str' object has no attribute 'parameters' due to new version of sklearn
问题描述
我正在使用 sklearn 进行主题建模.在尝试从网格搜索输出中获取对数似然时,我收到以下错误:
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' 对象没有属性 'parameters'
AttributeError: 'str' object has no attribute 'parameters'
我想我明白这个问题是:在旧版本中使用了参数",而我正在使用 sklearn 的新版本 (0.22) 并且出现错误.我还搜索了新版本中使用的术语,但找不到.代码如下:
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()
提前致谢!
推荐答案
有一个键 'params' 用于存储所有候选参数的参数设置字典列表.您可以在 此处查看 GridSearchCv 文档.a> 来自 sklearn 文档.
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.
在您的代码中,gscore
是 cv_results_
的字符串键值.
In your code, gscore
is a string key value of cv_results_
.
cv_results_
的输出是字符串键的字典,如 'params'、'split0_test_score' 等(你可以参考文档),它们的值是 list
或 array
等
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]
这篇关于AttributeError: 由于 sklearn 的新版本,“str"对象没有属性“参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!