A confusion matrix looks like:
Is there an easy way to do the same using LaTeX?
Otherwise I could just create a diagram with any software.
matricestables
A confusion matrix looks like:
Is there an easy way to do the same using LaTeX?
Otherwise I could just create a diagram with any software.
If the confusion matrix, as produced by the code given in the earlier answer, is too large for your taste (even though it fits easily inside the available space), you could either edit the code to reduce the various dimensions by, say, 20 percent, or you could place the code inside a \scalebox{0.8}{<code for confusion matrix>}
directive.
The code below uses the second approach. It shows both an unscaled confusion matrix and one that's scaled down 20 percent.
\documentclass[conference,hidelinks]{IEEEtran}
\usepackage{array,graphicx,multirow}
\usepackage{lipsum} % for filler text
\newcommand\MyBox[2]{
\fbox{\lower0.75cm
\vbox to 1.7cm{\vfil
\hbox to 1.7cm{\hfil\parbox{1.4cm}{#1\\#2}\hfil}
\vfil}%
}%
}
\begin{document}
Two sizes: First unscaled, then scaled down 20\%
\begin{center}
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{0pt}
\begin{tabular}{c >{\bfseries}r @{\hspace{0.7em}}c @{\hspace{0.4em}}c @{\hspace{0.7em}}l}
\multirow{11}{*}{\rotatebox{90}{\parbox{1.1cm}{\bfseries\centering actual\\ value}}} &
& \multicolumn{2}{c}{\bfseries Prediction outcome} & \\
& & \bfseries p & \bfseries n & \bfseries total \\
& p$'$ & \MyBox{True}{Positive} & \MyBox{False}{Negative} & P$'$ \\[2.4em]
& n$'$ & \MyBox{False}{Positive} & \MyBox{True}{Negative} & N$'$ \\
& total & P & N &
\end{tabular}
\bigskip
\scalebox{0.8}{%
\begin{tabular}{c >{\bfseries}r @{\hspace{0.7em}}c @{\hspace{0.4em}}c @{\hspace{0.7em}}l}
\multirow{11}{*}{\rotatebox{90}{\parbox{1.1cm}{\bfseries\centering actual\\ value}}} &
& \multicolumn{2}{c}{\bfseries Prediction outcome} & \\
& & \bfseries p & \bfseries n & \bfseries total \\
& p$'$ & \MyBox{True}{Positive} & \MyBox{False}{Negative} & P$'$ \\[2.4em]
& n$'$ & \MyBox{False}{Positive} & \MyBox{True}{Negative} & N$'$ \\
& total & P & N &
\end{tabular}}
\end{center}
\lipsum[2] % filler text
\end{document}
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{collcell}
\usepackage{caption}
%The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.2} %
\newcommand*{\MaxNumber}{1.0}%
\def\OmitZero#1.#2.#3!{%
\ifx\relax#2\relax % #2 is empty -> no decimal
#1%
\else % #2 not empty -> decimal
\ifnum#1=0 % we have 0.
.#2%
\else % we have no 0.xx
#1.#2%
\fi
\fi}
%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
\ifdim #1 pt > \MidNumber pt
\pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 -
\MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
\hspace{-0.33em}\colorbox{green!\PercentColor!red}{\OmitZero#1..!}
\else
\pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber -
#1)/(\MidNumber-\MinNumber),100.0),0.00)} %
\hspace{-0.33em}\colorbox{white!\PercentColor!red}{\OmitZero#1..!}
\fi
}
\newcolumntype{G}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}
%% for rotation in table
\usepackage{adjustbox}
\usepackage{array}
\newcolumntype{R}[2]{%
>{\adjustbox{angle=#1,lap=\width-(#2)}\bgroup}%
l%
<{\egroup}%
}
\newcommand*\rotz{\multicolumn{1}{R{0}{-1em}}}% no optional argument here, please!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{table}[t]
\caption{The confusion matrix}
\label{tab:CM}
\centering
\resizebox{1\textwidth}{!}{ % to nicely resize the table in the text width
\begin{tabular}{r*{6}{G}}
\rotz{} &
\rotz{A} &
\rotz{B} &
\rotz{C} &
\rotz{D} &
\rotz{E} \smallskip \\
A & 0.54 & 0 & 0.08 & 0.17 & 0 \\
B & 0 & 0.96 & 0 & 0 & 0 \\
C & 0.04 & 0.04 & 0.38 & 0.13 & 0 \\
D & 0.08 & 0 & 0.04 & 0.75 & 0 \\
E & 0 & 0.04 & 0 & 0 & 0.96 \\
\end{tabular}%\par\bigskip
}
\end{table}
\end{document}
Best Answer
Here's a solution without
TikZ
:In a comment it has been requested to rotate one of the labels; this can be easily done using
\rotatebox
fromgraphicx
: