Search code examples
python-3.xfor-loopexport-to-csv

Save classification report & confusion matrix of for loop into csv file


I need to have 10 results in my CSV file but it shows only one. I looked through some questions posted and it said might be my previous iteration being covered.

How should I edit my code in order to get 10 repetitions in my CSV file?

for x in range (10):
    from sklearn.metrics import classification_report
    report = classification_report(Y_test, Y_pred, output_dict=True)
    CR = pd.DataFrame(report).transpose()
    CR.to_csv('LR_CR.csv')

    from sklearn.metrics import confusion_matrix
    matrix = confusion_matrix(Y_test, Y_pred)
    CM = pd.DataFrame(matrix).transpose()
    CM.to_csv('LR_CM.csv')


Output
              precision    recall  f1-score    support
0              0.421053  0.444444  0.432432  18.000000
1              0.777778  0.760870  0.769231  46.000000
accuracy       0.671875  0.671875  0.671875   0.671875
macro avg      0.599415  0.602657  0.600832  64.000000
weighted avg   0.677449  0.671875  0.674506  64.000000

    0   1
0   8  14
1  10  32    

             precision    recall  f1-score  support
0              0.625000  0.277778  0.384615    18.00
1              0.767857  0.934783  0.843137    46.00
accuracy       0.750000  0.750000  0.750000     0.75
macro avg      0.696429  0.606280  0.613876    64.00
weighted avg   0.727679  0.750000  0.714178    64.00

    0   1
0   5   3
1  13  43

Solution

  • What is happening here is that you are overwriting your CSV file in each iteration of the loop. If you want to have 10 separate CSV files, you need to name each one differently which can be achieved by using f-strings

    for i in range(10):
        pd.to_csv(f'some_name_{i}.csv')
    

    Or if you need all the results in a single CSV file, then just append to the existing file

    df.to_csv('existing.csv', mode='a', index=False, header=False)