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!
The table header and footer are just text, so I'll skip those.
Take the output of
classification_report
, and split it into lines withstr.splitlines()
.Since you know what the first and last two lines contain, you can concentrate your formatting efforts on the remaining lines.
The
avg
line is dealt with in much the same way.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 simplelatable
Python module that I've written.