深層学習とその他

機械学習したいマン

scikit-learnのclassification_reportでlabelを設定する

アドベントカレンダー19日目です。
今日はclassification_reportでラベルを設定する方法を書いていきたいと思います。
adventar.org

ラベルの設定の仕方

scikit-learnのドキュメントはこちらです。
sklearn.metrics.classification_report — scikit-learn 0.20.2 documentation


classification_reportにラベルと予測結果を渡すことで、表を出力してくれます。
その際にラベルを設定しようとして少してこずったので書いていきます。


classification_reportの引数は以下の通りです。

classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False)

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html#sklearn.metrics.classification_report



これを見たときに、labelsにラベルを渡せばいいのかと思ったのですが、エラーを吐いてだめでした。
実際には、target_namesに渡す必要があったみたいです。

print(classification_report(label, pred, target_names=["a", "b", "c", "d", "e", "f", "g", "h", "i"]))
             precision    recall  f1-score   support

          a       0.16      0.63      0.26       104
          b       0.71      0.66      0.69       671
          c       0.02      0.48      0.04        29
          d       0.96      0.72      0.82      7205
          e       0.13      0.61      0.21       102
          f       0.49      0.55      0.52       796
          g       0.56      0.84      0.67       277
          h       0.02      0.57      0.04         7
          i       0.09      0.60      0.15        10

avg / total       0.87      0.70      0.77      9201

まとめ

これでclassification_reportをより見やすくすることができますね。
日本語を使用すると表がずれやすいので気を付けてください。