sklearn.metrics.classificationは、適合率と再現率の反転値を報告する場合がありますか?
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
def calc_precision_recall(conf_matrix, class_labels):
# for each class
for i in range(len(class_labels)):
# calculate true positives
true_positives =(conf_matrix[i, i])
# false positives
false_positives = (conf_matrix[i, :].sum() - true_positives)
# false negatives
false_negatives = 0
for j in range(len(class_labels)):
false_negatives += conf_matrix[j, i]
false_negatives -= true_positives
# and finally true negatives
true_negatives= (conf_matrix.sum() - false_positives - false_negatives - true_positives)
# print calculated values
print(
"Class label", class_labels[i],
"T_positive", true_positives,
"F_positive", false_positives,
"T_negative", true_negatives,
"F_negative", false_negatives,
"\nSensitivity/recall", true_positives / (true_positives + false_negatives),
"Specificity", true_negatives / (true_negatives + false_positives),
"Precision", true_positives/(true_positives+false_positives), "\n"
)
return
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, 0:3] # we only take the first two features.
y = iris.target
# Random_state parameter is just a random seed that can be used to reproduce these specific results.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=27)
# Instantiate a K-Nearest Neighbors Classifier:
KNN_model = KNeighborsClassifier(n_neighbors=2)
# Fit the classifiers:
KNN_model.fit(X_train, y_train)
# Predict and store the prediction:
KNN_prediction = KNN_model.predict(X_test)
# Generate the confusion matrix
conf_matrix = confusion_matrix(KNN_prediction, y_test)
# Print the classification report
print(classification_report(KNN_prediction, y_test))
# Dummy class labels for the three iris classes
class_labels = [0,1,2]
# Own function to calculate precision and recall from the confusion matrix
calc_precision_recall(conf_matrix, class_labels)
私の関数は、クラスごとに以下を返します。
クラスラベル0T_positive 7 F_positive 0 T_negative 23 F_negative 0
感度/再現率1.0特異度1.0適合率1.0
クラスラベル1T_positive 11 F_positive 1 T_negative 18 F_negative 0
感度/再現率1.0特異度0.9473684210526315適合率0.9166666666666666
クラスラベル2T_positive 11 F_positive 0 T_negative 18 F_negative 1
感度/再現率0.9166666666666666特異度1.0適合率1.0
precision recall
0 1.00 1.00
1 0.92 1.00
2 1.00 0.92
私の関数は、混同行列が上部のx軸に実際の値、左側のy軸に予測値で構成されていることを前提としています。 これは、ウィキペディアで使用されているものや、混同行列関数のドキュメントで参照されているものと同じ構造です。
対照的に、これらはsklearn.metrics importclassification_reportによって報告された結果です。
precision recall f1-score support
0 1.00 1.00 1.00 7
1 1.00 0.92 0.96 12
2 0.92 1.00 0.96 11
システム:
python:3.8.1(デフォルト、2020年1月8日、22:29:32)[GCC 7.3.0]
実行可能ファイル:/ home / will / anaconda3 / envs / ElStatLearn / bin / python
マシン:Linux-4.15.0-91-generic-x86_64-with-glibc2.10
Pythonの依存関係:
ピップ:20.0.2
setuptools:38.2.5
sklearn:0.22.1
numpy:1.18.1
scipy:1.4.1
Cython:なし
パンダ:1.0.1
matplotlib:3.1.3
joblib:0.14.1
OpenMPで構築:True
y_test
はprint(classification_report(KNN_prediction, y_test))
最初に来るべきだと思います。
だから: print(classification_report(y_test, KNN_prediction))
。
関数sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
は、最初の引数としてy_true
があります。 これにより、適合率が反転し、再現率が向上します。
Classification_reportを参照してください。
編集:あなたの混同行列も後方にありますが、sklearnの混同行列はウィキペディアから後方にあるのでうまくいきます。
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
[0, 0, 1],
[1, 0, 2]])
行1に1つの観測値があり、列1に0があることがわかります。したがって、行はグラウンドトゥルースであり、列は予測です。 したがって、 confusion_matrixに示されているC[i, j]
表記を使用できます。
それを明確にしてくれてありがとう-ウィキペディアのリファレンスは私を混乱させました!
問題ありません。おそらくウィキペディアに例をsklearnオリエンテーションに切り替えさせる必要があります。
最も参考になるコメント
y_test
はprint(classification_report(KNN_prediction, y_test))
最初に来るべきだと思います。だから:
print(classification_report(y_test, KNN_prediction))
。関数
sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
は、最初の引数としてy_true
があります。 これにより、適合率が反転し、再現率が向上します。Classification_reportを参照してください。
編集:あなたの混同行列も後方にありますが、sklearnの混同行列はウィキペディアから後方にあるのでうまくいきます。
行1に1つの観測値があり、列1に0があることがわかります。したがって、行はグラウンドトゥルースであり、列は予測です。 したがって、 confusion_matrixに示されている
C[i, j]
表記を使用できます。