从函数调用时显示混淆矩阵

时间:2019-04-09 08:36:43

标签: python pandas function machine-learning confusion-matrix

我有一个函数可以从scikit learning导入一个随机森林分类器,我将其与数据拟合,最后我要显示准确性,kappa和混淆矩阵。除打印混淆矩阵外,所有工作均可。我没有收到任何错误,但是混乱矩阵无法显示。

我尝试调用print(cm)并可以正常工作,但是它并不能以通常的pandas数据框样式打印,这正是我想要的。

这是代码

def rf_clf(X, y, test_size = 0.3, random_state = 42):
    """This function splits the data into train and test and fits it in a random forest classifier 
    to the data provided, analysing its errors (Accuracy and Kappa). Also as this is classification,
    the function will output a confusion matrix"""

    #Split data in train and test, as well as predictors (X) and targets, (y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state, stratify=y)

    #import random forest classifier
    base_model = RandomForestClassifier(random_state=random_state)

    #Train the model
    base_model.fit(X_train,y_train)

    #make predictions on test set
    y_pred=base_model.predict(X_test)

    #Print Accuracy and Kappa
    print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
    print("Kappa:",metrics.cohen_kappa_score(y_test, y_pred))

    #create confusion matrix
    labs = [y_test[i][0] for i in range(len(y_test))]
    cm = pd.DataFrame(confusion_matrix(labs, y_pred))
    cm #here is the issue. Kinda works with print(cm)

1 个答案:

答案 0 :(得分:1)

  1. 首先从sklearn导入指标。

    from sklearn import metrics
    
  2. 当您要显示混淆矩阵时,请使用它。

    # Get and show confussion matrix
    cm = metrics.confusion_matrix(y_test, y_pred)
    print(cm)
    

使用此方法,您应该以原始文本查看混淆矩阵。

如果您要用颜色显示混淆矩阵,请以其他方式进行:

  1. 导入

    from sklearn.metrics import confusion_matrix
    import pandas as pd
    import seaborn as sns; sns.set()
    
  2. 以这种方式使用它:

    cm = confusion_matrix(y_test, y_pred)
    cmat_df = pd.DataFrame(cm, index=class_names, columns=class_names)
    ax = sns.heatmap(cmat_df, square=True, annot=True, cbar=False)
    ax.set_xlabel('Predicción')
    ax.set_ylabel('Real')`
    
  3. 希望最好!