Classification report sklearn

Hi All, I want to discuss Classification report from sklearn in this post.


It is important to keep different metrics in mind when we are both training and evaluating models. Depending on the context, certain metrics will make more sense than others. The best thing is that the Classification report summarize it very well in a single tabular structure.

Scikit-learn does provide a convenience report when working on classification problems to give us a quick idea of the accuracy of a model using a number of measures.

The classification_report() function displays the precision, recall, f1-score and support for each class.

The classification_report function builds a text report showing the main classification metrics.

Lets see the same using an example:


# Load libraries
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

Load Iris Flower Data

# Load data
iris = datasets.load_iris()

# Create feature matrix
X =

# Create target vector
y =

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

Create Training And Test Sets

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

Train A Logistic Regression Model

# Create logistic regression
classifier = LogisticRegression()

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

Generate Report

# Create a classification report
print(classification_report(y_test, y_hat, target_names=class_names))
             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        13
 versicolor       1.00      0.62      0.77        16
  virginica       0.60      1.00      0.75         9

avg / total       0.91      0.84      0.84        38

Note: Support refers to the number of observations in each class.

Happy Learning




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s