3

I am wondering how I can create a figure using a table or TikZ to depict the idea behind cross validation. Can anyone help me create something that looks something like the attached image?

k-fold cross validation

3
  • Welcome to TeX.SX! What have you done so far?
    – Bobyandbob
    May 1 '18 at 8:33
  • Thanks! I haven't done anything yet, since I am uncertain where to start. I considered just making a table with borders and coloring the "holdout" cells (main diagonal), but I thought it would be easier to draw the legends etc using TikZ. May 1 '18 at 8:37
  • @EirikFosse legend could be easily done with a second tabular with both vertical alignment c. Only thing that could be hard to get is the long arrows in the top row.
    – Skillmon
    May 1 '18 at 9:11
7

Today is 1st of May, so I don't have to work and I'll draw it for you ;)

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}
    \matrix (M) [matrix of nodes,
        nodes={minimum height = 7mm, minimum width = 2cm, outer sep=0, anchor=center, draw},
        column 1/.style={nodes={draw=none}, minimum width = 4cm},
        row sep=1mm, column sep=-\pgflinewidth, nodes in empty cells,
        e/.style={fill=yellow!10}
      ]
      {
        Experiment 1 & |[e]| & & & & \\
        Experiment 2 & & |[e]| & & & \\
        Experiment 3 & & & |[e]| & & \\
        Experiment 4 & & & & |[e]| & \\
        Experiment 5 & & & & & |[e]| \\
      };
      \draw (M-1-2.north west) ++(0,2mm) coordinate (LT) edge[|<->|, >= latex] node[above]{Total number of datasets} (LT-|M-1-6.north east);
  \end{tikzpicture}
\end{document}

enter image description here

3
  • Is edge[|<->| something new? Never saw it before --> very handy May 1 '18 at 9:27
  • @Dr.ManuelKuehner No it is not new and it is used everywhere in the manual.
    – Kpym
    May 1 '18 at 9:32
  • Good to know. Will be useful in the future. Thanks for the reply. May 1 '18 at 9:40
5

Version without TikZ and only tabular:

\documentclass[]{article}

\usepackage[table]{xcolor}
\definecolor{ccellcolor}{HTML}{d09000}

\newcommand*\revealcline{\noalign{\vskip\arrayrulewidth}}
\newcommand*\nextrow[1]
  {\\\cline{#1}\noalign{\vskip1ex}\cline{#1}\revealcline}
\newcount\ccellA
\newcommand*\ccell[2]
  {%
    \def\tmpa{}%
    \ccellA=1
    \loop
      \ifnum#1=\ccellA
        \edef\tmpa{\unexpanded\expandafter{\tmpa\cellcolor{ccellcolor}}}%
      \fi
    \ifnum#2>\ccellA
      \advance\ccellA1
      \edef\tmpa{\unexpanded\expandafter{\tmpa&}}%
    \repeat
    \tmpa
  }

\begin{document}
\begin{tabular}[c]{l *5{|p{2em}}|}
  & \multicolumn{5}{c|}{$\longleftarrow$ Total Number of Dataset $\longrightarrow$}\\
  \cline{2-6}\revealcline
  Experiment 1 & \ccell{1}{5}
  \nextrow{2-6}
  Experiment 2 & \ccell{2}{5}
  \nextrow{2-6}
  Experiment 3 & \ccell{3}{5}
  \nextrow{2-6}
  Experiment 4 & \ccell{4}{5}
  \nextrow{2-6}
  Experiment 5 & \ccell{5}{5}\\
  \cline{2-6}
\end{tabular}\hskip1em
\begin{tabular}[c]{|p{2em}|l}
  \cline{1-1}
  \ccell{0}{1}  & Training
  \nextrow{1-1}
  \ccell{1}{1}  & Validation\\
  \cline{1-1}
\end{tabular}
\end{document}

enter image description here

0

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.