title: Python - How to create confusion matrix statistics using python pandas crosstab
body: <p>Hereunder is my Phyton script that generates the following confusion matrix</p>
  
  <p><a href="https://i.stack.imgur.com/23OnU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/23OnU.png" alt="enter image description here"></a></p>
  
  <pre><code># /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&gt;0.61 and beta&lt;=0.75):
              return "Cubic"
          if (beta&gt;=0.45 and beta&lt;0.61):
              return "Reno"
          if (beta&gt;0.75 and beta&lt;=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 &gt;= 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)
  </code></pre>
  
  <p>How can we add a confusion matrix statistics (for example: <code>accuracy</code> to <code>Python</code> pandas crosstab? If we manually do it, the <code>accuracy</code> will be (4+24+21)/(4+24+4+1+1+21) - but I want to automatically generate the statistics </p>