Scikit learn Confusion Matrix

In this Python tutorial, we will learn How Scikit learn confusion matrix works in Python and we will also cover different examples related to Scikit learn confusion matrix. And, we will cover these topics.

  • Scikit learn confusion matrix
  • Scikit learn confusion matrix example
  • Scikit learn confusion matrix plot
  • Scikit learn confusion matrix accuracy
  • Scikit learn confusion matrix multiclass
  • Scikit learn confusion matrix display
  • Scikit learn confusion matrix labels
  • Scikit learn confusion matrix normalize

Scikit learn confusion matrix

In this section, we will learn about how the Scikit learn confusion matrix works in python.

  • Scikit learn confusion matrix is defined as a technique to calculate the performance of classification.
  • The confusion matrix is also used to predict or summarise the result of the classification problem.

Code:

  • y_true = [2, 0, 0, 2, 0, 1] is used to get the true value.
  • y_pred = [0, 0, 2, 0, 0, 2] is used to get the predicted value.
  • confusion_matrix(y_true, y_pred) is used to evaluate the confusion matrix.
from sklearn.metrics import confusion_matrix
y_true = [2, 0, 0, 2, 0, 1]
y_pred = [0, 0, 2, 0, 0, 2]
confusion_matrix(y_true, y_pred)

Output:

After running the above code, we get the following output in which we can see that the confusion matrix value is printed on the screen.

Scikit learn confusion matrix
Scikit learn confusion matrix

Read: Scikit learn Image Processing

Scikit learn confusion matrix example

In this section, we will learn about how Scikit learn confusion matrix example works in python.

  • Scikit learn confusion matrix example is defined as a technique to summarise the result of the classification.
  • The confusion matrix also predicted the number of correct and incorrect predictions of the classification model.

Code:

In the following code, we will import some libraries from which we can make the confusion matrix.

  • iris = datasets.load_iris() is used to load the iris data.
  • class_names = iris.target_names is used to get the target names.
  • x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0) is used to split the data set into train and test data.
  • classifier = svm.SVC(kernel=”linear”, C=0.02).fit(x_train, y_train) is used to fit the model.
  • ConfusionMatrixDisplay.from_estimator() is used to plot the confusion matrix.
  • print(display.confusion_matrix) is used to print the confusion matrix.
import numpy as num
import matplotlib.pyplot as plot

from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import ConfusionMatrixDisplay


iris = datasets.load_iris()
x = iris.data
y = iris.target
class_names = iris.target_names


x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0)


classifier = svm.SVC(kernel="linear", C=0.02).fit(x_train, y_train)

num.set_printoptions(precision=2)

title_options = [
    ("Confusion matrix, without normalization", None),
    ("Normalized confusion matrix", "true"),
]
for title, normalize in title_options:
    display = ConfusionMatrixDisplay.from_estimator(
        classifier,
        x_test,
        y_test,
        display_labels=class_names,
        cmap=plot.cm.Blues,
        normalize=normalize,
    )
    display.ax_.set_title(title)

    print(title)
    print(display.confusion_matrix)

plot.show()

Output:

In the following output, we can see that the result of the classification is summarised on the screen with help of a confusion matrix.

Scikit learn confusion matrix example
Scikit learn confusion matrix example

Read: Scikit learn non-linear [Complete Guide]

READ:  Python Tkinter Entry - How to use

Scikit learn confusion matrix plot

In this section, we will learn about how Scikit learn confusion matrix plot in python.

  • Scikit learn confusion matrix plot is used to plot the graph on the screen to summarise the result of the model.
  • It is used to plot the graph to predict the number of correct or incorrect predictions of the model.

Code:

In the following code, we will import some libraries from which we can plot the confusion matrix on the screen.

  • x, y = make_classification(random_state=0) is used to make classification.
  • x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0) is used to split the data set into train and test set.
  • classifier.fit(x_train, y_train) is used to fit the model.
  • plot_confusion_matrix(classifier, x_test, y_test) is used to plot the confusion matrix on the screen.
import matplotlib.pyplot as plot
from sklearn.datasets import make_classification
from sklearn.metrics import plot_confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
x, y = make_classification(random_state=0)
x_train, x_test, y_train, y_test = train_test_split(
         x, y, random_state=0)
classifier = SVC(random_state=0)
classifier.fit(x_train, y_train)
SVC(random_state=0)
plot_confusion_matrix(classifier, x_test, y_test)  
plot.show()

Output:

After running the above code, we get the following output in which we can see that the confusion matrix is plotted on the screen.

Scikit learn confusion matrix plot
Scikit learn confusion matrix plot

Read: Scikit learn KNN Tutorial

Scikit learn confusion matrix accuracy

In this section, we will learn about Scikit learn confusion matrix accuracy of the model in python.

Scikit learn confusion matrix accuracy is used to calculate the accuracy of the matrix how accurate our model result.

Code:

In the following code, we will import some libraries from which we can calculate the accuracy of the model.

  • y_pred = [1, 3, 2, 0] is used to predict the predicted value.
  • y_true = [0, 1, 2, 3] is used to predict the true value.
  • accuracy_score(y_true, y_pred) is used to predict the accuracy score.
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
y_pred = [1, 3, 2, 0]
y_true = [0, 1, 2, 3]
accuracy_score(y_true, y_pred)
accuracy_score(y_true, y_pred, normalize=False)
import numpy as num
accuracy_score(num.array([[0, 1], [1, 1]]), num.ones((2, 2)))

Output:

After running the above code, we get the following output in which we can see that the confusion matrix accuracy score is printed on the screen.

Scikit learn confusion matrix accuracy
Scikit learn confusion matrix accuracy

Read: Scikit learn Sentiment Analysis

Scikit learn confusion matrix multiclass

In this section, we will learn about how scikit learn confusion matrix multiclass works in python.

Scikit learn confusion matrix multi-class is defined as a problem of classifying illustration of one of the three or more classes.

Code:

In the following code, we will import some libraries from which we can make a confusion matrix multiclass.

  • df = pd.read_csv(“IRIS.csv”) is used to load the dataset.
  • df.dtypes is used to select the types of data.
#importing packages
import pandas as pds
import numpy as num
import seaborn as sb
import matplotlib.pyplot as plot
 
df = pd.read_csv("IRIS.csv")
df.head()
df.dtypes
Scikit learn confusion matrix multi class dataset
Scikit learn confusion matrix multi-class dataset
  • x = df.drop([‘species’], axis=1) is used to separating independent variable and dependent variable.
  • print(x.shape) is used to print the shape of the dataset.
#Separating independant variable and dependent variable("Species")
x = df.drop(['species'], axis=1)
y = df['species']

print(x.shape)

print(y.shape)
scikit learn confusion matrix multiclass separating dependent or independent variable
scikit learn confusion matrix multiclass separating dependent or independent variable
  • x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0) is used to split the dataset into train and test set.
  • print(x_train.shape) is used to print the shape of the train set.
  • print(x_test.shape) is used to print the shape of the test set.
# Splitting the dataset to Train and test
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
Scikit learn confusion matrix multi class train test data split
Scikit learn confusion matrix multi-class train test data split
  • classifier = SVC(kernel = ‘linear’).fit(x_train,y_train) is used to training the classifier from x train and y train.
  • cm = confusion_matrix(y_test, y_pred) is used to creating the confusion matrix.
  • cm_df = pd.DataFrame() is used to create the data frame.
  • plot.figure(figsize=(5,4)) is used to plot the figure.
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix

classifier = SVC(kernel = 'linear').fit(x_train,y_train)
classifier.predict(x_train)

y_pred = classifier.predict(X_test)

cm = confusion_matrix(y_test, y_pred)

cm_df = pd.DataFrame(cm,
                     index = ['SETOSA','VERSICOLR','VIRGINICA'], 
                     columns = ['SETOSA','VERSICOLR','VIRGINICA'])

plot.figure(figsize=(5,4))
sb.heatmap(cm_df, annot=True)
plot.title('Confusion Matrix')
plot.ylabel('Actal Values')
plot.xlabel('Predicted Values')
plot.show()
Scikit learn confusion matrix multi class
Scikit learn confusion matrix multiclass

Read: Scikit learn Gradient Descent

READ:  Module 'keras.backend' has no attribute 'set_session'

Scikit learn confusion matrix display

In this section, we will learn about how Scikit learn confusion matrix display works in python.

Scikit learn confusion matrix display is defined as a matrix in which i,j is equal to the number of observations are forecast to be in a group.

Code:

In the following code, we will learn to import some libraries from which we can see how the confusion matrix is displayed on the screen.

  • x, y = make_classification(random_state=0) is used to make classification.
  • x_train, x_test, y_train, y_test = train_test_split(x, y,random_state=0) is used to split the dataset into train and test data.
  • classifier.fit(x_train, y_train) is used to fit the data.
  • predictions = classifier.predict(x_test) is used to predict the data.
  • display=ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=classifier.classes_) is used to display the confusion matrix.
  • display.plot() is used to plot the matrix.
import matplotlib.pyplot as plot
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
x, y = make_classification(random_state=0)
x_train, x_test, y_train, y_test = train_test_split(x, y,
                                                    random_state=0)
classifier = SVC(random_state=0)
classifier.fit(x_train, y_train)
SVC(random_state=0)
predictions = classifier.predict(x_test)
cm = confusion_matrix(y_test, predictions, labels=classifier.classes_)
display = ConfusionMatrixDisplay(confusion_matrix=cm,
                               display_labels=classifier.classes_)
display.plot()

plot.show()

Output:

After running the above code, we get the following output in which we can see that a confusion matrix is displayed on the screen.

Scikit learn confusion matrix display
Scikit learn confusion matrix display

Read: Scikit learn Classification Tutorial

Scikit learn confusion matrix labels

In this section, we will learn how Scikit learn confusion matrix labels works in python.

Scikit learn confusion matrix label is defined as a two-dimension array that contrasts a predicted group of labels with true labels.

Code:

In the following code, we will import some libraries to know how scikit learn confusion matrix labels works.

  • y_true = num.array([[1, 0, 0],[0, 1, 1]]) is used to collect the true labels in the array.
  • y_pred = num.array([[1, 0, 1],[0, 1, 0]]) is used to collect the predicted labelsin the array.
  • multilabel_confusion_matrix(y_true, y_pred) is used to get multilabel confusion matrix.
import numpy as num
from sklearn.metrics import multilabel_confusion_matrix
y_true = num.array([[1, 0, 0],
                    [0, 1, 1]])
y_pred = num.array([[1, 0, 1],
                   [0, 1, 0]])
multilabel_confusion_matrix(y_true, y_pred)

Output:

After running the above code, we get the following output in which we can see that the confusion matrix labels are printed on the screen.

Scikit learn confusion matrix labels
Scikit learn confusion matrix labels

Read: Scikit learn Hyperparameter Tuning

READ:  How to count duplicates in Pandas dataframe in Python [4 Methods]

Scikit learn confusion matrix normalize

In this section, we will learn about how scikit learn confusion matrix normalize works in python.

Scikit learn confusion matrix normalize is defined as a process that represents one sample is present in each group.

Code:

In the following code, we will import some libraries from which we can normalize the matrix.

  • iris = datasets.load_iris() is used to load the data.
  • x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0) is used use to split the dataset into train and test data.
  • classifier = svm.SVC(kernel=’linear’, C=0.01) is used as a classifier.
  • y_pred = classifier.fit(x_train, y_train).predict(x_test) is used to fit the model.
  • cm = confusion_matrix(y_true, y_pred) is used to compute the confusion matrix.
  • classes = classes[unique_labels(y_true, y_pred)] is used of label that appear in the data.
  • figure, axis = plot.subplots() is used to plot the figure on the screen.
  • plot.setp(axis.get_xticklabels(), rotation=45, ha=”right”,rotation_mode=”anchor”) is used to set the alignment and rotate the ticks.
  • plot_confusion_matrix(y_test, y_pred, classes=class_names, normalize=True, title=’Normalized confusion matrix’) is used to plot the normalized confusion matrix.
import numpy as num
import matplotlib.pyplot as plot
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels


iris = datasets.load_iris()
x_digits = iris.data
y = iris.target
class_names = iris.target_names

x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0)


classifier = svm.SVC(kernel='linear', C=0.01)
y_pred = classifier.fit(x_train, y_train).predict(x_test)


def plot_confusion_matrix(y_true, y_pred, classes,
                          normalize=False,
                          title=None,
                          cmap=plot.cm.Blues):
   
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        
    cm = confusion_matrix(y_true, y_pred)

    classes = classes[unique_labels(y_true, y_pred)]
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, num.newaxis]
        print("Normalized confusion matrix")
   

    print(cm)

    figure, axis = plot.subplots()
    im = axis.imshow(cm, interpolation='nearest', cmap=cmap)
    axis.figure.colorbar(im, ax=axis)

    axis.set(xticks=num.arange(cm.shape[1]),
           yticks=num.arange(cm.shape[0]),

           xticklabels=classes, yticklabels=classes,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')


    plot.setp(axis.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            axis.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="cyan" if cm[i, j] > thresh else "red")
    figure.tight_layout()
    return axis

plot_confusion_matrix(y_test, y_pred, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')

plot.show()

Output:

In the following code, we will see a normalized confusion matrix array is created, and also a normalized confusion matrix graph is plotted on the screen.

Scikit learn confusion matrix normalized
Scikit learn confusion matrix normalized

Also, take a look at some more Scikit learn tutorials.

  • Scikit learn Linear Regression
  • Scikit learn Feature Selection
  • Scikit learn Ridge Regression
  • Scikit learn Random Forest

So, in this tutorial we discussed Scikit learn confusion matrix and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Scikit learn confusion matrix
  • Scikit learn confusion matrix example
  • Scikit learn confusion matrix plot
  • Scikit learn confusion matrix accuracy
  • Scikit learn confusion matrix multiclass
  • Scikit learn confusion matrix display
  • Scikit learn confusion matrix labels
  • Scikit learn confusion matrix normalize