AttributeError: 'str' object has no attribute 'parameters' due to new version of sklearn

AttributeError: 'str' object has no attribute 'parameters' due to new version of sklearn

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

AttributeError: 'str' object has no attribute 'parameters'

我想我理解的问题是:'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',用于存储所有候选参数的参数设置字典列表。您可以从 sklearn 文档中查看 GridSearchCv 文档 here

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

cv_results_ 的输出是一个字符串键的字典,如 'params'、'split0_test_score' 等(你可以参考文档),它们的值为 listarray

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

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]