classification_report
を用いてはいかがでしょうか。
sklearn.metrics.classification_report — scikit-learn 0.21.3 documentation
シンプルな使用例。
python
1 ort pandas as pd
2 from sklearn . datasets import load_iris
3 from sklearn . svm import SVC
4 from sklearn . model_selection import train_test_split
5 from sklearn . metrics import classification_report
6
7 iris = load_iris ( )
8 clf = SVC ( gamma = "scale" )
9
10 X_train , X_test , y_train , y_test = train_test_split ( iris . data , iris . target , test_size = 0.4 , stratify = iris . target , random_state = 0 )
11
12 clf . fit ( X_train , y_train )
13 pred = clf . predict ( X_test )
14
15 print ( classification_report ( y_test , pred , target_names = iris . target_names ) )
16 """ =>
17 precision recall f1-score support
18
19 setosa 1.00 1.00 1.00 20
20 versicolor 1.00 0.95 0.97 20
21 virginica 0.95 1.00 0.98 20
22
23 accuracy 0.98 60
24 macro avg 0.98 0.98 0.98 60
25 weighted avg 0.98 0.98 0.98 60
26
27 """
28
29 report_dict = classification_report ( y_test , pred , target_names = iris . target_names , output_dict = True )
30 print ( report_dict )
31 """ =>
32 {'virginica': {'precision': 0.9523809523809523, 'recall': 1.0, 'support': 20, 'f1-score': 0.975609756097561}, 'macro avg': {'precision': 0.9841269841269842, 'recall': 0.9833333333333334, 'support': 60, 'f1-score': 0.9833229101521784}, 'versicolor': {'precision': 1.0, 'recall': 0.95, 'support': 20, 'f1-score': 0.9743589743589743}, 'setosa': {'precision': 1.0, 'recall': 1.0, 'support': 20, 'f1-score': 1.0}, 'accuracy': 0.9833333333333333, 'weighted avg': {'precision': 0.9841269841269842, 'recall': 0.9833333333333333, 'support': 60, 'f1-score': 0.9833229101521785}}
33 """
34
35 report_df = pd . DataFrame ( report_dict )
36 print ( report_df )
37 """ =>
38 accuracy macro avg setosa versicolor virginica weighted avg
39 f1-score 0.983333 0.983323 1.0 0.974359 0.975610 0.983323
40 precision 0.983333 0.984127 1.0 1.000000 0.952381 0.984127
41 recall 0.983333 0.983333 1.0 0.950000 1.000000 0.983333
42 support 0.983333 60.000000 20.0 20.000000 20.000000 60.000000
43 """
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/01 13:55