使用“tikz”修改决策树终端节点

使用“tikz”修改决策树终端节点

我正在尝试创建决策树,但在尝试单独着色终端叶子时遇到了麻烦。我目前的代码如下所示(忽略表格):

在此处输入图片描述

但是,我想单独为终端叶子着色,但不确定如何操作,因为下面一行leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=green, text width=1cm}]对应于leaf只采用一个颜色值。我如何创建与下面类似的决策树?

在此处输入图片描述

代码:

\documentclass[]{article}


\usepackage{float}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,shapes,positioning,shadows,trees,patterns}
\definecolor{blue1}{HTML}{0081FF}
\definecolor{grey1}{HTML}{B0B0B0}


\begin{document}

\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{
      \resizebox{0.5\textwidth}{!}{
        \begin{tikzpicture}[->,>=stealth',
            level/.style={sibling distance = 5cm/#1, level distance = 2cm},
            basic/.style={draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
            split/.style={basic, rounded corners=2pt, thin, align=center, fill=blue1},
            leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=green, text width=1cm}] 
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$} 
            child{ node [leaf] {$\omega_{00}$} edge from parent node[above left] {$no$}}
            child{ node [split] {$x_1 < 1.78$}
            child{ node [leaf] {$\omega_{99}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{87}$} edge from parent node[above right] {$yes$}}
            }
            %child{ node [leaf] {$\omega_{01}$} edge from parent node[above right] {$yes$}}                      
            edge from parent node[above left] {$no$}}
            child{ node [split] {$x_2<1.25$}
            child{ node [leaf] {$\omega_{10}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{11}$} edge from parent node[above right] {$yes$}}
            edge from parent node[above right] {$yes$}
            };
        \end{tikzpicture}
      }
    }
    \subfloat[Oblivious Decision Tree]{
        \begin{tabular}{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}



\end{document}

答案1

由于节点内容不太明显,树的结构可能更明显,我将节点内容更改为字母 A、A1、A2 等,用于树的左侧,将节点内容更改为字母 B、B1 和 B2,用于树的右侧。将所需内容插入这些节点应该不是大问题

关于节点的着色:重新定义leaf样式

leaf/.style = {basic, fill=#1}

为节点添加所需颜色很简单。如下图所示,从所需树的图片可以看出,大多数节点leaf都是红色的,因此将默认颜色定义为红色并在例外情况下添加其他颜色是明智的(参见下面的 MWE):

\documentclass{article}

\usepackage{array}
\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,
                patterns,positioning,
                shadows,shapes,
                trees}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}
    \begin{figure}
    \centering
\subfloat[Oblivious Decision Tree]{
    \begin{tikzpicture}[
        ->,>=stealth',
        level/.style={sibling distance = 3cm/#1, level distance = 10mm},  
        basic/.style = {draw, drop shadow, font=\sffamily\small, text=white,
                        rounded corners=2pt, thin}, % <---
        split/.style = {basic, fill=blue},    % <---
         leaf/.style = {basic, fill=#1},      % <---
       leaf/.default = red,
          lbl/.style = {font=\small, #1}      % <---
                        ]
\node [split] {$x_1<0.75$}
    child{ node [split] {A}%{$x_2<1.25$}
        child{ node [leaf] {A1}%{$\omega_{00}$} 
                edge from parent node[lbl=left] {No}}
        child{ node [split] {A2}%{$x_1 < 1.78$}
             child{ node [leaf] {A21}%{$\omega_{99}$} 
                edge from parent node[lbl=left] {No}}
            child{ node [leaf=green] {A22}%{$\omega_{87}$} 
                edge from parent node[lbl=right] {Yes}}
            edge from parent node[lbl=right] {Yes}
            }
    edge from parent node[lbl=left] {No}
        }
%
    child{ node [split] {B}%{$x_2<1.25$}
        child{ node [leaf=green] {B1}%{$\omega_{10}$} 
            edge from parent node[above left] {No}}
        child{ node [leaf] {B2}%{$\omega_{11}$} 
            edge from parent node[above right] {Yes}}
        edge from parent node[right] {Yes}
        };
    \end{tikzpicture}
    }\hfill
\subfloat[Oblivious Decision Tree]{
    \setlength\extrarowheight{2pt}
    \renewcommand\arraystretch{1.1}
    \small
    \begin{tabular}[b]{cc cc}
        \hline
        \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
        \hline
        false & false & 00b & \( w_{00} \) \\
        false & true  & 01b & \( w_{01} \) \\
        true  & false & 10b & \( w_{10} \) \\
        true  & true  & 11b & \( w_{11} \) \\
        \hline
    \end{tabular}
}
\caption{}
\label{fig:decision-tree}
    \end{figure}
\end{document}

在此处输入图片描述

相关内容