Generate classification report and confusion matrix in Python

In this recipe you will generate classification report and confusion matrix, also you will learn what are the required libraries for classification report generation and how to perform train test split on a dataset in Python

Recipe Objective

While using a classification problem we need to use various metrics like precision, recall, f1-score, support or others to check how efficient our model is working.

For this we need to compute there scores by classification report and confusion matrix. So in this recipie we will learn how to generate classification report and confusion matrix in Python.

This data science python source code does the following:
1. Imports necessary libraries and dataset from sklearn
2. performs train test split on the dataset
3. Applies DecisionTreeClassifier model for prediction
4. Prepares classification report for the output

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library

from sklearn import datasets from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report, confusion_matrix

We have imported datasets to use the inbuilt dataframe , DecisionTreeClassifier, train_test_split, classification_report and confusion_matrix.

Step 2 - Setting up the Data

Here we have used datasets to load the inbuilt wine dataset and we have created objects X and y to store the data and the target value respectively. wine = datasets.load_wine() X = wine.data y = wine.target We are creating a list of target names and We are using train_test_split is used to split the data into two parts, one is train which is used to train the model and the other is test which is used to check how our model is working on unseen data. Here we are passing 0.3 as a parameter in the train_test_split which will split the data such that 30% of data will be in test part and rest 70% will be in the train part.

class_names = wine.target_names X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

Step 3 - Training the model

Here we are using DecisionTreeClassifier to predict as a classification model and training it on the train data. After that predicting the output of test data. classifier_tree = DecisionTreeClassifier() y_predict = classifier_tree.fit(X_train, y_train).predict(X_test)

Explore the Must Know Python Libraries for Data Science and Machine Learning.

Step 5 - Creating Classification Report and Confusion Matrix

Let us first have a look on the parameters of Classification Report:

  • y_true : In this parameter we have to pass the true target values of the data.
  • y_pred : It this parameter we have to pass the predicted output of model.
  • target_names : In this parameter we have to pass the names of target.

For Confusion Matrix there are two parameters test and predicted values of the data. print(classification_report(y_test, y_predict, target_names=class_names)) print(confusion_matrix(y_test, y_predict)) So the output comes as

              precision    recall  f1-score   support

     class_0       0.95      0.95      0.95        19
     class_1       0.95      0.95      0.95        21
     class_2       0.95      0.95      0.95        19

   micro avg       0.95      0.95      0.95        59
   macro avg       0.95      0.95      0.95        59
weighted avg       0.95      0.95      0.95        59

[[18  1  0]
 [ 0 20  1]
 [ 1  0 18]]

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .