# Confusion Matrix

#### Latest posts by Renuka Joshi (see all)

- Image Classification Using CNN - May 13, 2018
- Convolutional Neural Networks - May 12, 2018
- Hierarchical Clustering - March 15, 2018

We learned different types of classification in previous tutorials.In those tutorials we counted number of right and wrong predictions manually.This counting by using Confusion Matrix.In this tutorial we will learn about confusion matrix and how this operation can be performed on predictions and actual results using python.

# Confusion Matrix

According to Python documentation,

confusion matrix C is such that CC_{i,j }is equal to the number of observations known to be in group i but predicted to be in group j.Thus in binary classification, the count of true negatives is_{0,0 }, false negatives isC_{1,0}, true positives is C_{1,1}and false positives is C_{0,1}.

Consider the same problem statement as previous tutorials. We wanted to determine whether a user will buy car or not. We can compare predictions against actual results by using confusion_matrix.

## Program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# Logistic Regression # Importling the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('Social_Network_Ads.csv') X = dataset.iloc[:, [2, 3]].values y = dataset.iloc[:, 4].values # Splitting the dataset into the Training set and Test set from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) # Feature Scaling from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) # Fitting Logistic Regression to the Training set from sklearn.linear_model import LogisticRegression classifier = LogisticRegression(random_state = 0) classifier.fit(X_train, y_train) # Predicting the Test set results y_pred = classifier.predict(X_test) # Visualising the Test set results from matplotlib.colors import ListedColormap X_set, y_set = X_test, y_test aranged_ages = np.arange(start = X_set[:, 0].min(), stop = X_set[:, 0].max(), step = 0.01) aranged_salaries = np.arange(start = X_set[:, 1].min(), stop = X_set[:, 1].max(), step = 0.01) X1, X2 = np.meshgrid(aranged_ages, aranged_salaries) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.9, cmap = ListedColormap(('orange', 'blue'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j) plt.title('Logistic Regression (Test set)') plt.xlabel('Age') plt.ylabel('Salary') plt.legend() plt.show() <strong>from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred)</strong> |

As we have already explained whole code till plot.show() in each classification article, we will only focus on confusion matrix.

- we imported
**confusion_matrix()**from**sklearn.metrics** - compared
**y_test**with**y_pred**by using**confusion_matrix(y_test, y_pred)**

Execute whole program and check cm variable in variable explorer.

Here,

- C
_{0,0}: true negatives - C
_{1,0}: false negative - C1,1: true positives
- C
_{0,1 }: false positives

Total 11 predictions are wrong out of 100.Thus,we can use confusion matrix to compare predictions against test results.Happy learning!

Pingback:artificial neural network using keras - theJavaGeek