在 tikz 中复制决策树

在 tikz 中复制决策树

tikz我正在尝试对来自的决策树模型进行一些修改这里。我使用的代码与该帖子完全相同,唯一的变化是\documentclass[]{article}

代码:

\documentclass[]{article}

\usepackage{forest}
\usetikzlibrary{fit,positioning}

\tikzset{
  font=\Large\sffamily\bfseries,
  red arrow/.style={
    midway,red,sloped,fill, minimum height=3cm, single arrow, single arrow head extend=.5cm, single arrow head indent=.25cm,xscale=0.3,yscale=0.15,
    allow upside down
  },
  black arrow/.style 2 args={-stealth, shorten >=#1, shorten <=#2},
  black arrow/.default={1mm}{1mm},
  tree box/.style={draw, rounded corners, inner sep=1em},
  node box/.style={white, draw=black, text=black, rectangle, rounded corners},
}

\begin{document}
\begin{forest}
  for tree={l sep=3em, s sep=3em, anchor=center, inner sep=0.7em, fill=blue!50, circle, where level=2{no edge}{}}
  [
  Training Data, node box
  [sample and feature bagging, node box, alias=bagging, above=4em
  [,red!70,alias=a1[[,alias=a2][]][,red!70,edge label={node[above=1ex,red arrow]{}}[[][]][,red!70,edge label={node[above=1ex,red arrow]{}}[,red!70,edge label={node[below=1ex,red arrow]{}}][,alias=a3]]]]
  [,red!70,alias=b1[,red!70,edge label={node[below=1ex,red arrow]{}}[[,alias=b2][]][,red!70,edge label={node[above=1ex,red arrow]{}}]][[][[][,alias=b3]]]]
  [~~$\dots$~,scale=2,no edge,fill=none,yshift=-4em]
  [,red!70,alias=c1[[,alias=c2][]][,red!70,edge label={node[above=1ex,red arrow]{}}[,red!70,edge label={node[above=1ex,red arrow]{}}[,alias=c3][,red!70,edge label={node[above=1ex,red arrow]{}}]][,alias=c4]]]]
  ]
  \node[tree box, fit=(a1)(a2)(a3)](t1){};
  \node[tree box, fit=(b1)(b2)(b3)](t2){};
  \node[tree box, fit=(c1)(c2)(c3)(c4)](tn){};
  \node[below right=0.5em, inner sep=0pt] at (t1.north west) {Tree 1};
  \node[below right=0.5em, inner sep=0pt] at (t2.north west) {Tree 2};
  \node[below right=0.5em, inner sep=0pt] at (tn.north west) {Tree $n$};
  \path (t1.south west)--(tn.south east) node[midway,below=4em, node box] (mean) {mean in regression or majority vote in classification};
  \node[below=3em of mean, node box] (pred) {prediction};
  \draw[black arrow={5mm}{4mm}] (bagging) -- (t1.north);
  \draw[black arrow] (bagging) -- (t2.north);
  \draw[black arrow={5mm}{4mm}] (bagging) -- (tn.north);
  \draw[black arrow={5mm}{5mm}] (t1.south) -- (mean);
  \draw[black arrow] (t2.south) -- (mean);
  \draw[black arrow={5mm}{5mm}] (tn.south) -- (mean);
  \draw[black arrow] (mean) -- (pred);
\end{forest}
\end{document}

我一直在尝试做以下几件事:

  1. 使tikz图表适合于\documentclass[]{article}而不是\documentclass[tikz]{standalone}
  2. 我一直在尝试改变颜色以匹配下面的树

在此处输入图片描述

其中终端节点为绿色和红色,并且所有其他节点都是相同的颜色,但似乎无法弄清楚这一部分(当前,当箭头为红色时,代码中的树是红色的。我想保留箭头,但只是使所有颜色相同 - 除终端节点之外)。

  1. 我可以通过修改下面的行来更改circle为,但它变成了一个正方形。rectanglefor tree={l sep=3em, s sep=3em, anchor=center, inner sep=0.7em, fill=blue!50, rectangle, where level=2{no edge}{}}

编辑:

在此处输入图片描述

答案1

  1. 嗯,它太宽了,所以你需要把它变窄。例如通过减小s sep

  2. 节点的颜色和箭头的颜色没有关系,如果节点是红色的,那是因为你,red!70为该特定节点添加了颜色。所以你只需要删除很多这样的,red!70颜色。

  3. 您需要分别设置宽度和高度:

      inner sep=0,
      minimum width=1em,
      minimum height=0.5em,
    

inner sep,因此没有填充,然后将minimum width/设置height为合适的值。您可能需要修改它们。

我还设置了s sep节点sample and feature bagging,,以便将子树间隔得更远一些,并且我添加了两个phantom节点以在第二个和第三个之间留出更多空间。然后我通过在和\dots中间放置一个节点来添加后者。t2tn

我可能会将mean in regression..节点直接设置在下面t2,但我将由您来决定。

此屏幕截图中的框架由showframe包制作,表示文本块的宽度。

在此处输入图片描述

\documentclass[]{article}

\usepackage{
  forest,
 % showframe
 }
\usetikzlibrary{fit,positioning}

\tikzset{
  font=\large\sffamily\bfseries,
  red arrow/.style={
    midway,red,sloped,fill, minimum height=3cm, single arrow, single arrow head extend=.5cm, single arrow head indent=.25cm,xscale=0.3,yscale=0.15,
    allow upside down
  },
  black arrow/.style 2 args={-stealth, shorten >=#1, shorten <=#2},
  black arrow/.default={1mm}{1mm},
  tree box/.style={draw, rounded corners, inner sep=1em},
  node box/.style={white, draw=black, text=black, rectangle, rounded corners},
}

\begin{document}
\begin{center}
\begin{forest}
  for tree={
     l sep=2em,
     s sep=2mm,
     anchor=center,
     inner sep=0,
     minimum width=1em,
     minimum height=0.5em,
     fill=blue!50,
     rectangle,
     where level=2{no edge}{}}
  [
  Training Data, node box
  [sample and feature bagging, node box, alias=bagging, above=4em,s sep=1.1cm
  [,alias=a1[[,alias=a2][]][,edge label={node[above=1ex,red arrow]{}}[[][]]
  [,edge label={node[above=1ex,red arrow]{}}[,red!70,edge label={node[below=1ex,red arrow]{}}][,alias=a3]]]]
  [,alias=b1[,edge label={node[below=1ex,red arrow]{}}[[,alias=b2][]][,red!70,edge label={node[above=1ex,red arrow]{}}]][[][[][,alias=b3]]]]
  [,phantom]
  [,phantom]
  [,alias=c1[[,alias=c2][]][,edge label={node[above=1ex,red arrow]{}}[,edge label={node[above=1ex,red arrow]{}}[,alias=c3][,red!70,edge label={node[above=1ex,red arrow]{}}]][,alias=c4]]]]
  ]
  \node[tree box, fit=(a1)(a2)(a3)](t1){};
  \node[tree box, fit=(b1)(b2)(b3)](t2){};
  \node[tree box, fit=(c1)(c2)(c3)(c4)](tn){};
  \begin{scope}[every node/.append style={below right=0.5em, inner sep=0pt, font=\normalsize\sffamily\bfseries}]
  \node at (t1.north west) {Tree 1};
  \node at (t2.north west) {Tree 2};
  \node at (tn.north west) {Tree $n$};
  \end{scope}
  \path (t1.south west)--(tn.south east) node[midway,below=4em, node box] (mean) {mean in regression or majority vote in classification};
  \node[below=3em of mean, node box] (pred) {prediction};
  \draw[black arrow={5mm}{4mm}] (bagging) -- (t1.north);
  \draw[black arrow] (bagging) -- (t2.north);
  \draw[black arrow={5mm}{4mm}] (bagging) -- (tn.north);
  \draw[black arrow={5mm}{5mm}] (t1.south) -- (mean);
  \draw[black arrow] (t2.south) -- (mean);
  \draw[black arrow={5mm}{5mm}] (tn.south) -- (mean);
  \draw[black arrow] (mean) -- (pred);
  \path (t2) -- node {\dots} (tn); % <-- new node
\end{forest}
\end{center}
\end{document}

相关内容