○ The main points of this article I've been studying a couple of times, but I can't remember the mixed matrix. Output with the code to remember.
For testing mixed matrices
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
import numpy as np
#Breast cancer data download
data = load_breast_cancer()
X = data.data
#Invert labels 0 and 1 (in the dataset, 0 is malignant, 1 is benign, but 0 is benign and 1 is malignant)
y = 1 - data.target
X = X[:, :10]
#Logistic regression model
model = LogisticRegression()
#Learning
model.fit(X, y)
#Forecast
predict = model.predict(X)
#Instance for displaying mixed matrix
cm = confusion_matrix(y, predict)
result
print(cm)
[[337 20]
[ 30 182]]
TN, FP, FN, TP in order from top left to right TN: Actually, negative data is correctly predicted as negative In this example, benign data is correctly predicted to be benign. FP: Actually, negative data is mistakenly predicted as positive In this example, the benign data was mistakenly predicted to be malignant. FN: Actually, positive data is mistakenly predicted as negative In this example, the malignant data was mistakenly predicted to be benign. TP: Actually, positive data is correctly predicted as positive In this example, malignant data is correctly predicted to be malignant. As a prediction of cancer, FN (malignant data is mistakenly predicted to be benign) is bad.
Percentage of correct predictions for all prediction results
Correct answer rate= (TP + TN) / (TP + TN + FP + FN)
Correct answer rate
accuracy_score(y, predict)
0.9121265377855887
Percentage of what was predicted to be positive to what was predicted to be positive
Compliance rate= TP / (TP + FP)
Compliance rate
precision_score(y, predict)
0.900990099009901
Percentage of positives that can be correctly predicted compared to positives
Recall= TP / (TP + FN)
Recall
recall_score(y, predict)
0.8584905660377359
A value that reflects trends in both precision and recall
F value= 2×(Conformity rate x recall rate)/(Compliance rate+Recall)
F value
f1_score(y, predict)
0.8792270531400966
■ Impressions ・ I understand every time I read the definition, but I forget it after a while. .. .. ・ You have to make it pop out when you actually use it. .. ..
Recommended Posts