How to generate classification report and confusion matrix in Python?
MODEL SELECTION

How to generate classification report and confusion matrix in Python?

How to generate classification report and confusion matrix in Python?

This recipe helps you generate classification report and confusion matrix in Python

0
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
In [1]:
## How to generate classification report and confusion matrix in Python
def Snippet_138():
    print()
    print(format('How to generate classification report & confusion matrix in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    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

    # Load data
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    # Create list of target class names
    class_names = iris.target_names

    # Create training and test set
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

    # Create DecisionTree model
    classifier = DecisionTreeClassifier()

    # Train model and make predictions
    y_hat = classifier.fit(X_train, y_train).predict(X_test)

    # Create a classification report
    print()
    print(classification_report(y_test, y_hat, target_names=class_names))

    # Create a confusion matrix
    print()
    print(confusion_matrix(y_test, y_hat))

Snippet_138()
********How to generate classification report & confusion matrix in Python********

              precision    recall  f1-score   support

      setosa       1.00      1.00      1.00        15
  versicolor       1.00      0.80      0.89        15
   virginica       0.87      1.00      0.93        20

   micro avg       0.94      0.94      0.94        50
   macro avg       0.96      0.93      0.94        50
weighted avg       0.95      0.94      0.94        50


[[15  0  0]
 [ 0 12  3]
 [ 0  0 20]]

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.