Reputation: 431
I am working on a logistic regression with just some of the classifiers of the complete data set. It works fine, I get a good confusion matrix, but I can't get the plot to work. I'm using Python 3.6 in a Jupyter Notebook, all packages I have imported I verified are up to date.
Here is where I get and process the data set:
import itertools
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import os
os.chdir('C:/Users/theca/Desktop/Rstuff')
data = pd.read_csv('telco_customer_churn.csv')
categorical = data[["gender", "SeniorCitizen"]]
df = data[["tenure", "MonthlyCharges","Churn"]]
dummies = pd.get_dummies(categorical)
df_new = dummies.join(df)
df_new.head()
X = df_new.iloc[:,[0,1,2,3,4]]
y = df_new.iloc[:,[5]]
#Splitting the data set
from sklearn.model_selection import train_test_split
X_train,X_test, y_train,y_test = train_test_split(X,y, test_size = 0.25,random_state = 0)
#Fitting logistic regression
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train,np.ravel(y_train))
#predicting the test results
y_pred = classifier.predict(X_test)
#making the confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,y_pred)
The confusion matrix:
[[1164 134]
[ 250 213]]
Now I'm trying to use a method I found for sklearn at http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
Here is how I adapted it:
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
Then I tried to generate the graphic:
plt.figure()
plot_confusion_matrix(cm, classes=df_new[["Churn"]],
title='Confusion matrix, without normalization')
My graphic looked like this, with no data on it:
I realized that this method is not using a pandas dataframe, but maybe a numpy array? How would I make it display properly?
Thanks!
Upvotes: 0
Views: 4410
Reputation: 141
This code can also be helpful.
import numpy as np
import matplotlib.pyplot as plt
import itertools
from pycm import ConfusionMatrix
def plot_confusion_matrix(cm,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function modified to plots the ConfusionMatrix object.
Normalization can be applied by setting `normalize=True`.
Code Reference :
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
plt_cm = []
for i in cm.classes :
row=[]
for j in cm.classes:
row.append(cm.table[i][j])
plt_cm.append(row)
plt_cm = np.array(plt_cm)
if normalize:
plt_cm = plt_cm.astype('float') / plt_cm.sum(axis=1)[:, np.newaxis]
plt.imshow(plt_cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(cm.classes))
plt.xticks(tick_marks, cm.classes, rotation=45)
plt.yticks(tick_marks, cm.classes)
fmt = '.2f' if normalize else 'd'
thresh = plt_cm.max() / 2.
for i, j in itertools.product(range(plt_cm.shape[0]), range(plt_cm.shape[1])):
plt.text(j, i, format(plt_cm[i, j], fmt),
horizontalalignment="center",
color="white" if plt_cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Predict')
and then this function can be used as follows:
cm = ConfusionMatrix(matrix={0:{0:13,1:0,2:0},1:{0:0,1:10,2:6},2:{0:0,1:0,2:9}})
plt.figure()
plot_confusion_matrix(cm,title='cm')
plt.figure()
plot_confusion_matrix(cm,title='cm(Normalized)',normalize=True)
plt.show()
for plot using seaborn and pandas see here
Upvotes: 0
Reputation: 21
You can use seaborn to plot the confusion matrix graphic. I am passing the true and predicted labels to the function. Here's the code:
def plot_confusion_matrix(true, pred):
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(true, pred, labels=[1, 0])
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
cm_df = pd.DataFrame(confusion_matrix,
index = ['1', '0'],
columns = ['1', '0'])
ax = sns.heatmap(cm_df, fmt = 'd' , cmap="YlGnBu", cbar = False, annot=True)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.title('Confusion Matrix')
plt.show()
Upvotes: 2