分類モデルの評価

ロウ
2020-03-17
ロウ
2020-03-17

前置き

scikit-learn機械学習ライブラリが用意されているメソッドを用いて、分類結果を評価します。ここにメモとして残しておきます。

精度

精度(Accuracy) = (TP + TN) / (TP + TN + FP + FN)

```
>>> from sklearn.metrics import accuracy_score
>>> y_test = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
>>> y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
>>> accuracy_score(y_test, y_pred)
0.6
```

適合率

適合率(Precision) = TP / (TP + FP)

```
>>> from sklearn.metrics import precision_score
>>> y_test = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
>>> y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
>>> precision_score(y_test, y_pred)
0.75
```

再現率

再現率(Recall) = TP / (TP + FN)

```
>>> from sklearn.metrics import recall_score
>>> y_test = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
>>> y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
>>> recall_score(y_test, y_pred)
0.5
```

F値

F値 (F1 scoreまたはF-measure) = 2 * (Precision * Recall) / (Precision + Recall)

```
>>> from sklearn.metrics import f1_score
>>> y_test = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
>>> y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
>>> f1_score(y_test, y_pred)
0.6
```

classification_report

```
>>> from sklearn.metrics import classification_report
>>> y_test = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
>>> y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 0, 0]
>>> classification_report(y_test, y_pred)
                     precision   recall   f1-score   support
             0        0.50         0.75    0.60            4
             1        0.75         0.50    0.60            6

      accuracy                             0.60           10
     macro avg        0.62         0.62    0.60           10
  weighted avg        0.65         0.60    0.60           10
```