<jupyter_start><jupyter_text>Machine Learning in Python    Confusion Matrix    <NAME>   Topics:- [ ] What is a Confusion Matrix- [ ] Confusion Matrix Metrics- [ ] Displaying the Confusion Matrix using seaborn- [ ] Confusion Matrix with Scikit-learn- [ ] Multi-Class Confusion Matrix  What is Confusion Matrix? Confusion matrix represents the accuracy of the model in the tabular format by representing the count of correct/incorrect labels.  * Positive (P): Observation is positive.* Negative (N): Observation is not positive.* True Positive (TP): Outcome where the model correctly predicts the positive class.* True Negative (TN): Outcome where the model correctly predicts the negative class.* False Positive (FP): Also called a type 1 error, an outcome where the model incorrectly predicts the positive class when it is actually negative.* False Negative (FN): Also called a type 2 error, an outcome where the model incorrectly predicts the negative class when it is actually positive.  The total number of correct predictions for a class go into the expected row for that class value and the predicted column for that class value.In the same way, the total number of incorrect predictions for a class go into the expected row for that class value and the predicted column for that class value.The diagonal elements represent the number of points for which the predicted label is equal to the true label, while off-diagonal elements are those that are mislabelled by the classifier. The higher the diagonal values of the confusion matrix the better, indicating many correct predictions.<jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn<jupyter_output><empty_output><jupyter_text>Example of 2x2 Confusion Matrix logistic regression would, in this synthetic dataset, classify values as either 0 or 1, i.e. class one or two, using the logistic curve.<jupyter_code>data = {'y_Actual':    [0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
        'y_Predicted': [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0]
        }
df = pd.DataFrame(data)
confusion_matrix = pd.crosstab( df['y_Predicted'],df['y_Actual'], rownames=['Predicted'], colnames=['Actual'])
df
confusion_matrix
sns.heatmap(confusion_matrix, annot=True)
plt.show()
# Confusion Matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(df["y_Actual"], df["y_Predicted"])
# Accuracy
from sklearn.metrics import accuracy_score
accuracy_score(df["y_Actual"], df["y_Predicted"])
# Recall
from sklearn.metrics import recall_score
recall_score(df["y_Actual"], df["y_Predicted"])
# Precision
from sklearn.metrics import precision_score
precision_score(df["y_Actual"], df["y_Predicted"])<jupyter_output><empty_output><jupyter_text>Precision is a useful metric in cases where False Positive is a higher concern than False Negatives.Precision is important in music or video recommendation systems, e-commerce websites, etc. Wrong results could lead to customer churn and be harmful to the business.   Recall is a useful metric in cases where False Negative is a higher concern than False Positive.Example : Covid-19<jupyter_code># Method 1: sklearn
from sklearn.metrics import f1_score
f1_score(df["y_Actual"], df["y_Predicted"])
# Method 2: Manual Calculation
recall = recall_score(df["y_Actual"], df["y_Predicted"])
precision = precision_score(df["y_Actual"], df["y_Predicted"])

F1 = 2 * (precision * recall) / (precision + recall)
F1
# Method 3: Classification report
from sklearn.metrics import classification_report
print(classification_report(df["y_Actual"], df["y_Predicted"]))<jupyter_output>precision    recall  f1-score   support

           0       0.88      0.78      0.82         9
           1       0.50      0.67      0.57         3

    accuracy                           0.75        12
   macro avg       0.69      0.72      0.70        12
weighted avg       0.78      0.75      0.76        12
{'fraction_non_alphanumeric': 0.07840649581324537, 'fraction_numerical': 0.022836843440751077, 'mean_word_length': 4.256, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '250431', 'license': 'MIT', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1082, 'n_words': 469, 'path': 'Logistic Regression/multi_Regression/Confusion_Matrix.ipynb', 'repo_name': 'SMSajadi99/Machine-Learning', 'size': '3956'}