0% found this document useful (0 votes)
4 views2 pages

Confusion Matrix Visualization in Python

Uploaded by

v57r2jnyk2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Confusion Matrix Visualization in Python

Uploaded by

v57r2jnyk2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Aim: Compute Confusion matrix and visualize it

using matplotlib to find quality of data


accurately on the given dataset.
# Import required libraries
import numpy as np
import [Link] as plt
from [Link] import confusion_matrix, accuracy_score,
ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from [Link] import load_iris
from sklearn.linear_model import LogisticRegression

# 1. Load dataset (example: Iris dataset)


data = load_iris()
X = [Link]
y = [Link]

# 2. Split into training and test sets


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)

# 3. Train a model (Logistic Regression for demo)


model = LogisticRegression(max_iter=200)
[Link](X_train, y_train)

# 4. Predict on test data


y_pred = [Link](X_test)

# 5. Compute Confusion Matrix


cm = confusion_matrix(y_test, y_pred)
acc = accuracy_score(y_test, y_pred)

print("Confusion Matrix:\n", cm)


print(f"Accuracy: {acc:.2f}")

# 6. Visualize using Matplotlib


disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=data.target_names)
[Link](cmap=[Link])
[Link]("Confusion Matrix - Iris Dataset")
[Link]()
How it works:

1. Loads the Iris dataset (you can replace it with your dataset).
2. Splits the data into train/test sets.
3. Fits a simple Logistic Regression model.
4. Predicts and generates the Confusion Matrix.
5. Calculates accuracy to check model quality.
6. Visualizes the matrix using Matplotlib with color mapping.

You might also like