SHARE
TWEET

Untitled

a guest Sep 20th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def gen_align(align, size):
  2.     texto = ""
  3.     for i in range(0,size):
  4.         texto += align
  5.     return texto
  6.  
  7. def gen_header(size):
  8.     header = " "
  9.     for i in range(0, size):
  10.         header += "& " + str(i) + " "
  11.     header += "\\\ \\midrule\n"
  12.     return header
  13.  
  14. def cm_to_latex(array, align="l"):
  15.     n_cols = len(array[0])
  16.     table = "\\begin{table}\n"
  17.     table += "\caption{Table of Confusion Matrix generated with python}\n"
  18.     table += "\label{cm:python:generated}\n"
  19.     table += "\small\n"
  20.     table += "\\centering\n"
  21.     table += "\\begin{tabular}{@{}"+ gen_align(align, n_cols+1) + "@{}}\n"
  22.     table += "\\toprule\n"
  23.     table += gen_header(n_cols)
  24.     for i, linha in enumerate(array):
  25.         table += str(i) + " "
  26.         for val in linha:
  27.             table += "& " + str(val) + " "
  28.         if i+1 == n_cols:
  29.             table += "\\\ \\bottomrule \n"
  30.         else:
  31.             table += "\\\ \n"
  32.     table += "\end{tabular}\n"
  33.     table += "\end{table}"
  34.     print(table)
  35.    
  36. # Test
  37.  
  38. array = [[3682,    7,  127,  107,  277,    3,    3,    5],
  39.          [ 108,    9,   14,    6,   20,    0,    0,    0],
  40.          [  70,    1,  162,    3,   14,    0,    0,    0],
  41.          [  63,    2,    5,  112,   15,    0,    3,    3],
  42.          [  73,    0,   11,    6,  354,    0,    0,    8],
  43.          [   0,    0,    0,    0,    0,    5,    0,    0],
  44.          [   1,    0,    0,    3,    2,    0,    8,    0],
  45.          [  22,    0,    2,    0,   15,    0,    0,   30]]
  46.  
  47. cm_to_latex(array)
  48.  
  49. # Result
  50.  
  51. """
  52.  
  53. \begin{table}
  54. \caption{Table of Confusion Matrix generated with python}
  55. \label{cm:python:generated}
  56. \small
  57. \centering
  58. \begin{tabular}{@{}lllllllll@{}}
  59. \toprule
  60.  & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \midrule
  61. 0 & 3682 & 7 & 127 & 107 & 277 & 3 & 3 & 5 \\
  62. 1 & 108 & 9 & 14 & 6 & 20 & 0 & 0 & 0 \\
  63. 2 & 70 & 1 & 162 & 3 & 14 & 0 & 0 & 0 \\
  64. 3 & 63 & 2 & 5 & 112 & 15 & 0 & 3 & 3 \\
  65. 4 & 73 & 0 & 11 & 6 & 354 & 0 & 0 & 8 \\
  66. 5 & 0 & 0 & 0 & 0 & 0 & 5 & 0 & 0 \\
  67. 6 & 1 & 0 & 0 & 3 & 2 & 0 & 8 & 0 \\
  68. 7 & 22 & 0 & 2 & 0 & 15 & 0 & 0 & 30 \\ \bottomrule
  69. \end{tabular}
  70. \end{table}
  71.  
  72. """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top