Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Calculating Precision and Recall for Multiclass Classification Using Confusion Matrix
Next article icon

Compute Classification Report and Confusion Matrix in Python

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Classification Report and Confusion Matrix are used to check machine learning model's performance during model development. These help us understand the accuracy of predictions and tells areas of improvement. In this article, we will learn how to compute these metrics in Python using a simple example.

Understanding the Classification Report and Confusion Matrix

The Classification Report summarizes the performance of a classification model. It includes key metrics such as:

  1. Precision : Measures the accuracy of positive predictions.
  2. Recall : Indicates how many actual positives were correctly identified.
  3. F1-Score : Balances precision and recall into a single score.
  4. Support : Shows the number of samples for each class.

These metrics help us understand how well the model performs for each class.

Confusion Matrix is a table that compares the model's predictions against the actual values. It highlights where the model succeeds and where it makes mistakes. For a binary classification problem like "Yes" or "No" the confusion matrix looks like this:


PREDICTED YES

PREDICTED NO

Actual Yes

True Positive

False Negative

Actual No

False Negative

True Negative

  • True Positive (TP) : Correctly predicted as "Yes."
  • False Negative (FN) : Incorrectly predicted as "No" when it was actually "Yes."
  • False Positive (FP) : Incorrectly predicted as "Yes" when it was actually "No."
  • True Negative (TN) : Correctly predicted as "No".

This matrix helps identify patterns in errors and evaluate overall accuracy.

Step-by-Step Guide to Compute Metrics in Python

We will use Python and the scikit-learn library to compute the Classification Report and Confusion Matrix.

Step 1: Import Necessary Libraries

We need to import the required library scikit learn.

Python
from sklearn.metrics import classification_report, confusion_matrix from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris 

Step 2: Load Dataset

We’ll use the Iris dataset which contains data about flowers and their species.

Python
data = load_iris() X = data.data   y = data.target  

Step 3: Split Data into Training and Testing Sets

To evaluate the model on unseen data we split the dataset into 70% training data and 30% testing data.

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

Step 4: Train a Model

We’ll use logistic regression for classification.

Python
model = LogisticRegression(max_iter=200) model.fit(X_train, y_train) 

Output:

Model_training
Model Training

Step 5: Make Predictions

After training the model we use it to predict labels for the test data.

Python
y_pred = model.predict(X_test) 

Step 6: Compute the Confusion Matrix

Now let’s calculate the confusion matrix to analyze the results.

Python
conf_matrix = confusion_matrix(y_test, y_pred) print("Confusion Matrix:") print(conf_matrix) 

Output:

confusion_matrix
Confusion matrix

This means:

  • For class 0 (Setosa) all predictions were correct.
  • For class 1 (Versicolor) there was 1 false negative.
  • For class 2 (Virginica) there was 1 false positive.

Step 7: Generating the Classification Report

Finally let’s generate the classification report to get detailed performance metrics.

Python
class_report = classification_report(y_test, y_pred, target_names=data.target_names) print("Classification Report:") print(class_report) 

Output:

classification_report
Classification report

This above classification report shows that the model performs good with high precision, recall and F1-scores across all classes. You can easily compute Classification Report and Confusion Matrix using the scikit-learn Python library.


Next Article
Calculating Precision and Recall for Multiclass Classification Using Confusion Matrix
author
pintusaini
Improve
Article Tags :
  • Machine Learning
  • Geeks Premier League
  • AI-ML-DS
  • Geeks-Premier-League-2022
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

  • Evaluation Metrics For Classification Model in Python
    Classification is a supervised machine-learning technique that predicts the class label based on the input data. There are different classification algorithms to build a classification model, such as Stochastic Gradient Classifier, Support Vector Machine Classifier, Random Forest Classifier, etc. To
    7 min read
  • Calculating Precision and Recall for Multiclass Classification Using Confusion Matrix
    Multiclass classification is a common problem in machine learning where a model is required to predict one of several predefined categories. Evaluating the performance of such models can be complex, especially when dealing with imbalanced datasets. Two essential metrics for evaluating multiclass cla
    6 min read
  • How to Create simulated data for classification in Python?
    In this article, we are going to see how to create simulated data for classification in Python.  We will use the sklearn library that provides various generators for simulating classification data. Single Label Classification Here we are going to see single-label classification, for this we will use
    2 min read
  • Python - tensorflow.math.confusion_matrix()
    TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.  confusion_matrix() is used to find the confusion matrix from predictions and labels. Syntax: tensorflow.math.confusion_matrix( labels, predictions, num_classes, weights
    2 min read
  • Create a correlation Matrix using Python
    A Correlation matrix is a table that shows how different variables are related to each other. Each cell in the table displays a number i.e. correlation coefficient which tells us how strongly two variables are together. It helps in quickly spotting patterns, understand relationships and making bette
    3 min read
  • Classification of Text Documents using Naive Bayes
    In natural language processing and machine learning Naïve Bayes approach is a popular method for classifying text documents. This method classifies documents into predetermined types based on the likelihood of a word occurring by using the concepts of the Bayes theorem. This article aims to implemen
    5 min read
  • Classifying Data With Pandas In Python
    Pandas is a widely used Python library renowned for its prowess in data manipulation and analysis. Its core data structures, such as DataFrame and Series, provide a powerful and user-friendly interface for handling structured data. This makes Pandas an indispensable tool for tasks like classifying o
    5 min read
  • Pandas Functions in Python: A Toolkit for Data Analysis
    Pandas is one of the most used libraries in Python for data science or data analysis. It can read data from CSV or Excel files, manipulate the data, and generate insights from it. Pandas can also be used to clean data, filter data, and visualize data. Whether you are a beginner or an experienced pro
    6 min read
  • How to produce a confusion matrix and find the misclassification rate of the Naive Bayes Classifier in R?
    Producing a confusion matrix and calculating the misclassification rate of a Naive Bayes Classifier in R involves a few straightforward steps. In this guide, we'll use a sample dataset to demonstrate how to interpret the results. Understanding Confusion MatrixA confusion matrix is a table that descr
    3 min read
  • sklearn.metrics.max_error() function in Python
    The max_error() function computes the maximum residual error. A metric that captures the worst-case error between the predicted value and the true value. This function compares each element (index wise) of both lists, tuples or data frames and returns the count of unmatched elements. Syntax: sklearn
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences