Sti*_*cky 57 python scikit-learn
我收到这个奇怪的错误:
classification.py:1113: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)`
Run Code Online (Sandbox Code Playgroud)
但是它也会在我第一次运行时打印出f-score:
metrics.f1_score(y_test, y_pred, average='weighted')
Run Code Online (Sandbox Code Playgroud)
我第二次跑,它提供没有错误的分数.这是为什么?
>>> y_pred = test.predict(X_test)
>>> y_test
array([ 1, 10, 35, 9, 7, 29, 26, 3, 8, 23, 39, 11, 20, 2, 5, 23, 28,
30, 32, 18, 5, 34, 4, 25, 12, 24, 13, 21, 38, 19, 33, 33, 16, 20,
18, 27, 39, 20, 37, 17, 31, 29, 36, 7, 6, 24, 37, 22, 30, 0, 22,
11, 35, 30, 31, 14, 32, 21, 34, 38, 5, 11, 10, 6, 1, 14, 12, 36,
25, 8, 30, 3, 12, 7, 4, 10, 15, 12, 34, 25, 26, 29, 14, 37, 23,
12, 19, 19, 3, 2, 31, 30, 11, 2, 24, 19, 27, 22, 13, 6, 18, 20,
6, 34, 33, 2, 37, 17, 30, 24, 2, 36, 9, 36, 19, 33, 35, 0, 4,
1])
>>> y_pred
array([ 1, 10, 35, 7, 7, 29, 26, 3, 8, 23, 39, 11, 20, 4, 5, 23, 28,
30, 32, 18, 5, 39, 4, 25, 0, 24, 13, 21, 38, 19, 33, 33, 16, 20,
18, 27, 39, 20, 37, 17, 31, 29, 36, 7, 6, 24, 37, 22, 30, 0, 22,
11, 35, 30, 31, 14, 32, 21, 34, 38, 5, 11, 10, 6, 1, 14, 30, 36,
25, 8, 30, 3, 12, 7, 4, 10, 15, 12, 4, 22, 26, 29, 14, 37, 23,
12, 19, 19, 3, 25, 31, 30, 11, 25, 24, 19, 27, 22, 13, 6, 18, 20,
6, 39, 33, 9, 37, 17, 30, 24, 9, 36, 39, 36, 19, 33, 35, 0, 4,
1])
>>> metrics.f1_score(y_test, y_pred, average='weighted')
C:\Users\Michael\Miniconda3\envs\snowflakes\lib\site-packages\sklearn\metrics\classification.py:1113: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
0.87282051282051276
>>> metrics.f1_score(y_test, y_pred, average='weighted')
0.87282051282051276
>>> metrics.f1_score(y_test, y_pred, average='weighted')
0.87282051282051276
Run Code Online (Sandbox Code Playgroud)
另外,为什么会出现尾随'precision', 'predicted', average, warn_for)错误消息?没有开括号,为什么它以右括号结束?我在Windows 10的conda环境中使用Python 3.6.0运行sklearn 0.18.1.
Sho*_*alt 76
如评论中所述,y_true中的某些标签不会出现在y_pred中.特别是在这种情况下,永远不会预测标签'2':
>>> set(y_test) - set(y_pred)
{2}
Run Code Online (Sandbox Code Playgroud)
这意味着没有为此标签计算的F分数,因此该案例的F分数被认为是0.0.由于您要求平均分数,您必须考虑到计算中包含0分,这就是scikit-learn向您显示该警告的原因.
这让我没有第二次看到错误.正如我所提到的,这是一个警告,与python中的错误不同.大多数环境中的默认行为是仅显示一次特定警告.可以更改此行为:
import warnings
warnings.filterwarnings('always') # "error", "ignore", "always", "default", "module" or "once"
Run Code Online (Sandbox Code Playgroud)
如果在导入其他模块之前设置此项,则每次运行代码时都会看到警告.
除了设置之外,没有办法避免第一次看到此警告warnings.filterwarnings('ignore').你有什么可以做的,就是决定你不感兴趣,那些没有预测标签的分数,然后明确指定标签是兴趣(这是被预测至少一次标签):
>>> metrics.f1_score(y_test, y_pred, average='weighted', labels=np.unique(y_pred))
0.91076923076923078
Run Code Online (Sandbox Code Playgroud)
在这种情况下不显示警告.
小智 7
我最终遇到了同样的错误,但在阅读了 @Shovalt 的答案后,我意识到我的测试/训练比例相当低。我一开始有一个很大的数据集,但将其拆分后,其中一组非常小。通过增大样本量,这个警告消失了,我得到了 f1 分数。由此
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
Run Code Online (Sandbox Code Playgroud)
对此
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
Run Code Online (Sandbox Code Playgroud)
小智 5
当我训练我的分类模型时,同样的问题也发生在我身上。导致这个问题的原因是警告消息说“在没有预测样本的标签中”,它会在计算f1-score时导致零除。我在阅读sklearn.metrics.f1_score doc时找到了另一个解决方案,有一个注释如下:
当真阳性 + 假阳性 == 0 时,精度未定义;当真阳性 + 假阴性 == 0 时,召回是不确定的。在这种情况下,默认情况下,度量将设置为 0,f-score 也是如此,并且将引发 UndefinedMetricWarning。可以使用 zero_division 修改此行为
该zero_division默认值是"warn",你可以将其设置为0或1要避免UndefinedMetricWarning。它对zero_division我有用;) 哦等等,当我使用时还有另一个问题,我的 sklearn 报告说使用 scikit-learn 0.21.3 没有这样的关键字参数。只需通过运行将您的 sklearn 更新到最新版本pip install scikit-learn -U
| 归档时间: |
|
| 查看次数: |
67839 次 |
| 最近记录: |