如何在 LaTeX 中构建混淆矩阵?

如何在 LaTeX 中构建混淆矩阵?

A混淆矩阵好像:

在此处输入图片描述

有没有一种使用 LaTeX 的简单方法可以做到同样的事情?

否则,我可以使用任何软件创建图表。

答案1

这是一个没有的解决方案TikZ

\documentclass{article}
\usepackage{array}
\usepackage{multirow}

\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}

\noindent
\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{10}{*}{\parbox{1.1cm}{\bfseries\raggedleft 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{document}

在评论中,有人要求旋转其中一个标签;这可以通过以下\rotatebox方式轻松完成graphicx

\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage{multirow}

\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}

\noindent
\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{10}{*}{\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{document}

在此处输入图片描述

答案2

以下是我用来为教程制作相关表格的一些 LaTeX 代码。虽然您可能想要更改布局,但它可能会让您接近想要的结果。

\begin{tabular}{l|l|c|c|c}
\multicolumn{2}{c}{}&\multicolumn{2}{c}{True diagnosis}&\\
\cline{3-4}
\multicolumn{2}{c|}{}&Positive&Negative&\multicolumn{1}{c}{Total}\\
\cline{2-4}
\multirow{2}{*}{Screening test}& Positive & $a$ & $b$ & $a+b$\\
\cline{2-4}
& Negative & $c$ & $d$ & $c+d$\\
\cline{2-4}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{Total} & \multicolumn{1}{c}{$a+c$} & \multicolumn{    1}{c}{$b+d$} & \multicolumn{1}{c}{$N$}\\
\end{tabular}

在此处输入图片描述

% need the pifont package
\begin{tabular}{l|l|>{\columncolor{gray!20}}l|l|l}
%\begin{tabular}{l|l|l|l|l}                                                                  
\multicolumn{2}{c}{}&\multicolumn{2}{c}{True diagnosis}&\\
\cline{3-4}
\multicolumn{2}{c|}{}&\multicolumn{1}{c|}{Positive}&\multicolumn{1}{c|}{Negative}&\multicolu    mn{1}{c}{}\\
%\cline{2-4}                                                                                 
\hhline{~|---}
\multirow{2}{*}{Screening test}& Positive & TP & FP ($\alpha$) &\ding{214} PPV\\
%\cline{2-4}                                                                                 
\hhline{~|---}
& Negative & FN ($\beta$) & TN & \ding{214} NPV\\
%\cline{2-4}                                                                                 
\hhline{~|---}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{} &
\multicolumn{1}{c}{\multirow{2}{*}{\parbox{2em}{\vskip1ex\ding{215}\\ Se}}} & \multicolumn{1    }{c}{\multirow{2}{*}{\parbox{2em}{\vskip1ex\ding{215}\\ Sp}}} &\multicolumn{1}{c}{}\\
\end{tabular}

在此处输入图片描述

答案3

一旦掌握了窍门,用 TikZ 制作这样的事情就很容易了。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
box/.style={draw,rectangle,minimum size=2cm,text width=1.5cm,align=left}]
\matrix (conmat) [row sep=.1cm,column sep=.1cm] {
\node (tpos) [box,
    label=left:\( \mathbf{p'} \),
    label=above:\( \mathbf{p} \),
    ] {True \\ positive};
&
\node (fneg) [box,
    label=above:\textbf{n},
    label=above right:\textbf{total},
    label=right:\( \mathrm{P}' \)] {False \\ negative};
\\
\node (fpos) [box,
    label=left:\( \mathbf{n'} \),
    label=below left:\textbf{total},
    label=below:P] {False \\ positive};
&
\node (tneg) [box,
    label=right:\( \mathrm{N}' \),
    label=below:N] {True \\ negative};
\\
};
\node [left=.05cm of conmat,text width=1.5cm,align=right] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}
\end{document}

旋转实际价值你可以更换

\node [left=.05cm of conmat,text width=1.5cm,align=right] {\textbf{actual \\ value}};

 \node [rotate=90,left=.05cm of conmat,anchor=center,text width=1.5cm,align=center] {\textbf{actual \\ value}};

结果

答案4

混淆矩阵

\documentclass[conference]{IEEEtran}
\usepackage[center]{caption}
\usepackage{multirow}
\usepackage{textcomp}
\usepackage{graphicx}
\begin{document}
\begin{table}[!ht]
    \centering
    \caption{Confusion Matrix}
    \label{sa}
    \renewcommand{\arraystretch}{3}
    \begin{tabular}{ll|l|l|l|l|l|}
        
\multicolumn{2}{c}{}&   \multicolumn{5}{c}{Predicted Classes}\\
        \multicolumn{2}{c}{}&\multicolumn{5}{c}{{\rotatebox[origin=c]{0}{Fear}
            } {\rotatebox[origin=c]{0}{Happy}
            } {\rotatebox[origin=c]{0}{Love}
            } {\rotatebox[origin=c]{0}{Sad}
            } {\rotatebox[origin=c]{0}{Violence}
        }}\\
        \cline{3-7}
        \multirow{5}{*}{{\rotatebox[origin=c]{90}{Actual Classes}
        }} & 
        Fear&0.55 & 0.16 &0.04 & 0.30 & 0.02  \\ \cline{3-7}
        &   Happy&0.13 & 0.75 &0.02 & 0.11 & 0.02 \\ \cline{3-7}
        &   Love&0.03 & 0.01 &0.91 & 0.00 & 0.01 \\ \cline{3-7}
        &   Sad&0.25 & 0.07 &0.01 & 0.58 & 0.01 \\ \cline{3-7}
        &   Violence&0.04 & 0.01 &0.02 & 0.00 & 0.94 \\ \cline{3-7}
    \end{tabular}
\end{table}
\end{document}

相关内容