scikit-learnのf1_scoreでUndefinedMetricWarning
sklearn.metrics.f1_score
Notes in https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
precisionもrecallも定義されないとき(=trueもpredも全部のラベルが0のとき)、F1 scoreは定義されない
When true positive + false positive == 0, precision is undefined.
When true positive + false negative == 0, recall is undefined.
In such cases, by default the metric will be set to 0, as will f-score, and UndefinedMetricWarning will be raised.
This behavior can be modified with zero_division.
zero_division引数
"warn", 0 or 1, default=”warn”
Sets the value to return when there is a zero division, i.e. when all predictions and labels are negative. If set to “warn”, this acts as 0, but warnings are also raised.
デフォルトはwarnでf1 scoreが定義されないとき0として扱う
1として扱うように指定もできる
(IMO:全てnegativeというpredはtrueと一致している、とみなせる)
code:Examplesよりzero_division=1の指定.py
>> y_true = 0, 0, 0, 0, 0, 0
>> y_pred = 0, 0, 0, 0, 0, 0
>> f1_score(y_true, y_pred, zero_division=1)
1.0...
参考:サンプルのf1_scoreが0となるとき
mean-F1とmacro-F1とmicro-F1で手を動かすの2例目
y_true = [1, 0, 0]
y_pred = [0, 1, 0]
code:sklearn.metrics.confusion_matrix
array([1, 1,
1, 0])
IMO:y_true = [0, 0, 0]かつy_pred = [0, 0, 0]というケースとは扱いが異なるように思われる
zero_divisionを1に指定しない限り、うまく推論できないサンプルと扱いが同じと理解した