convert string to latex table format in Python -


i have following performance report machine learning algorithms using sklearn:

>>> 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 saving classification_report text file using file.write(report), save in tex table format 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 achieve this? thanks!

the table header , footer text, i'll skip those.

take output of classification_report, , split lines 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 know first , last 2 lines contain, can concentrate formatting efforts on 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() 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() ln in cl]  in [23]: 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 dealt in 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 suggest skipping empty lines since you're using rulers booktabs package.


alternative

if there way of getting data out of sklearn row, might want @ simple latable python module i've written.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -