convert string to latex table format in Python

727 Views Asked by At

I have the following performance report for Machine learning algorithms using Sklearn:

>>> from sklearn.metrics import classification_report
>>> y_true = [0, 1, 2, 2, 2]
>>> y_pred = [0, 0, 2, 2, 1]
>>> target_names = ['class 0', 'class 1', 'class 2']
>>> print(classification_report(y_true, y_pred, target_names=target_names))
             precision    recall  f1-score   support

    class 0       0.50      1.00      0.67         1
    class 1       0.00      0.00      0.00         1
    class 2       1.00      0.67      0.80         3

avg / total       0.70      0.60      0.61         5

I am saving the classification_report as a text file using file.write(report), but I would like to save it as in TEX table format as follows:

\begin{table}[htbp]
  \centering
  \caption{Add caption}
    \begin{tabular}{rrrrr}
    \toprule
          & precision & recall & f1-score & support \\
    \midrule
          &       &       &       &  \\
    class 0 & 0.5   & 1     & 0.67  & 1 \\
    class 1 & 0     & 0     & 0     & 1 \\
    class 2 & 1     & 0.67  & 0.8   & 3 \\
          &       &       &       &  \\
    avg/total & 0.7   & 0.6   & 0.61  & 5 \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

Any recommendations on how to achieve this? Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

The table header and footer are just text, so I'll skip those.

Take the output of classification_report, and split it into lines with str.splitlines().

In [7]: rep = """             precision    recall  f1-score   support
   ...: 
   ...:     class 0       0.50      1.00      0.67         1
   ...:     class 1       0.00      0.00      0.00         1
   ...:     class 2       1.00      0.67      0.80         3
   ...: 
   ...: avg / total       0.70      0.60      0.61         5"""

In [8]: rep.splitlines()
Out[8]: 
['             precision    recall  f1-score   support',
 '',
 '    class 0       0.50      1.00      0.67         1',
 '    class 1       0.00      0.00      0.00         1',
 '    class 2       1.00      0.67      0.80         3',
 '',
 'avg / total       0.70      0.60      0.61         5']

Since you know what the first and last two lines contain, you can concentrate your formatting efforts on the remaining lines.

In [9]: lines = rep.splitlines()

In [10]: lines[2:-2]
Out[10]: 
['    class 0       0.50      1.00      0.67         1',
 '    class 1       0.00      0.00      0.00         1',
 '    class 2       1.00      0.67      0.80         3']

In [11]: cl = lines[2:-2]

In [19]: [ln.replace('class ', '').split() for ln in cl]
Out[19]: 
[['0', '0.50', '1.00', '0.67', '1'],
 ['1', '0.00', '0.00', '0.00', '1'],
 ['2', '1.00', '0.67', '0.80', '3']]

In [20]: cl = [ln.replace('class ', '').split() for ln in cl]

In [23]: for ln in cl:
    print('class ' + ' & '.join(ln) + r'\\')
   ....:     
class 0 & 0.50 & 1.00 & 0.67 & 1\\
class 1 & 0.00 & 0.00 & 0.00 & 1\\
class 2 & 1.00 & 0.67 & 0.80 & 3\\

The avg line is dealt with in much the same way.

In [25]: last = lines[-1]

In [29]: last[11:].split()
Out[29]: ['0.70', '0.60', '0.61', '5']

In [30]: numbers = last[11:].split()

In [31]: print('avg / total & ' + ' & '.join(numbers) + r'\\')
avg / total & 0.70 & 0.60 & 0.61 & 5\\

I would suggest skipping the empty lines especially since you're already using the rulers from the booktabs package.


Alternative

If there is a way of getting the data out of sklearn by row, you might want to look at the simple latable Python module that I've written.