Tuesday 7 May 2019

Confusion Matrix Scikit Learn

Hey Guys,

I just started working on one of machine learning project in which i used python with few libraries.

One among them is Scikit-Learn which is amazing tool for doing machine learning techniques like
data preprocessing and data mining.

Official link to the site https://scikit-learn.org/stable/

In this today we talk about confusion matrix:

So , Basically we used some of the classification algorithms of scikit to classify the test data based on trained data.

A basic example if we take from scikit site :

Confusion_Matrix :
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])

Now , consider this example where system predicted values [y_pred] are  [0, 0, 2, 2, 0, 2] but real values are

Here the classes (nothing but the values are 0,1,2)
so Lets put matrix like this


Predicted 0 1 2
True

0 2 0 0

1 0 0 1

2 1 0 2


How it is formed:

Lets take the true values array : [2, 0, 2, 2, 0, 1]. Lets go one by one
for 2 : y_pred array first element is 0 --> its predicted in-correctly. put 1 for (2,0)

for 0 : y_pred array second element is 0 --> its predicted correctly . put 1 for
(0,0)

for 2: y_pred array third element is 2 --> its predicted correctly . put 1 for
(2,2)

for 2: y_pred array fourth element is 2 --> its predicted correctly . increment 1 for (2,2) which becomes 2.

for 0: y_pred array fifth element is 0 --> its predicted correctly . increment 1 for (0,0) which becomes 2.

for 1: y_pred array sixth element is 2 --> its predicted in-correctly . put 1 for


(1,2)


Now you if you see the matrix diagonal, all are correct predicted values.

Hope you got it now. Thanks :)





No comments:

Post a Comment