简体   繁体   中英

Python - How to create confusion matrix statistics using python pandas crosstab

Hereunder is my Phyton script that generates the following confusion matrix

在此处输入图片说明

# /usr/bin/python -tt

from __future__ import division
import csv
import os
import pandas as pd


#----------------------------------------------------------------------------
def get_tcp_variant(filepath):
    def tcp_congestion_variant(beta):
        print('predict({}; abs({})'.format(beta, abs(beta)))
        if (beta>0.61 and beta<=0.75):
            return "Cubic"
        if (beta>=0.45 and beta<0.61):
            return "Reno"
        if (beta>0.75 and beta<=0.99):
            return "BIC"

        return "None"
#----------------------------------------------------------------------------

    with open(filepath, "r") as csvfile:
        ff = csv.reader(csvfile)

        beta_values = []
        cwnd_loss = 0
        for current_cwnd, col2 in ff:
            value = int(current_cwnd)
            if value >= cwnd_loss:
                cwnd_loss = value
            else:
                beta_value = int(current_cwnd)/cwnd_loss
                beta_value=(round(beta_value,2))
                beta_values.append(beta_value)
                cwnd_loss = value

    return tcp_congestion_variant(sum(beta_values)/len(beta_values))

print ("*********************************************")
print ("Confusion matrix ")
print ("*********************************************")
matrix = {'actual':[], 'predict':[]}
path = './csv_files'

#----------------------------------------------------------------------------
def get_variant_predict(filename):
    if 'cubic' in filename:
        return 'Cubic'
    if 'reno' in filename:
        return "Reno"
    if 'bic' in filename:
        return "BIC"
    else:
        return filename [0]
#----------------------------------------------------------------------------

for filename in os.listdir(path):
    #matrix['predict'].append(filename[:4])
    matrix['predict'].append(get_variant_predict(filename))
    matrix['actual'].append(get_tcp_variant(os.path.join(path, filename)))

data_frame = pd.crosstab(pd.Series(matrix['actual'], name='Actual'),
                 pd.Series(matrix['predict'], name='    Predicted'))
                 #,margins=True) # To add "All"
print (" ")

print(data_frame)

How can we add a confusion matrix statistics (for example: accuracy to Python pandas crosstab? If we manually do it, the accuracy will be (4+24+21)/(4+24+4+1+1+21) - but I want to automatically generate the statistics

(df * np.eye(3)).values.sum() / df.values.sum()

输出:

0.89090909090909087

Check this out sklearn.metrics.classification_report

print(classification_report(matrix['actual'], 
                            matrix['predict'], 
                            target_names=['BIC', 'Cubic', 'Reno']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM