先来看看报错的代码:
import sklearn
sorted(sklearn.metrics.keys())
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[81], line 4
2 import sklearn
3 # from sklearn.metrics import SCORERS
----> 4 sorted(sklearn.metrics.keys())#这里的keys()是一个方法,不是一个属性
AttributeError: module 'sklearn.metrics' has no attribute 'keys'
然后试着修改代码:
from sklearn.metrics import SCORERS
sorted(sklearn.metrics.keys())
ok,继续报错:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[82], line 3
1 #来查看一下sklearn中所有的模型评估指标
2 import sklearn
----> 3 from sklearn.metrics import SCORERS
4 sorted(sklearn.metrics.keys())#这里的keys()是一个方法,不是一个属性
ImportError: cannot import name 'SCORERS' from 'sklearn.metrics' (D:\python\Lib\site-packages\sklearn\metrics\__init__.py)
最后使用scikit-learn中的另一个函数_scorer:
from sklearn.metrics import _scorer
sorted(_scorer.make_scorer.__dict__.keys())
问题成功解决。