0

Working with the IEEE document template ( ieeetran document class) to create a nice-looking table for the confusion matrix. Initially, I created the table for normalized values and it was looking perfect.

When I created a second table, with the actual class value for the confusion matrix, the table alignment somehow changed and for a couple of minutes, I could not understand how to make it aligned well as in table 1.

Code below reproduces the 2-tables:

\documentclass{ieeetran}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}
\usepackage{siunitx}

 \begin{document}
    
    \begin{table}[ht]
        \caption{Confusion matrix normalized}
        \label{tab1}
        \settowidth\rotheadsize{\textbf{Predicted}}
        \centering
        \begin{tabular}{*{2}{>{\bfseries}l} *{6}{S[table-format=2.2]} }
        \toprule
        &   &   \mcc[6]{\textbf{Actual}}                            \\
        \cmidrule{3-8}
        &   & {\textbf{Foot}} & {\textbf{Bike}}  & {\textbf{Bus}} 
        & {\textbf{Car}}  & {\textbf{Metro}}  & {\textbf{Precision}}\\ 
        \midrule
        \multirow{5}{*}{\rothead{Predicted}} 
            & Foot  &  0.34 &  0.13  &  0.17  &  0.09  &  0.27  & 0.59     \\
            & Bike  &  0.29 &  0.17  &  0.21  &  0.02  &  0.33  & 0.05     \\
            & Bus   &  0.20 &  0.16  &  0.26  &  0.16  &  0.21  & 0.22     \\
            & Car   &  0.12 &  0.15  &  0.22  &  0.36  &  0.15  & 0.65     \\
            & Metro &  0.34 &  0.13  &  0.10  &  0.17  & 0.26   & 0.01     \\
        \midrule
            & Recall & 0.35 &  0.05  & 0.03   & 0.78   & 0.16   &      \\ 
        \bottomrule
        \end{tabular}
    \end{table}
    
    \begin{table}[ht]
            \caption{Confusion matrix actual class values}
            \label{tab2}
            \settowidth\rotheadsize{\textbf{Predicted}}
            \centering
            \begin{tabular}{*{2}{>{\bfseries}l} *{6}{S[table-format=2.2]} }
            \toprule
            &   &   \mcc[6]{\textbf{Actual}}                \\
            \cmidrule{3-8}
            &   & {\textbf{Foot}} & {\textbf{Bike}}  & {\textbf{Bus}} 
            & {\textbf{Car}}  & {\textbf{Metro}}  & {\textbf{Precision}}\\ 
            \midrule
            \multirow{5}{*}{\rothead{Predicted}} 
                & Foot  &  5007  &  1896  &  2437  &  1314  & 3947  &  0.59   \\
                & Bike  &  409   &  218   &  286   &  27    & 458  &  0.05   \\
                & Bus   &  1688  &  1365  &  2159  &  1346  & 1759  &  0.22 \\
                & Car   &  2386  &  3093  &  4521  &  7323  & 2978  &  0.65 \\
                & Metro &  311   &  113   &  93    &  154   & 233   &  0.01  \\
            \midrule
                &Recall &  0.35  &  0.05  & 0.03   &  0.78  & 0.16  &       \\ 
            \bottomrule
            \end{tabular}
        \end{table}
    
\end{document}

Output: enter image description here

0
2

Use the relevant parameters for table-format. I preferred centring rather than aligning the numbers in the last row. Even the first table is not perfect from this point of view:

\documentclass{ieeetran}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}
\usepackage{siunitx}

 \begin{document}

\begin{table}[ht]
\sisetup{table-number-alignment=center}
    \caption{Confusion matrix normalized}
    \label{tab1}
    \settowidth\rotheadsize{\textbf{Predicted}}
    \centering
    \begin{tabular}{*{2}{>{\bfseries}l} *{6}{S[table-format=1.2]} }
    \toprule
    & & \mcc[6]{\textbf{Actual}} \\
    \cmidrule{3-8}
    & & {\textbf{Foot}} & {\textbf{Bike}} & {\textbf{Bus}}
    & {\textbf{Car}} & {\textbf{Metro}} & {\textbf{Precision}}\\
    \midrule
    \multirow{5}{*}{\rothead{Predicted}}
        & Foot & 0.34 & 0.13 & 0.17 & 0.09 & 0.27 & 0.59 \\
        & Bike & 0.29 & 0.17 & 0.21 & 0.02 & 0.33 & 0.05 \\
        & Bus & 0.20 & 0.16 & 0.26 & 0.16 & 0.21 & 0.22 \\
        & Car & 0.12 & 0.15 & 0.22 & 0.36 & 0.15 & 0.65 \\
        & Metro & 0.34 & 0.13 & 0.10 & 0.17 & 0.26 & 0.01 \\
    \midrule
        & Recall & 0.35 & 0.05 & 0.03 & 0.78 & 0.16 & \\
    \bottomrule
    \end{tabular}
\end{table}

\begin{table}[ht]
        \caption{Confusion matrix actual class values}
        \label{tab2}
        \settowidth\rotheadsize{\textbf{Predicted}}
        \centering
        \begin{tabular}{*{2}{>{\bfseries}l} *{5}{S[table-format=4.0]}S[table-format=1.2] }
        \toprule
        & & \mcc[6]{\textbf{Actual}} \\
        \cmidrule{3-8}
        & & {\textbf{Foot}} & {\textbf{Bike}} & {\textbf{Bus}}
        & {\textbf{Car}} & {\textbf{Metro}} & {\textbf{Precision}}\\
        \midrule
        \multirow{5}{*}{\rothead{Predicted}}
            & Foot & 5007 & 1896 & 2437 & 1314 & 3947 & 0.59 \\
            & Bike & 409 & 218 & 286 & 27 & 458 & 0.05 \\
            & Bus & 1688 & 1365 & 2159 & 1346 & 1759 & 0.22 \\
            & Car & 2386 & 3093 & 4521 & 7323 & 2978 & 0.65 \\
            & Metro & 311 & 113 & 93 & 154 & 233 & 0.01 \\
        \midrule
            &Recall & {0.35} & {0.05} & {0.03} & {0.78} & {0.16} & \\
        \bottomrule
        \end{tabular}
    \end{table}

\end{document} 

enter image description here

1

(This is similar to @leandriis' comment but with different values suggested)

In siunitx's S-type columns the table-head should be set to the digits you actually need, so change 2.2 to 1.2 for your first table, and to 4.2 for your second table (except for the last column):

\documentclass{article}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}
\usepackage{siunitx}

 \begin{document}
    
    \begin{table}[ht]
        \caption{Confusion matrix normalized}
        \label{tab1}
        \settowidth\rotheadsize{\textbf{Predicted}}
        \centering
        \begin{tabular}{*{2}{>{\bfseries}l} *{6}{S[table-format=1.2]} }
        \toprule
        &   &   \mcc[6]{\textbf{Actual}}                            \\
        \cmidrule{3-8}
        &   & {\textbf{Foot}} & {\textbf{Bike}}  & {\textbf{Bus}} 
        & {\textbf{Car}}  & {\textbf{Metro}}  & {\textbf{Precision}}\\ 
        \midrule
        \multirow{5}{*}{\rothead{Predicted}} 
            & Foot  &  0.34 &  0.13  &  0.17  &  0.09  &  0.27  & 0.59     \\
            & Bike  &  0.29 &  0.17  &  0.21  &  0.02  &  0.33  & 0.05     \\
            & Bus   &  0.20 &  0.16  &  0.26  &  0.16  &  0.21  & 0.22     \\
            & Car   &  0.12 &  0.15  &  0.22  &  0.36  &  0.15  & 0.65     \\
            & Metro &  0.34 &  0.13  &  0.10  &  0.17  & 0.26   & 0.01     \\
        \midrule
            & Recall & 0.35 &  0.05  & 0.03   & 0.78   & 0.16   &      \\ 
        \bottomrule
        \end{tabular}
    \end{table}
    
    \begin{table}[ht]
            \caption{Confusion matrix actual class values}
            \label{tab2}
            \settowidth\rotheadsize{\textbf{Predicted}}
            \centering
            \begin{tabular}{*{2}{>{\bfseries}l} *{5}{S[table-format=4.2]} S[table-format=0.2]}
            \toprule
            &   &   \mcc[6]{\textbf{Actual}}                \\
            \cmidrule{3-8}
            &   & {\textbf{Foot}} & {\textbf{Bike}}  & {\textbf{Bus}} 
            & {\textbf{Car}}  & {\textbf{Metro}}  & {\textbf{Precision}}\\ 
            \midrule
            \multirow{5}{*}{\rothead{Predicted}} 
                & Foot  &  5007  &  1896  &  2437  &  1314  & 3947  &  0.59   \\
                & Bike  &  409   &  218   &  286   &  27    & 458  &  0.05   \\
                & Bus   &  1688  &  1365  &  2159  &  1346  & 1759  &  0.22 \\
                & Car   &  2386  &  3093  &  4521  &  7323  & 2978  &  0.65 \\
                & Metro &  311   &  113   &  93    &  154   & 233   &  0.01  \\
            \midrule
                &Recall &  0.35  &  0.05  & 0.03   &  0.78  & 0.16  &       \\ 
            \bottomrule
            \end{tabular}
        \end{table}
    
\end{document}

The picture looks slightly different, as I don't have ieeetran installed and used article instead.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.