說明 TikZ 中的隨機森林演算法

說明 TikZ 中的隨機森林演算法

我試著說明一個的工作原理隨機森林在 TikZ 中結合這兩個數字。 TikZ 圖片應該具有第二個影像中存在的不同樹狀結構,同時也顯示通過單一樣本(紅點)的每棵樹的路徑,如第一個影像所示。

  1. 來源 在此輸入影像描述
  2. 來源 在此輸入影像描述

這是我到目前為止所擁有的。

在此輸入影像描述

\documentclass{standalone}

\usepackage{forest}

\begin{document}
\begin{forest} for tree={l sep=3em, s sep=3em, anchor=center, inner sep=0.7em, fill=blue!50, circle, font=\Large\sffamily}
  [Training Data, draw, rectangle, rounded corners, orange, text=white
    [,red!70[[][]][,red!70[[][]][,red!70[,red!70][]]]]
    [,red!70[,red!70[[][]][,red!70]][[][[][]]]]
    [,red!70[[][]][,red!70[,red!70[][,red!70]][]]]
  ]
\end{forest}
\end{document}

我仍在掙扎:

  1. 在周圍畫框並對每棵樹進行編號(樹 1、樹 2、樹 n),如第二張圖所示。
  2. 取得樹 2 和 n 之間的 3 個點。
  3. 沿著樣本穿過每棵樹的路徑繪製箭頭,如圖 1 所示。
  4. 將底部所有樹的結果與文本“分類的多數投票/回歸的平均值”相結合

任何幫助將不勝感激!

更新

感謝 user121799 的大力幫助,這就是完成的 TikZ 影像。

在此輸入影像描述

\documentclass[tikz]{standalone}

\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

應執行以下操作:

  1. 使用fit
  2. 新增適當的節點。
  3. 這裡
  4. 手動新增節點。

\documentclass[tikz]{standalone}
\usetikzlibrary{fit,shapes.arrows,positioning}
\usepackage{forest}
\tikzset{marrow/.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}}
\begin{document}
\begin{forest} 
for tree={l sep=3em, s sep=3em, anchor=center, inner sep=0.7em, fill=blue!50,
circle, font=\Large\sffamily,where level=1{no edge}{}}
  [Training Data, draw, rectangle, rounded corners, orange, text=white,alias=TD
    [,red!70,alias=a1[[,alias=a2][]][,red!70,edge label={node[above=1ex,marrow]{}}[[][]][,red!70,edge label={node[above=1ex,marrow]{}}[,red!70,edge label={node[below=1ex,marrow]{}}][,alias=a3]]]]
    [,red!70,alias=b1[,red!70,edge label={node[below=1ex,marrow]{}}[[,alias=b2][]][,red!70,edge label={node[above=1ex,marrow]{}}]][[][[][,alias=b3]]]]
    [~$\cdots$~,scale=4,no edge,fill=none,yshift=-1em]
    [,red!70,alias=c1[[,alias=c2][]][,red!70,edge label={node[above=1ex,marrow]{}}[,red!70,edge label={node[above=1ex,marrow]{}}[,alias=c3][,red!70,edge label={node[above=1ex,marrow]{}}]][,alias=c4]]]
  ]
\node[draw,fit=(a1)(a2)(a3)](f1){};  
\node[draw,fit=(b1)(b2)(b3)](f2){};  
\node[draw,fit=(c1)(c2)(c3)(c4)](f3){};  
\path (f1.south west)--(f3.south east) node[midway,below=4em] (David) {mean};
\node[below=2em of David] (pred){prediction};
\foreach \X in {1,2,3}{\draw[-stealth] (TD) -- (f\X.north);
\draw[-stealth] (f\X.south) -- (David);}
\draw[-stealth] (David) -- (pred);
\end{forest}
\end{document}

在此輸入影像描述

相關內容