[PYTHON] About machine learning mixed matrices

○ 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.

○ Source code (Python)

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)

Mixed matrix

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.

Correct answer rate

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

Compliance rate

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

Recall

Percentage of positives that can be correctly predicted compared to positives

Recall= TP / (TP + FN)

Recall


recall_score(y, predict)
0.8584905660377359

F value

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

About machine learning mixed matrices
About machine learning overfitting
Machine learning
A story about machine learning with Kyasuket
[Memo] Machine learning
Machine learning classification
Machine Learning sample
A story about simple machine learning using TensorFlow
Gaussian mixed model EM algorithm [statistical machine learning]
About the development contents of machine learning (Example)
A story about data analysis by machine learning
Machine learning tutorial summary
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning logistic regression
Machine learning support vector machine
Studying Machine Learning ~ matplotlib ~
Machine learning linear regression
Machine learning course memo
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
What I learned about AI / machine learning using Python (1)
About testing in the implementation of machine learning models
What I learned about AI / machine learning using Python (3)
What I learned about AI / machine learning using Python (2)
Talk about improving machine learning algorithm bottlenecks with Cython
Machine learning learned with Pokemon
Data set for machine learning
Japanese preprocessing for machine learning
Machine learning in Delemas (practice)
An introduction to machine learning
Machine learning / classification related techniques
Machine Learning: Supervised --Linear Regression
Machine learning beginners tried RBM
[Machine learning] Understanding random forest
About learning with google colab
What I learned about AI and machine learning using Python (4)
Machine Learning Study Resource Notepad
Machine learning ② Naive Bayes Summary
Understand machine learning ~ ridge regression ~.
Machine learning article summary (self-authored)
Machine Learning: Supervised --Random Forest
Practical machine learning system memo
Machine learning Minesweeper with PyTorch
Machine learning environment construction macbook 2021
Build a machine learning environment
Python Machine Learning Programming> Keywords
Machine learning algorithm (simple perceptron)
Used in machine learning EDA
Importance of machine learning datasets
Machine learning and mathematical optimization
Machine Learning: Supervised --Support Vector Machine
Supervised machine learning (classification / regression)
I implemented Extreme learning machine
Beginning with Python machine learning