Python : How to get the sum of confusion matrix and average classification report from kfold cross validation

Viewed 279

I'm using 10 fold cross validation and trying to get the sum of confusion matrix and also find the mean classification report of each folds. Using this answer , I could do the latter, but then I found out you can't make a confussion matrix with cross_val_score like explained here.

My question is how can I achieve both? finding the sum of confusion matrix and print only the mean classification report

my code look like this so far :

originalclass = []
predictedclass = []

model = MultinomialNB()
X = df_gr_traintest.drop(['Sentimen'], axis=1)
y = df_gr_traintest.Sentimen

def classification_report_with_accuracy_score(y_train, y_test):
    originalclass.extend(y_train)
    predictedclass.extend(y_test)
    return accuracy_score(y_train, y_test) # return accuracy score

classif_report = cross_val_score(model, X, y, cv=10, 
                                 scoring=make_scorer(classification_report_with_accuracy_score))

# Average values in classification report for all folds in a K-fold Cross-validation  
print(classification_report(originalclass, predictedclass)) 

output :

precision    recall  f1-score   support

           0       0.49      0.41      0.45       100
           1       0.48      0.56      0.52       100
           2       0.57      0.56      0.56       100

    accuracy                           0.51       300
   macro avg       0.51      0.51      0.51       300
weighted avg       0.51      0.51      0.51       300
0 Answers
Related