
    0Ph                        d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z
 ddlmZmZmZmZ dd	lmZmZ d
dlmZmZmZmZ d
dlmZmZ d Z G d d          Z e            Z eeeg eeddd          dgddgg d eh d          gdgdgdgdgdg eeddd          dgd eeddd          dgdd          dddddddddddddd            Z G d d           Z G d! d"e          Z  G d# d$e          Z! ei d%dd&e"d ed'          gd( eeddd          dgd)ddgd*g dd+ eh d          gd,dgd-dgd.dgd/dgd0dgd1dgd2dgd3dgd4 eeddd          dgd5e"gd          	 d>dddddddddddddd6d7d8            Z#d9 Z$ eeegddgddg eeddd          dg eed
dd          dg eeddd          dgdgd:d          ddd;dddd<d=            Z%dS )?z:
This module defines export functions for decision trees.
    )Iterable)StringIO)IntegralN   )is_classifier)
HasMethodsInterval
StrOptionsvalidate_params)check_arraycheck_is_fitted   )DecisionTreeClassifierDecisionTreeRegressor
_criterion_tree)Treebuchheimc           
         g }d\  }}||z  }||z
  }t          j        ddd| z                                t                    D ]}|dz  }|dt	          |dz  dz
            z
  z  }||df||dfd||fd||f|d|f|d|f||dfg}	|	t          |                   \  }
}}t          d	|
|z   z            t          d	||z   z            t          d	||z   z            g}	|                    |	           |S )
a
  Generate n colors with equally spaced hues.

    Parameters
    ----------
    n : int
        The number of colors required.

    Returns
    -------
    color_list : list, length n
        List of n tuples of form (R, G, B) being the components of each color.
    )g      ?g?   i  g     v@g      N@r   r   r      )nparangeastypeintabsappend)n
color_listsvcmhh_barxrgbrgbs                T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/tree/_export.py_color_brewr,      s5    J DAq	AA	AAYr3	**11#66  DS%!)q)))* 1I1I1I1I1I1I1I
 c%jj/1aC1q5M""c#Q-&8&8Cq1u<N<NP#    c                       e Zd Zd ZdS )Sentinelc                     dS )Nz
"tree.dot" )selfs    r+   __repr__zSentinel.__repr__@   s    |r-   N)__name__
__module____qualname__r3   r1   r-   r+   r/   r/   ?   s#            r-   r/   left)closed
array-like)r9   booleanN>   allnonerootr:   no_validation)decision_tree	max_depthfeature_namesclass_nameslabelfilledimpuritynode_ids
proportionrounded	precisionaxfontsizeT)prefer_skip_nested_validationr;   F   )r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   c                    t          |            t          |||||||||	|
|          }|                    | |          S )a[  Plot a decision tree.

    The sample counts that are shown are weighted with any sample_weights that
    might be present.

    The visualization is fit automatically to the size of the axis.
    Use the ``figsize`` or ``dpi`` arguments of ``plt.figure``  to control
    the size of the rendering.

    Read more in the :ref:`User Guide <tree>`.

    .. versionadded:: 0.21

    Parameters
    ----------
    decision_tree : decision tree regressor or classifier
        The decision tree to be plotted.

    max_depth : int, default=None
        The maximum depth of the representation. If None, the tree is fully
        generated.

    feature_names : array-like of str, default=None
        Names of each of the features.
        If None, generic names will be used ("x[0]", "x[1]", ...).

    class_names : array-like of str or True, default=None
        Names of each of the target classes in ascending numerical order.
        Only relevant for classification and not supported for multi-output.
        If ``True``, shows a symbolic representation of the class name.

    label : {'all', 'root', 'none'}, default='all'
        Whether to show informative labels for impurity, etc.
        Options include 'all' to show at every node, 'root' to show only at
        the top root node, or 'none' to not show at any node.

    filled : bool, default=False
        When set to ``True``, paint nodes to indicate majority class for
        classification, extremity of values for regression, or purity of node
        for multi-output.

    impurity : bool, default=True
        When set to ``True``, show the impurity at each node.

    node_ids : bool, default=False
        When set to ``True``, show the ID number on each node.

    proportion : bool, default=False
        When set to ``True``, change the display of 'values' and/or 'samples'
        to be proportions and percentages respectively.

    rounded : bool, default=False
        When set to ``True``, draw node boxes with rounded corners and use
        Helvetica fonts instead of Times-Roman.

    precision : int, default=3
        Number of digits of precision for floating point in the values of
        impurity, threshold and value attributes of each node.

    ax : matplotlib axis, default=None
        Axes to plot to. If None, use current axis. Any previous content
        is cleared.

    fontsize : int, default=None
        Size of text font. If None, determined automatically to fit figure.

    Returns
    -------
    annotations : list of artists
        List containing the artists for the annotation boxes making up the
        tree.

    Examples
    --------
    >>> from sklearn.datasets import load_iris
    >>> from sklearn import tree

    >>> clf = tree.DecisionTreeClassifier(random_state=0)
    >>> iris = load_iris()

    >>> clf = clf.fit(iris.data, iris.target)
    >>> tree.plot_tree(clf)
    [...]
    r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rK   )rJ   )r   _MPLTreeExporterexport)r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   exporters                 r+   	plot_treerS   G   s`    n M"""#  H ??=R?000r-   c                   D    e Zd Z	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd	 Zd
 ZdS )_BaseTreeExporterNr;   FTrM   c                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        d S NrO   )r2   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rK   s               r+   __init__z_BaseTreeExporter.__init__   sW     #*&
  $" r-   c                    | j         d         wt          | j         d         t          j        |                             }t	          |d          }t          |          dk    rdn||d         |d         z
  d|d         z
  z  n^t          | j         d         d                   }|| j         d         d         z
  | j         d         d         | j         d         d         z
  z  fd|D             }d	t          |          z  S )
Nboundsr'   T)reverser   g        r   c           	      b    g | ]+}t          t          |z  d z
  dz  z   d                    ,S )r   r   r   )r   round).0r"   alphas     r+   
<listcomp>z/_BaseTreeExporter.get_color.<locals>.<listcomp>   s=    MMM!U519E	S'88!<<==MMMr-   z
#%2x%2x%2x)colorslistr   argmaxsortedlentuple)r2   valuecolorsorted_valuesr_   s       @r+   	get_colorz_BaseTreeExporter.get_color   s   ;x (U+BIe,<,<=>>E"5$777M=!!Q&&&q)M!,<<]STEUAUV U+A.//ET[2155H%a(4;x+@+CCE NMMMuMMMeEll**r-   c                    d| j         vrt          |j        d                   | j         d<   |j        dk    r=t	          j        |j                   t	          j        |j                   f| j         d<   nu|j        d         dk    rdt          t	          j	        |j
                            dk    r:t	          j        |j
                  t	          j        |j
                  f| j         d<   |j        dk    r_|j
        |         dd d f         }|j        d         dk    r6t          |t                    r!| j         d         |                                }n|j        |          }|                     |          S )Nr'   r   r   rZ   )ra   r,   	n_classes	n_outputsr   maxrE   minre   uniquerg   
isinstancer   itemrj   )r2   treenode_idnode_vals       r+   get_fill_colorz _BaseTreeExporter.get_fill_color   sN   ##!,T^A->!?!?DK~"" +-&*?*?)?"&BWBWAW(XH%%"a''C	$*0E0E,F,F!,K,K)+
););RVDJ=O=O(PH%>Qz'*1aaa40Hq!Q&&x22 'K)5 $==?? g..H~~h'''r-   c           	      	   |j         dk    r|j        |         dd d f         }n|j        |         }| j        dk    r|dk    p
| j        dk    }| j        }|d         }| j        r+|r|dz  }||d         t          |          z   |d         z   z  }|j        |         t          j        k    r| j	        .| j	        |j
        |                  }|                     |          }n d|d         |j
        |         |d	         }||d
|d         d
t          |j        |         | j                  |d         z  }| j        rt!          |t"          j                  rd}n:t!          |t"          j                  s|dk    rd}nt!          |t
                    sd}|r|d|z  z  }|t          t          |j        |         | j                            |d         z   z  }|r|dz  }| j        rXd|j        |         z  t-          |j        d                   z  }	|t          t          |	d                    dz   |d         z   z  }n&|t          |j        |                   |d         z   z  }| j        s!|j        d         dk    r||j        |         z  }|r|dz  }|j        d         dk    rt3          j        || j                  }
n| j        rt3          j        || j                  }
not3          j        t3          j        t3          j        |d          d                    r|                    t>                    }
nt3          j        || j                  }
t          |
                    d                                         dd          }
|
                     dd                               dd          }
|j        d         dk    r5|j         dk    r*|
                     dd                               dd          }
|
                     d|d                   }
||
|d         z   z  }| j!        |j        d         dk    r||j         dk    rq|r|dz  }| j!        dur5| j!        t3          j"        |                   }|                     |          }n'd|d         t3          j"        |          |d	         }||z  }|#                    |d                   r|d tI          |d                             }||d          z   S )!Nr   r   r=   r;   znode    r&   r    rM   friedman_msesquared_errorrE   z%s = z
samples = g      Y@%zvalue = S32zb''z' ',  []z
 zclass = Ty   )%rm   rg   rC   
charactersrF   strchildren_leftr   	TREE_LEAFrA   feature
str_escaper]   	thresholdrI   rE   rq   r   FriedmanMSEMSErG   n_node_samplesfloatrl   weighted_n_node_samplesr   aroundr;   equalmodr   r   replacerB   rc   endswithre   )r2   rs   rt   	criterionrg   labelsr   node_stringr   percent
value_text
class_names               r+   node_to_strz_BaseTreeExporter.node_to_str  s@   >QJw'111-EEJw'E *&77a<ODJ%<O_
 n = 	H 'w&:a=3w<<7*Q-GGK g&%/99!-,T\'-BC//'22 .
 qMML))qMM
 1dnW-t~>>>1	 K = 	)Z%;<< '*		Iz~66 '):V:V+			3// '&	 3w22E$-0$.AABBZPQ]RK
  	(<'K? 	M+G44uT=PQR=S7T7TT  3uWa0011C7*Q-GGKK3t27;<<z!}LLK  	B4>!#4#9#9D8AAE 	&:%K>!!!5$.99JJ_ 	:5$.99JJVBHRVE1--q1122 	:c**JJ 5$.99J**51122::4EE
''t44<<S"EE
>!!!dn&9&9#++C44<<S"EEJ''z!}==
zJqM11 (q!Q&&!##  *z)t++!-bi.>.>?
!__Z88

 ,
 qMMIe$$$qMM

 :%K 
1.. 	=%&;Z]););(;&;<KZ]**r-   c                     |S rW   r1   r2   strings     r+   r   z_BaseTreeExporter.str_escape  s    r-   NNNr;   FTFFFrM   N)r4   r5   r6   rX   rj   rv   r   r   r1   r-   r+   rU   rU      s         ! ! ! !4+ + +*( ( (>q+ q+ q+f    r-   rU   c                   ^     e Zd Zeddddddddddddddf fd	Zd Zd	 Zd
 ZddZd Z	 xZ
S )_DOTTreeExporterNr;   FTrM   	helveticac                     t                                          |||||||	|
||
  
         || _        || _        || _        || _        || _        |r
g d| _        n	g d| _        dg i| _        dd i| _	        d S )N
r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   )z&#35;z<SUB>z</SUB>z&le;z<br/>><)#r   r   <=z\n"r   leavesrZ   )
superrX   leaves_parallelout_filespecial_charactersfontnamerotater   ranksra   )r2   r   r@   rA   rB   rC   rD   r   rE   rF   rG   r   rH   r   rI   r   	__class__s                   r+   rX   z_DOTTreeExporter.__init__  s    $ 	'#! 	 	
 	
 	
  / "4   	EUUUDOODDDDO ^
&r-   c                    | j         Ht          | j                   |j        k    r+t          dt          | j                   |j        fz            |                                  t          |t          j                  r|                     |dd           n"|                     |j	        d|j
                   |                                  d S )NzALength of feature_names, %d does not match number of features, %dr   rE   r   )rA   re   n_features_in_
ValueErrorheadrq   r   r   recursetree_r   tail)r2   r?   s     r+   rQ   z_DOTTreeExporter.export  s     )4%&&-*FFF W4-..0LMN  
 			mUZ00 	TLLZL@@@@LL,a=;RLSSS		r-   c           	         | j         r_t          | j                  D ]J}| j                            dd                    d | j        |         D                       z   dz              K| j                            d           d S )Nz{rank=same ; z; c              3      K   | ]}|V  d S rW   r1   )r^   r(   s     r+   	<genexpr>z(_DOTTreeExporter.tail.<locals>.<genexpr>  s"      /L/La/L/L/L/L/L/Lr-   z} ;
})r   rd   r   r   writejoin)r2   ranks     r+   r   z_DOTTreeExporter.tail  s     	tz**  ###dii/L/L4:d;K/L/L/L&L&LLwV    	C     r-   c                    | j                             d           | j                             d           g }| j        r|                    d           | j        r|                    d           t          |          dk    r0| j                             dd                    |          z             | j                             d| j        z             | j                             d	           | j        r| j                             d
           | j                             d| j        z             | j	        r| j                             d           d S d S )Nzdigraph Tree {
znode [shape=boxrD   rH   r   z, style="%s", color="black"r   z, fontname="%s"] ;
z,graph [ranksep=equally, splines=polyline] ;
zedge [fontname="%s"] ;
zrankdir=LR ;
)
r   r   rD   r   rH   re   r   r   r   r   )r2   rounded_filleds     r+   r   z_DOTTreeExporter.head  s^   ./// 	-...; 	,!!(+++< 	-!!),,,~""M-		.0I0II   	-=>>>G$$$  	QM OPPP6FGGG; 	2M 011111	2 	2r-   r   c           	         |t           j        k    rt          dt           j        z            |j        |         }|j        |         }| j        || j        k    rT|t           j        k    r.| j        d                             t          |                     nvt          |          | j        vr&t          |          g| j        t          |          <   n:| j        t          |                                       t          |                     | j	        
                    d||                     |||          fz             | j        r1| j	        
                    d|                     ||          z             | j	        
                    d           || j	        
                    d||fz             |dk    rt          j        dd	g          | j        d
z
  dz  z  }| j	        
                    d           |dk    r$| j	        
                    d|d         z             n#| j	        
                    d|d         z             | j	        
                    d           |t           j        k    r<|                     |||||dz              |                     |||||dz              d S d S | j        d                             t          |                     | j	        
                    d|z             | j        r| j	        
                    d           | j	        
                    d|z             |!| j	        
                    d||fz             d S d S )NzInvalid node_id %sr   z%d [label=%sz, fillcolor="%s"r   z%d -> %dr   -   i      ?z  [labeldistance=2.5, labelangle=r   z%d, headlabel="True"]z%d, headlabel="False"]z ;
)r   parentdepthz%d [label="(...)"z, fillcolor="#C0C0C0"z%d -> %d ;
)r   r   r   r   children_rightr@   r   r   r   r   r   r   rD   rv   r   arrayr   r   )	r2   rs   rt   r   r   r   
left_childright_childangless	            r+   r   z_DOTTreeExporter.recurse  sd   eo%%1EOCDDD'0
)'2 >!Udn%<%<U_,,
8$++CLL9999U4:--*-g,,
3u::&&
3u::&--c'll;;;M'4+;+;D'9+U+U!VV   { ##&)<)<T7)K)KK   M(((!##J&'1B$BCCCQ;;Xr3i00T[35F"4LMFM''(JKKK!||++,CfQi,OPPPP++,Dvay,PQQQ##F+++U_,,'"!)     '"!)       -," Jx ''G555M 3g =>>>{ =##$;<<<M' 1222!##Nfg5F$FGGGGG "!r-   c                 .    |                     dd          S )Nr   z\")r   r   s     r+   r   z_DOTTreeExporter.str_escapeC  s    ~~c5)))r-   )Nr   )r4   r5   r6   SENTINELrX   rQ   r   r   r   r   __classcell__r   s   @r+   r   r     s          !-' -' -' -' -' -'^  (! ! !2 2 26CH CH CH CHJ* * * * * * *r-   r   c                   N     e Zd Z	 	 	 	 	 	 	 	 	 	 	 d fd	ZddZdd	Zdd
Z xZS )rP   Nr;   FTrM   c                    t                                          |||||||||	|

  
         || _        dg i| _        dd i| _        g d| _        t                      | _        | j        r
d| j        d<   t          d          | _	        d S )	Nr   r   rZ   )r   r   r   r   
r   r   r]   boxstylez<-)
arrowstyle)
r   rX   rK   r   ra   r   dict	bbox_argsrH   
arrow_args)r2   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rK   r   s               r+   rX   z_MPLTreeExporter.__init__I  s     	'#! 	 	
 	
 	
 ! ^
&===< 	1)0DN:&$///r-   r   c                 f   |                      |||          }|j        |         t          j        k    ra| j        || j        k    rO|                     |j        |         |||dz             |                     |j        |         |||dz             g}nt          ||          S t          ||g|R  S )Nr   r   r   )r   r   r   r   r@   
_make_treer   r   )r2   rt   etr   r   namechildrens          r+   r   z_MPLTreeExporter._make_treeq  s     GyAAG$77N"et~&=&= $W-r9EAI     %g.IUQY    	HH g&&&D'-H----r-   c                    dd l m} ddlm ||                                }|                                 |                                 |                     d|j        |j	                  }t          |          }|                                dz   \  }}|                                j        }|                                j        }	||z  }
|	|z  }|                     ||j        |||           fd|                                D             }|j        j                                        }|D ]}|                    |           | j        fd|D             }t-          d |D                       }t-          d |D                       }|d                                         t1          |
|z  ||z            z  }|D ]}|                    |           |S )Nr   )
Annotationr   c                 4    g | ]}t          |          |S r1   )rq   )r^   annr   s     r+   r`   z+_MPLTreeExporter.export.<locals>.<listcomp>  s(    PPPJsJ4O4OPPPPr-   c                 d    g | ],}|                                 x                                -S rW   )get_bbox_patchget_window_extent)r^   r   
bbox_patchs     r+   r`   z+_MPLTreeExporter.export.<locals>.<listcomp>  sF       "%"4"4"6"66JC ,,..CCCr-   c                     g | ]	}|j         
S r1   )widthr^   extents     r+   r`   z+_MPLTreeExporter.export.<locals>.<listcomp>  s    @@@fV\@@@r-   c                     g | ]	}|j         
S r1   )heightr   s     r+   r`   z+_MPLTreeExporter.export.<locals>.<listcomp>  s    BBBfmBBBr-   )matplotlib.pyplotpyplotmatplotlib.textr   gcaclearset_axis_offr   r   r   r   max_extentsr   r   r   r   get_childrenfigurecanvasget_rendererupdate_bbox_position_sizerK   rn   get_fontsizero   set_fontsize)r2   r?   rJ   pltmy_tree	draw_treemax_xmax_yax_width	ax_heightscale_xscale_yannsrendererr   extents	max_width
max_heightsizer   r   s                      @@r+   rQ   z_MPLTreeExporter.export  s!   ''''''......:B




//!]%8-:QRRW%%	 !,,..2u''))/((**1	U"e#Y 3RFFFPPPPr00PPP 9#0022 	4 	4C))(3333=      G
 @@@@@AAIBB'BBBCCJ7''))C)#Wz%9- - D  ' '  &&&&r-   c           
      ~   dd l m} t          dd|z  z
  d          }| j        
| j        |d<   t          ddd| j                                        | j                                        d|}	|j        d	         |	d
         d<   |j        dz   |z  ||j	        z
  dz
  |z  f}
| j
        || j
        k    rG| j        r*|                     ||j        j                  |	d         d<   n|                                |	d         d<   |j         |j        |j        j        |
fi |	 n|j        j        dz   |z  ||j        j	        z
  dz
  |z  f} |j        |j        j        ||
fi |	 |j        j        a|d         |
d         z   dz  |d         |
d         z   dz  f}|j                                        |k    rd\  }}nd\  }} |j        ||fd|i| |j        D ] }|                     ||||||dz              !d S |j        j        dz   |z  ||j        j	        z
  dz
  |z  f}d|	d         d<    |j        d||
fi |	 d S )Nr   d   
   zaxes fraction)zorderxycoordsrK   center)havabbox
arrowpropsz
text.colorr  	edgecolorr   r  fcr   r   )zTrue  right)z  Falser7   r  r   greyz
  (...)  
r1   )r   r   r   rK   r   copyr   rcParamsr&   r   r@   rD   rv   rs   rt   get_facecolorr   annotaterC   r7   r   r   )r2   noders   rJ   r  r  r   r   common_kwargskwargsxy	xy_parenttext_pos
label_textlabel_hachilds                   r+   r   z_MPLTreeExporter.recurse  s   '''''' e#$
 
 
 =$(,M*%  
$$&&++--	
 

 
 
 -0L,F|[) v|u$utv~';u&DE>!Udn%<%<{ :'+':':4AR'S'Svt$$')'7'7'9'9vt${"DIOR::6:::: []S(E1T[]*S0E9	 DIOYEEfEEE ;%- #11-2"11-2 H {''))T11/B,
HH/B,
HBK
HSSS]SSS M MUD"eU%!)LLLLM M
 $-&,5I $*F6N4 BKBAA&AAAAAr-   r   )r   rW   )r4   r5   r6   rX   r   rQ   r   r   r   s   @r+   rP   rP   H  s         &0 &0 &0 &0 &0 &0P. . . .&1 1 1 1f?B ?B ?B ?B ?B ?B ?B ?Br-   rP   r?   r   r   r@   rA   rB   rC   rD   r   rE   rF   rG   r   rH   r   rI   r   r   )r@   rA   rB   rC   rD   r   rE   rF   rG   r   rH   r   rI   r   c                V   |t          |ddd          }|(t          |t                    st          |ddd          }t          |            d}d}	 t          |t                    rt          |dd          }d}|d}t                      }t          |||||||||	|
|||||	          }|                    |            |r0|j	        
                                |r|                                 S S 	 |r|                                 dS dS # |r|                                 w w xY w)
a  Export a decision tree in DOT format.

    This function generates a GraphViz representation of the decision tree,
    which is then written into `out_file`. Once exported, graphical renderings
    can be generated using, for example::

        $ dot -Tps tree.dot -o tree.ps      (PostScript format)
        $ dot -Tpng tree.dot -o tree.png    (PNG format)

    The sample counts that are shown are weighted with any sample_weights that
    might be present.

    Read more in the :ref:`User Guide <tree>`.

    Parameters
    ----------
    decision_tree : object
        The decision tree estimator to be exported to GraphViz.

    out_file : object or str, default=None
        Handle or name of the output file. If ``None``, the result is
        returned as a string.

        .. versionchanged:: 0.20
            Default of out_file changed from "tree.dot" to None.

    max_depth : int, default=None
        The maximum depth of the representation. If None, the tree is fully
        generated.

    feature_names : array-like of shape (n_features,), default=None
        An array containing the feature names.
        If None, generic names will be used ("x[0]", "x[1]", ...).

    class_names : array-like of shape (n_classes,) or bool, default=None
        Names of each of the target classes in ascending numerical order.
        Only relevant for classification and not supported for multi-output.
        If ``True``, shows a symbolic representation of the class name.

    label : {'all', 'root', 'none'}, default='all'
        Whether to show informative labels for impurity, etc.
        Options include 'all' to show at every node, 'root' to show only at
        the top root node, or 'none' to not show at any node.

    filled : bool, default=False
        When set to ``True``, paint nodes to indicate majority class for
        classification, extremity of values for regression, or purity of node
        for multi-output.

    leaves_parallel : bool, default=False
        When set to ``True``, draw all leaf nodes at the bottom of the tree.

    impurity : bool, default=True
        When set to ``True``, show the impurity at each node.

    node_ids : bool, default=False
        When set to ``True``, show the ID number on each node.

    proportion : bool, default=False
        When set to ``True``, change the display of 'values' and/or 'samples'
        to be proportions and percentages respectively.

    rotate : bool, default=False
        When set to ``True``, orient tree left to right rather than top-down.

    rounded : bool, default=False
        When set to ``True``, draw node boxes with rounded corners.

    special_characters : bool, default=False
        When set to ``False``, ignore special characters for PostScript
        compatibility.

    precision : int, default=3
        Number of digits of precision for floating point in the values of
        impurity, threshold and value attributes of each node.

    fontname : str, default='helvetica'
        Name of font used to render text.

    Returns
    -------
    dot_data : str
        String representation of the input tree in GraphViz dot format.
        Only returned if ``out_file`` is None.

        .. versionadded:: 0.18

    Examples
    --------
    >>> from sklearn.datasets import load_iris
    >>> from sklearn import tree

    >>> clf = tree.DecisionTreeClassifier()
    >>> iris = load_iris()

    >>> clf = clf.fit(iris.data, iris.target)
    >>> tree.export_graphviz(clf)
    'digraph Tree {...
    NFr   	ensure_2ddtypeensure_min_sampleswzutf-8)encodingT)r   r@   rA   rB   rC   rD   r   rE   rF   rG   r   rH   r   rI   r   )r   rq   boolr   r   openr   r   rQ   r   getvalueclose)r?   r   r@   rA   rB   rC   rD   r   rE   rF   rG   r   rH   r   rI   r   own_filereturn_stringrR   s                      r+   export_graphvizr7    s   V  #U$1
 
 
 z+t'D'D!5
 
 
 M"""HM!h$$ 	HcG<<<HH MzzH#'#+!1
 
 
" 	&&& 	0$--//  	NN			0  	NN	 	8 	NN	s   BD D(c                 b    fdg } |d| j         | j        |           t          |          S )z:
    Returns the depth of the subtree rooted in node.
    c                     ||gz  }||          }||          }|dk    r,|dk    r( ||dz   |||            ||dz   |||           d S d S d S )Nrx   r   r1   )current_nodecurrent_depthr   r   depthsr7   r  compute_depth_s          r+   r=  z&_compute_depth.<locals>.compute_depth_  s     	=/!\*|,2::%2++Nma'   N}q(-    	 :++r-   r   )r   r   rn   )rs   r!  r<  r=  s      @r+   _compute_depthr>    sM    
     FN4D.0CVLLLv;;r-   )r?   rA   rB   r@   spacingdecimalsshow_weightsr  )rA   rB   r@   r?  r@  rA  c                   	
 t          ddd          t          ddd          t                       j        t                     rd j        nZt                    t           j                  k    r5t          dt                     dt           j                   d          dd		d
>t                    j        k    r&t          dj        t                    fz            t           t                    rdsdndfdj
        D             nd j
        D             dt          _         fd	
fd
 
dd           t          j        S )a9	  Build a text report showing the rules of a decision tree.

    Note that backwards compatibility may not be supported.

    Parameters
    ----------
    decision_tree : object
        The decision tree estimator to be exported.
        It can be an instance of
        DecisionTreeClassifier or DecisionTreeRegressor.

    feature_names : array-like of shape (n_features,), default=None
        An array containing the feature names.
        If None generic names will be used ("feature_0", "feature_1", ...).

    class_names : array-like of shape (n_classes,), default=None
        Names of each of the target classes in ascending numerical order.
        Only relevant for classification and not supported for multi-output.

        - if `None`, the class names are delegated to `decision_tree.classes_`;
        - otherwise, `class_names` will be used as class names instead of
          `decision_tree.classes_`. The length of `class_names` must match
          the length of `decision_tree.classes_`.

        .. versionadded:: 1.3

    max_depth : int, default=10
        Only the first max_depth levels of the tree are exported.
        Truncated branches will be marked with "...".

    spacing : int, default=3
        Number of spaces between edges. The higher it is, the wider the result.

    decimals : int, default=2
        Number of decimal digits to display.

    show_weights : bool, default=False
        If true the classification weights will be exported on each leaf.
        The classification weights are the number of samples each class.

    Returns
    -------
    report : str
        Text summary of all the rules in the decision tree.

    Examples
    --------

    >>> from sklearn.datasets import load_iris
    >>> from sklearn.tree import DecisionTreeClassifier
    >>> from sklearn.tree import export_text
    >>> iris = load_iris()
    >>> X = iris['data']
    >>> y = iris['target']
    >>> decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    >>> decision_tree = decision_tree.fit(X, y)
    >>> r = export_text(decision_tree, feature_names=iris['feature_names'])
    >>> print(r)
    |--- petal width (cm) <= 0.80
    |   |--- class: 0
    |--- petal width (cm) >  0.80
    |   |--- petal width (cm) <= 1.75
    |   |   |--- class: 1
    |   |--- petal width (cm) >  1.75
    |   |   |--- class: 2
    NFr   r+  zaWhen `class_names` is an array, it should contain as many items as `decision_tree.classes_`. Got z  while the tree was fitted with z	 classes.z{} {} <= {}
z{} {} >  {}
z{} {}
z.feature_names must contain %d elements, got %dz{}{} weights: {}
z{}{}{}
z{}{} value: {}
c                 D    g | ]}|t           j        k    r|         nd S rW   )r   TREE_UNDEFINED)r^   irA   s     r+   r`   zexport_text.<locals>.<listcomp>K  s@     
 
 
 !"U%9 9 9M!t
 
 
r-   c                 8    g | ]}d                      |          S )z
feature_{}format)r^   rE  s     r+   r`   zexport_text.<locals>.<listcomp>P  s&    HHHQ,--a00HHHr-   r   c                    d}t          t                    rLr4fd| D             }dd                    |          d d         z   dz   } |dt          |          z   z  }n1fd| D             }dd                    |          d d         z   dz   }t          xj                            |d|          z  c_        d S )Nr   c                 B    g | ]}d                      |z            S z{1:.{0}f}, rG  )r^   r!   r@  r   s     r+   r`   z2export_text.<locals>._add_leaf.<locals>.<listcomp>X  s?        "((17N3NOO  r-   r   r   r   z class: c                 <    g | ]}d                      |          S rK  rG  )r^   r!   r@  s     r+   r`   z2export_text.<locals>._add_leaf.<locals>.<listcomp>`  s)    DDD=''!44DDDr-   )rq   r   r   r   export_textreportrH  )	rg   r   r   indentvalr@  r?   rA  	value_fmts	    `   r+   	_add_leafzexport_text.<locals>._add_leafT  s    m%;<< 	0 (    "   BGGCLL"--3'':J//CCDDDDeDDDCSbS))C/Ci..vr3???r-   c                    ddz  z   |z  }|d           dz  z   }d }j         dk    rj        |          d         }nj        |          j        d         }t          j        |          }j        d         dk    rj         dk    r|         }j        |          }|dz   k    rd}|}|}j        |          t          j	        k    r߉|          }	j
        |          }
d                    |
          }
t          xj                            ||	|
          z  c_        t          xj        |z  c_         j        |          |dz              t          xj                            ||	|
          z  c_        t          xj        |z  c_         j        |          |dz              d S  ||||           d S t!          |           }|dk    r ||||           d S d|z  }t          xj                            ||          z  c_        d S )	N|rz   -r   r   r   z	{1:.{0}f}ztruncated branch of depth %d)rm   rg   Tr   rc   rl   r   r   r   rD  r   rH  rM  rN  r   r   r>  )r!  r   rO  rg   r   r   info_fmtinfo_fmt_leftinfo_fmt_rightr   r   subtree_depthtrunc_reportrR  rB   r@  feature_names_left_child_fmtr@   print_tree_recurseright_child_fmtr?  r   truncation_fmts                r+   r^  z'export_text.<locals>.print_tree_recursed  sK   w'50	'	"S7]2?aK%a(EEK%'*EYu%%
?1""u!';';$Z0J"'"?"EIM!!H$M%N}T"e&:::%d+!OD1	'..xCC	""o&<&<VT9&U&UU""""m3""""5#6t#<eaiHHH""n&;&;FD)&T&TT""""n4""""5#7#=uqyIIIII	%!8*fMMMMM*5$77M!!	%!8*fMMMMM=M""n&;&;FL&Q&QQ""""r-   r   )r   r   r   r   classes_re   r   
n_featuresrq   r   r   rM  rN  )r?   rA   rB   r@   r?  r@  rA  rR  r\  r]  r^  r_  r   r`  rQ  s   ```````@@@@@@@@r+   rM  rM    s~   p  #U$1
 
 
 !5
 
 
 M"""E]## 	'0KK]%;!<!<<<;$$; ; .//; ; ;   &O$NN S%7%75;K%K%K<]!3!345
 
 	

 -!788 '(	 	#"I&	 
 
 
 
]
 
 

 IH%-HHHK@ @ @ @ @ @ @ @ (R (R (R (R (R (R (R (R (R (R (R (R (R (R (RT q!r-   rW   )&__doc__collections.abcr   ior   numbersr   numpyr   baser   utils._param_validationr   r	   r
   r   utils.validationr   r   r   r   r   r   r   _reingold_tilfordr   r   r,   r/   r   rS   rU   r   rP   r   r7  r>  rM  r1   r-   r+   <module>rl     so    % $ $ $ $ $                             W W W W W W W W W W W W ; ; ; ; ; ; ; ; N N N N N N N N N N N N - - - - - - - -' ' 'T       
 8:: 02GHhxD@@@$G&-666*444556+KK k;hxD@@@$GXh4???F  #'!  * 
t1 t1 t1 t1% $t1nC C C C C C C CLo* o* o* o* o*( o* o* o*dnB nB nB nB nB( nB nB nBb S$

7 3 34 	hhxD@@@$G 	,-	
 	666 	**444556 	9+ 	I; 	YK 	YK 	yk 	9+ 	I; 	yk 	hhxD@@@$G  	SE!$ #''  . c 
#c c c c+ *cL  0 02GH&-$d+hxD@@@$GHXq$v>>>EXh4???F"  #'   w w w w w w wr-   