How to Plot a Confusion Matrix from a K-Fold Cross-Validation

Viewed 31

How can I plot a Confusion Matrix for this model below? I used 10 Fold Stratified K-Fold Cross-Validation method. Target value has two variables which is 0 and 1.

model = GradientBoostingClassifier()
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
score = cross_val_score(model, X, y, cv= skf, scoring="accuracy")

I tried this code but it gives me errors.

import numpy as np
import copy as cp
import matplotlib.pyplot as plt

import seaborn as sns
from typing import Tuple
from sklearn.metrics import confusion_matrix

def cross_val_predict(model, skf : StratifiedKFold, X : np.array, y : np.array) -> Tuple[np.array, np.array, np.array]:

    model_ = cp.deepcopy(model)
    
    no_classes = len(np.unique(y))
    
    actual_classes = np.empty([0], dtype=int)
    predicted_classes = np.empty([0], dtype=int)
    predicted_proba = np.empty([0, no_classes]) 

    for train_ndx, test_ndx in skf.split(X):

        train_X, train_y, test_X, test_y = X[train_ndx], y[train_ndx], X[test_ndx], y[test_ndx]

        actual_classes = np.append(actual_classes, test_y)

        model_.fit(train_X, train_y)
        predicted_classes = np.append(predicted_classes, model_.predict(test_X))

        try:
            predicted_proba = np.append(predicted_proba, model_.predict_proba(test_X), axis=0)
        except:
            predicted_proba = np.append(predicted_proba, np.zeros((len(test_X), no_classes), dtype=float), axis=0)

    return actual_classes, predicted_classes, predicted_proba

def plot_confusion_matrix(actual_classes : np.array, predicted_classes : np.array, sorted_labels : list):

    matrix = confusion_matrix(actual_classes, predicted_classes, labels=sorted_labels)
    
    plt.figure(figsize=(12.8,6))
    sns.heatmap(matrix, annot=True, xticklabels=sorted_labels, yticklabels=sorted_labels, cmap="Blues", fmt="g")
    plt.xlabel('Predicted'); plt.ylabel('Actual'); plt.title('Confusion Matrix')

    plt.show()

actual_classes, predicted_classes, _ = cross_val_predict(model, skf, X.to_numpy(), y.to_numpy())
plot_confusion_matrix(actual_classes, predicted_classes, ["Positive", "Neutral", "Negative"])
0 Answers
Related