我的代码中出现此错误:AttributeError: 'str' object has no attribute 'mean_validation_score'
。我可以做什么来解决它?
def report(grid_scores, n_top=3):
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores):
print("Rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
report(clf.cv_results_)
错误非常明显:AttributeError: 'str' object has no attribute 'mean_validation_score'
您只有一个地方可以使用mean_validation_score
你使用它的对象是string
- 不是什么you我认为是的。string
不支持您使用的方法 - 因此出现错误:
for i, score in enumerate(top_scores): # score from here
print("Rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
score.mean_validation_score, # usage here
np.std(score.cv_validation_scores)))
显然top_scores
是一个字符串类型的可迭代对象 - 因此当你枚举它时
for i, score in enumerate(top_scores):
它产生索引i
和字符串score
.
您可以通过调试代码来解决它:
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
看看为什么其中有字符串 - 修复它,使其包含具有以下内容的对象.mean_validation_score
错误就消失了。
Helpful:
- Python 调试技巧 https://stackoverflow.com/questions/1623039/python-debugging-tips
- https://wiki.python.org/moin/PythonDebuggingTools https://wiki.python.org/moin/PythonDebuggingTools
- 如何调试小程序 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)