ó
    †ñ:i–*  ã                  ó^  • S SK Jr  S SKrS SKrS SKJrJr  S SKr	S SK
rS SKrS SKrS SKrS SKJr  SSKJr  SSKJr  \(       a  SS	KJr  SS
 jrS r\S 5       r\SS j5       r\S 5       r\S 5       r\S 5       rSS jr     S           SS jjr     S           SS jjr!g)é    )ÚannotationsN)ÚTYPE_CHECKINGÚLiteral)Únjité   )ÚDimensionErroré   )Úshow_progress)Ú
_ArrayLikec                óL  • U [         R                  R                  " U R                  6 S-  -   n[        R
                  R                  R                  UR                  UR                  5       5      R                  US9n[        R                  R                  R                  U5      $ )Nç:Œ0âŽyE>©Úmetric)ÚnpÚrandomÚrandnÚshapeÚscipyÚspatialÚdistanceÚpdistÚfillnaÚmeanÚTÚclusterÚ	hierarchyÚcomplete)ÚXr   ÚX_full_rankÚDs       ÚY/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/shap/utils/_clustering.pyÚpartition_treer"      sw   € Ø”b—i‘i—o’o q§w¡wÐ/°$Ñ6Ñ6€KÜ�‰×Ñ×$Ñ$ [×%7Ñ%7¸×8HÑ8HÓ8JÓ%K×%MÑ%MÐV\Ð$Ð]€AÜ�=‰=×"Ñ"×+Ñ+¨AÓ.Ð.ó    c                óV   • [        U5      n[        UR                  S   S-
  XX#S5        g)a’  Randomly shuffle the indexes in a way that is consistent with the given partition tree.

Parameters
----------
indexes: np.array
    The output location of the indexes we want shuffled. Note that len(indexes) should equal index_mask.sum().
index_mask: np.array
    A bool mask of which indexes we want to include in the shuffled list.
partition_tree: np.array
    The partition tree we should follow.

r   r	   N)ÚlenÚ_pt_shuffle_recr   )ÚindexesÚ
index_maskr"   ÚMs       r!   Úpartition_tree_shuffler*      s,   € ô 	ˆJ‹€Aä�N×(Ñ(¨Ñ+¨aÑ/°ÀnÐYZÕ[r#   c                ó<  • U S:  a  X U-      (       a  X-   X'   US-   $ U$ [        X0S4   U-
  5      n[        X0S4   U-
  5      n[        R                  R                  5       S:  a  [	        XaX#XE5      n[	        XqX#XE5      nU$ [	        XqX#XE5      n[	        XaX#XE5      nU$ )Nr   r	   )Úintr   r   r   r&   )Úir'   r(   r"   r)   ÚposÚleftÚrights           r!   r&   r&   -   s¯   € àˆ1ƒuà˜!‘e×Ø™5ˆG‰LØ˜‘7ˆNàˆJÜˆ~ ˜dÑ# aÑ'Ó(€DÜ� !˜tÑ$ qÑ(Ó)€EÜ	‡y�y‡�Ó˜1ÓÜ˜d¨ZÈÓPˆÜ˜e¨jÈ!ÓQˆð €Jô ˜e¨jÈ!ÓQˆÜ˜d¨ZÈÓPˆØ€Jr#   c           	     ó  • [         R                  " [        U 5      5      n[        U5       H\  n[	        [        SU5      5       H@  n[        S[        U5      U-
  5       H!  n[        XXe5      S:”  d  M  [        X6U5        M#     MB     M^     U$ )Nr   r	   r   )r   Úaranger%   ÚrangeÚlistÚ_reverse_window_score_gainÚ_reverse_window)Ú	all_masksÚmax_swap_sizeÚ
num_passesÚorderÚ_Úlengthr-   s          r!   Údelta_minimization_orderr=   A   ss   € ä�IŠI”c˜)“nÓ%€EÜ�:ÖˆÜœ5  MÓ2Ö3ˆFÜ˜1œc %›j¨6Ñ1Ö2�Ü-¨iÀÓJÈQÕNÜ# E¨fÖ5ó 3ó 4ñ ð
 €Lr#   c                óx   • [        US-  5       H(  nXU-      nXU-   U-
  S-
     XU-   '   X@X-   U-
  S-
  '   M*     g )Nr   r	   )r3   )r:   Ústartr<   r-   Útmps        r!   r6   r6   L   sR   € ä�6˜Q‘;ÖˆØ˜A‘IÑˆØ ¨¡°!Ñ!3°aÑ!7Ñ8ˆ�a‰iÑØ(+ˆe‰n˜qÑ  1Ñ$Ó%ò  r#   c                óæ   • [        XUS-
        XU      5      [        XX#-   S-
        XX#-         5      -   n[        XUS-
        XX#-   S-
        5      [        XU      XX#-         5      -   nXE-
  $ )Nr	   )Ú_mask_delta_score)Úmasksr:   r?   r<   Úforward_scoreÚreverse_scores         r!   r5   r5   T   s¦   € ä% e°%¸!±)Ñ,<Ñ&=¸uÈ5Á\Ñ?RÓSÔVgØ�E‘N QÑ&Ñ'Ñ(¨%°e±nÑ0EÑ*FóWñ €Mô & e°%¸!±)Ñ,<Ñ&=¸uÈ5É>Ð\]ÑK]ÑE^Ñ?_Ó`ÔctØ�E‰lÑ˜U¨©Ñ#8Ñ9ódñ €Mð Ñ(Ð(r#   c                ó&   • X-  R                  5       $ )N)Úsum)Úm1Úm2s     r!   rB   rB   `   s   € à‰G�=‰=‹?Ðr#   c                óF  • [         R                  R                  R                  X5      n[         R                  R
                  R                  U5      n[         R                  R
                  R                  [         R                  R
                  R                  XC5      5      $ )z\A leaf ordering is under-defined, this picks the ordering that keeps nearby samples similar.)	r   r   r   r   r   r   r   Úleaves_listÚoptimal_leaf_ordering)r   r   Úanchor_firstr    Úcluster_matrixs        r!   Úhclust_orderingrO   e   si   € ô 	�‰×Ñ×$Ñ$ QÓ/€AÜ—]‘]×,Ñ,×5Ñ5°aÓ8€NÜ�=‰=×"Ñ"×.Ñ.¬u¯}©}×/FÑ/F×/\Ñ/\Ð]kÓ/oÓpÐpr#   c                óÄ  • SSK n[        R                  R                  XUS9u  p‰p«U R                  S   n/ n/ n[        U5       H–  nUR                  UUUSUS9nUR                  USS2XÿS-   24   X©SS2XÿS-   24   U4/SS9  UR                  UR                  USS2XÿS-   24   5      5        UR                  UR                  U	SS2XÿS-   24   5      5        M˜     [        R                  " U5      R                  n[        R                  " U5      R                  n[        R                  " XÌ45      n[        [        R                   " [        U5      [        U5      5      XÌ-  S9 Hô  u  nnUU:X  a  M  [        R"                  " USS2U4   5      nUS	:  a  [$        R&                  " S
U S35        SnOœUR                  UUUSUS9nUR                  USS2UUS-   24   USS2U4   U	SS2UUS-   24   USS2U4   4/SS9  [)        SS[        R*                  " USS2U4   UR                  U	SS2UUS-   24   5      -
  S-  5      U-  -
  5      nSU-
  UUU4'   Mö     U$ )aŽ  Compute redundancy distances scaled from 0-1 among all the features in X relative to the label y.

Distances are measured by training univariate XGBoost models of y for all the features, and then
predicting the output of these models using univariate XGBoost models of other features. If one
feature can effectively predict the output of another feature's univariate XGBoost model of y,
then the second feature is redundant with the first with respect to y. A distance of 1 corresponds
to no redundancy while a distance of 0 corresponds to perfect redundancy (measured using the
proportion of variance explained). Note these distances are not symmetric.

Returns
-------
np.ndarray
    A square matrix of shape (n_features, n_features) containing the pairwise
    redundancy distances between features. Each element [i, j] represents the
    redundancy distance from feature i to feature j with respect to y.

r   N©Úrandom_stater	   )Ú	subsampleÚn_estimatorsÚlearning_rateÚ	max_depthÚearly_stopping_roundsF)Úeval_setÚverbose)Útotalg-Cëâ6?z!No/low signal found from feature z™ (this is typically caused by constant or near-constant features)! Cluster distances can't be computed for it (so setting all redundancy distances to 1).r   )ÚxgboostÚsklearnÚmodel_selectionÚtrain_test_splitr   r3   ÚXGBRegressorÚfitÚappendÚpredictr   Úvstackr   Úzerosr
   ÚitÚproductÚvarÚwarningsÚwarnÚmaxr   )r   ÚyrU   rW   rS   Úmax_estimatorsrR   r[   ÚX_trainÚX_testÚy_trainÚy_testÚnum_featuresÚtrain_preds_listÚtest_preds_listr-   ÚmodelÚtrain_predsÚ
test_predsÚdistÚjÚ	preds_varÚr2s                          r!   Úxgboost_distances_r2r{   m   s°  € ó4 ô (/×'>Ñ'>×'OÑ'OÐPQÐcoÐ'OÐ'pÑ$€G�Wð —7‘7˜1‘:€LØÐØ€OÜ�<Ö ˆØ×$Ñ$ØØ'Ø'ØØ"7ð %ð 
ˆð 	�	‰	�'š!˜Q Q¡˜Y˜,Ñ'¨ÂAÀqÈqÉ5ÀyÀLÑ=QÐSYÐ<ZÐ;[Ðejˆ	ÑkØ×Ñ §¡¨g²a¸À¹U¸°lÑ.CÓ DÔEØ×Ñ˜uŸ}™}¨V²A°q¸q¹5°y°LÑ-AÓBÖCñ !ô —)’)Ð,Ó-×/Ñ/€KÜ—’˜?Ó+×-Ñ-€Jô �8Š8�\Ð0Ó1€DÜÜ
�
Š
”5˜Ó&¬¨lÓ(;Ó<ØÑ)ô‰ˆˆ1ð �‹6Ùô Ÿ6š6 *ªQ°¨TÑ"2Ó3ˆ	Ø�tÓÜ�MŠMØ3°A°3ð 72ð 2ôð
 ‰Bð ×(Ñ(Ø#Ø+Ø+ØØ&;ð )ð ˆEð �I‰IØš˜1˜q 1™u˜9˜Ñ%ØšA˜q˜DÑ!Ø!¢! Q¨¨Q© Y ,Ñ/°ºA¸q¸DÑ1AÐBÐCØð	 ñ ô �Q˜œBŸGšG Z²°1°Ñ%5¸¿¹ÀfÊQÐPQÐTUÐXYÑTYÐPYÈ\ÑFZÓ8[Ñ%[Ð`aÑ$aÓbÐenÑnÑnÓoˆBØ˜‘VˆˆQ�ˆT‹
ñCðF €Kr#   c           	     ó®  • [        U [        R                  5      (       a  U R                  nO[        R
                  " U 5      n[        UR                  5      S:w  a  [        S5      eSnX&;  a  [        SU 35      eUS:X  a  Ub  SnOSnUS:X  aÒ  [        XQUS	9n/ n[        R                  " [        [        U5      5      S5       H‚  u  pšUS
:X  a%  UR                  [        XyU
4   XzU	4   5      5        M0  US:X  a%  UR                  [!        XyU
4   XzU	4   5      5        M[  US:X  d  Mc  UR                  XyU
4   XzU	4   -   S-  5        M„     [        R
                  " U5      nOçUb  ["        R$                  " S5        UR'                  5       n[        UR                  S   5       H;  n	[        R(                  " USS2U	4   [        R*                  " USS2U	4   5      SS9  M=     [,        R.                  R0                  R3                  UR4                  [        R6                  R8                  " UR4                  R                  6 S-  -   US9nUS
:X  a)  [,        R:                  R<                  R?                  U5      $ US:X  a)  [,        R:                  R<                  RA                  U5      $ US:X  a)  [,        R:                  R<                  RC                  U5      $ g)aŸ  Fit a hierarchical clustering model for features X relative to target variable y.

For more information on clustering methods, see :external+scipy:func:`scipy.cluster.hierarchy.linkage`.

For more information on scipy distance metrics, see :external+scipy:func:`scipy.spatial.distance.pdist`.

Parameters
----------
X: 2d-array-like
    Features to cluster
y: array-like or None
    Target variable
linkage: str
    Defines the method to calculate the distance between clusters. Must be
    one of "single", "complete" or "average".
metric: str
    Scipy distance metric or "xgboost_distances_r2".

    * If ``xgboost_distances_r2``, estimate redundancy distances between
      features X with respect to target variable y using
      :func:`shap.utils.xgboost_distances_r2`.
    * Otherwise, calculate distances between features using the given
      distance metric.
    * If ``auto`` (default), use ``xgboost_distances_r2`` if target variable
      is provided, or else ``cosine`` distance metric.
random_state: int or np.random.RandomState
    Numpy random state, defaults to 0.

Returns
-------
clustering: np.array
    The hierarchical clustering encoded as a linkage matrix.

r   z/X needs to be a 2-dimensional array-like object)Úsingler   ÚaveragezUnknown linkage type: ÚautoNr{   ÚcosinerQ   r}   r   r~   ztIgnoring the y argument passed to shap.utils.hclust since the given clustering metric is not based on label fitting!r	   F)ÚnanÚcopyr   r   )"Ú
isinstanceÚpdÚ	DataFrameÚvaluesr   Úarrayr%   r   r   Ú
ValueErrorr{   re   Úcombinationsr3   ra   Úminrj   rh   ri   r‚   Ú
nan_to_numÚnanmeanr   r   r   r   r   r   r   r   r   r}   r   r~   )r   rk   Úlinkager   rR   ÚX_arrÚknown_linkagesÚ	dist_fullÚ	dist_listr-   rx   rw   Ú	bg_no_nans                r!   Úhclustr“   Æ   sg  € ôR �!”R—\‘\×"Ñ"Ø—‘‰ä—’˜“ˆÜ
ˆ5�;‰;Ó˜1ÓÜÐNÓOÐOà6€NØÓ$ÜÐ1°'°Ð;Ó<Ð<à�ÓØ‰=Ø+‰FàˆFð Ð'Ó'Ü 4°UÈLÑ Yˆ	ð "$ˆ	Ü—O’O¤E¬#¨i«.Ó$9¸1Ö=‰DˆAØ˜(Ó"Ø× Ñ ¤ Y°!¨t¡_°iÀ1À±oÓ!FÖGØ˜JÓ&Ø× Ñ ¤ Y°!¨t¡_°iÀ1À±oÓ!FÖGØ˜IÕ%Ø× Ñ  )¨q¨D¡/°IÀ¸d±OÑ"CÀqÑ!HÖIñ >ô �xŠx˜	Ó"‰ð ‰=Ü�MŠMð.ôð !&§
¡
£ˆ	Ü�y—‘ qÑ)Ö*ˆAÜ�MŠM˜)¢A q D™/¬r¯zªz¸)ÂAÀqÀD¹/Ó/JÐQVÔWñ +ä�}‰}×%Ñ%×+Ñ+¨I¯K©K¼"¿)¹)¿/º/È9Ï;É;×K\ÑK\Ð:]Ð`dÑ:dÑ,dÐmsÐ+Ðtˆð �(ÓÜ�}‰}×&Ñ&×-Ñ-¨dÓ3Ð3Ø	�JÓ	Ü�}‰}×&Ñ&×/Ñ/°Ó5Ð5Ø	�IÓ	Ü�}‰}×&Ñ&×.Ñ.¨tÓ4Ð4ð 
r#   )Úcorrelation)éd   r   )ÚsqeuclideanF)g333333ã?r   g      ð?i'  r   )rU   ÚfloatrW   ú
int | NonerS   zfloat | Nonerl   r˜   rR   úint | np.random.RandomStateÚreturnú
np.ndarray)Nr}   r   r   )r   r   rk   z_ArrayLike | Noner�   z(Literal['single', 'complete', 'average']r   ÚstrrR   r™   rš   r›   )"Ú
__future__r   Ú	itertoolsre   rh   Útypingr   r   Únumpyr   Úpandasr„   Úscipy.clusterr   Úscipy.spatialr\   Únumbar   Úutils._exceptionsr   Ú_show_progressr
   Ú_typesr   r"   r*   r&   r=   r6   r5   rB   rO   r{   r“   © r#   r!   Ú<module>r©      sP  ðÝ "ã Û ß )ã Û Û Û Û Ý å .Ý )æÝ"ô/ò\ð$ ñó ðð& óó ðð ñ,ó ð,ð ñ)ó ð)ð ñó ðôqð Ø()Ø!Ø!'Ø01ðVð ðVð &ð	Vð
 ðVð ðVð .ðVð õVðv  Ø8@ØØ01ðZ5ØðZ5àðZ5ð 6ðZ5ð ð	Z5ð
 .ðZ5ð öZ5r#   