在 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圖表適合 a\documentclass[]{article}而不是 a\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}

相關內容