混淆矩阵图形不显示数据

时间:2018-09-10 07:01:09

标签: python-3.x dataframe data-mining logistic-regression

我正在使用完整数据集的一些分类器进行逻辑回归。它工作正常,我得到了一个很好的混淆矩阵,但是我无法使情节起作用。我在Jupyter笔记本中使用Python 3.6,已验证并导入的所有软件包都是最新的。

这是我获取和处理数据集的地方:

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()

df_new.head() output

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)

混乱矩阵:

[[1164 134]

[250 213]]

现在,我正在尝试使用在sklearn中找到的方法 http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

这是我的适应方法:

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')

然后我尝试生成图形:

plt.figure()
plot_confusion_matrix(cm, classes=df_new[["Churn"]],
                      title='Confusion matrix, without normalization')

我的图形看起来像这样,上面没有数据:

output

我意识到这种方法没有使用pandas数据框,而是使用了一个numpy数组?我如何使其正确显示?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用seaborn绘制混淆矩阵图形。我将真实的和预测的标签传递给函数。这是代码:

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()

答案 1 :(得分:0)

此代码也可能有帮助。

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')

然后可以按以下方式使用此功能:

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()

有关使用 seaborn pandas 的情节,请参见here