ó
    †ñ:iNA  ã                   óÔ   • S SK r S SKrS SKJr  SSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJr  \ R                   " S	5      r " S
 S\5      r\S 5       r\S 5       rS rS rS rS rS rg)é    N)Únjité   )Úlinks)ÚModel)ÚMaskedModelÚdelta_minimization_orderÚ
make_masksÚshapley_coefficientsé   )Ú	ExplainerÚshapc                   ót   ^ • \ rS rSrSr\R                  SS4U 4S jjrSSSSS	SS
.U 4S jjrS r	S r
SrU =r$ )ÚExactExplaineré   aT  Computes SHAP values via an optimized exact enumeration.

This works well for standard Shapley value maskers for models with less than ~15 features that vary
from the background per sample. It also works well for Owen values from hclustering structured
maskers when there are less than ~100 features that vary from the background per sample. This
explainer minimizes the number of function evaluations needed by ordering the masking sets to
minimize sequential differences. This is done using gray codes for standard Shapley values
and a greedy sorting method for hclustering structured maskers.
TNc                 óü   >• [         TU ]  XX4US9  [        U5      U l        [	        USS5      bG  [        UR                  5      u  U l        U l        [        UR                  U R                  5      U l
        0 U l        g)a  Build an explainers.Exact object for the given model using the given masker object.

Parameters
----------
model : function
    A callable python object that executes the model given a set of input data samples.

masker : function or numpy.array or pandas.DataFrame
    A callable python object used to "mask" out hidden features of the form `masker(mask, *fargs)`.
    It takes a single a binary mask and an input sample and returns a matrix of masked samples. These
    masked samples are evaluated using the model function and the outputs are then averaged.
    As a shortcut for the standard masking used by SHAP you can pass a background data matrix
    instead of a function and that matrix will be used for masking. To use a clustering
    game structure you can pass a shap.maskers.TabularPartitions(data) object.

link : function
    The link function used to map between the output units of the model and the SHAP value units. By
    default it is shap.links.identity, but shap.links.logit can be useful so that expectations are
    computed in probability units while explanations remain in the (more naturally additive) log-odds
    units. For more details on how link functions work see any overview of link functions for generalized
    linear models.

linearize_link : bool
    If we use a non-linear link function to take expectations then models that are additive with respect to that
    link function for a single background sample will no longer be additive when using a background masker with
    many samples. This for example means that a linear logistic regression model would have interaction effects
    that arise from the non-linear changes in expectation averaging. To retain the additively of the model with
    still respecting the link function we linearize the link function by default.

)ÚlinkÚlinearize_linkÚfeature_namesÚ
clusteringN)ÚsuperÚ__init__r   ÚmodelÚgetattrÚpartition_masksr   Ú_partition_masksÚ_partition_masks_indsÚpartition_delta_indexesÚ_partition_delta_indexesÚ_gray_code_cache)Úselfr   Úmaskerr   r   r   Ú	__class__s         €ÚY/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/shap/explainers/_exact.pyr   ÚExactExplainer.__init__   st   ø€ ô> 	‰Ñ˜¨TÐ`mÐÑnä˜5“\ˆŒ
ä�6˜<¨Ó.Ñ:Ü@OÐPV×PaÑPaÓ@bÑ=ˆDÔ! 4Ô#=Ü,CÀF×DUÑDUÐW[×WlÑWlÓ,mˆDÔ)à "ˆÕó    i † FÚautor   ©Ú	max_evalsÚmain_effectsÚerror_boundsÚ
batch_sizeÚinteractionsÚsilentc          
      ó.   >• [         TU ]  " UUUUUUUS.6$ )zZExplains the output of model(*args), where args represents one or more parallel iterators.r'   )r   Ú__call__)	r    r(   r)   r*   r+   r,   r-   Úargsr"   s	           €r#   r/   ÚExactExplainer.__call__G   s,   ø€ ô ‰wÒØØØ%Ø%Ø!Ø%Øò
ð 	
r%   c                 ón   • XR                   ;  a  [        U5      U R                   U'   U R                   U   $ )N)r   Úgray_code_indexes)r    Úns     r#   Ú_cached_gray_codesÚ!ExactExplainer._cached_gray_codes^   s4   € Ø×)Ñ)Ó)Ü'8¸Ó';ˆD×!Ñ! !Ñ$Ø×$Ñ$ QÑ'Ð'r%   c          	      óÊ  • [        U R                  U R                  U R                  U R                  /UQ76 n	Sn
[        U R                  SS5      Gc  U	R                  5       n
Ub6  US:w  a0  US[        U
5      -  :  a  [        SS[        U
5      -   SU S35      eU R                  [        U
5      5      n[        R                  " S[        U
5      -  [        S9n[        S[        U
5      -  5       H*  nX½   [         R                  :X  a  X½   XÍ'   M!  X«U      XÍ'   M,     U	" US	US
9nUSL d  US:X  aŠ  USLa…  [        [        U
5      5      n[        R                  " [        U	5      4UR                   SS -   5      n[        R                  " [        U	5      ["        S9n[%        UUX¥Xì[         R                  5        GOUSL d  US:X  aŽ  [        [        U
5      5      n[        R                  " [        U	5      [        U	5      4UR                   SS -   5      n[        R                  " [        U	5      ["        S9n['        UUX¥Xì[         R                  5        OæUS:”  a  [)        S5      eOÔUb6  US:w  a0  U[        U	5      S-  :  a  [        S[        U	5      S-   SU S35      eU R*                  nU	" X´S9n[        R                  " [        U	5      4UR                   SS -   5      n[        [        U	5      5       HA  nXPR,                  U   S      nXPR,                  U   S	      nUU-
  R/                  S	5      Xý'   MC     SnU(       d  USL d  US:X  ab  U
c  [        R0                  " [        U	5      5      n
U	R3                  U
5      nUSL d  US:X  a$  [        [        U	5      5       H  nUU   WXÝ4'   M     WUS	   U	R4                  U(       a  UOS[        U R                  SS5      S.$ )z_Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes).Nr   r&   r   z	It takes zO masked evaluations to run the Exact explainer on this instance, but max_evals=Ú!©Údtyper   )Ú
zero_indexr+   Fr   TzPCurrently the Exact explainer does not support interactions higher than order 2!)r+   )ÚvaluesÚexpected_valuesÚmask_shapesr)   r   )r   r   r!   r   r   r   Úvarying_inputsÚlenÚ
ValueErrorr5   ÚnpÚzerosÚintÚrangeÚdelta_mask_noop_valuer
   ÚshapeÚboolÚ_compute_grey_code_row_valuesÚ _compute_grey_code_row_values_stÚNotImplementedErrorr   r   ÚmeanÚaranger)   r>   )r    r(   r)   r*   r+   Úoutputsr,   r-   Úrow_argsÚfmÚindsÚdelta_indexesÚextended_delta_indexesÚiÚcoeffÚ
row_valuesÚmaskÚ
on_outputsÚoff_outputsÚmain_effect_valuess                       r#   Úexplain_rowÚExactExplainer.explain_rowc   sß  € ô ˜Ÿ™ T§[¡[°$·)±)¸T×=PÑ=PÐ\ÐS[Ò\ˆð ˆÜ�4—;‘; ¨dÓ3Ò;à×$Ñ$Ó&ˆDð Ñ$¨°fÓ)<ÀÈQÔRUÐVZÓR[É^ÓA[Ü Ø ¤S¨£Y¡Ð/Ð/~ð  @Ið  Jð  JKð  Lóð ð !×3Ñ3´C¸³IÓ>ˆMô &(§X¢X¨a´3°t³9©nÄCÑ%HÐ"Ü˜1¤ D£	™>Ö*�Ø Ñ#¤{×'HÑ'HÓHØ0=Ñ0@Ð*Ó-à04À1Ñ5EÑ0FÐ*Ó-ñ	 +ñ Ð/¸AÈ*ÑUˆGð ˜uÒ$¨¸Ó):¸|ÐSWÒ?Wä,¬S°«YÓ7�ÜŸXšX¤s¨2£w j°7·=±=ÀÀÐ3DÑ&DÓE�
Ü—x’x¤ B£¬tÑ4�Ü-Ø  d°UÔT_×TuÑTuöð
  Ò%¨¸Ó):ä,¬S°«YÓ7�ÜŸXšX¤s¨2£w´°B³Ð&8¸7¿=¹=ÈÈÐ;LÑ&LÓM�
Ü—x’x¤ B£¬tÑ4�Ü0Ø  d°UÔT_×TuÑTuõð  Ó!Ü)Øfóð ð "ð Ñ$¨°fÓ)<ÀÌSÐQSËWÐXYÉ\ÓAYÜ Ø¤ B£¨1¡˜~Ð-|ð  ~Gð  }Hð  HIð  Jóð ð !×9Ñ9ˆMñ ˜Ñ>ˆGô Ÿš¤3 r£7 *¨w¯}©}¸Q¸RÐ/@Ñ"@ÓAˆJÜœ3˜r›7–^�Ø$×%?Ñ%?ÀÑ%BÀ1Ñ%EÑF�
Ø%×&@Ñ&@ÀÑ&CÀAÑ&FÑG�Ø!+¨kÑ!9× ?Ñ ?ÀÓ B�
“ñ $ð "ÐÞ˜<¨4Ò/°<À1Ó3DØ‰|Ü—y’y¤ R£Ó)�Ø!#§¡°Ó!6ÐØ˜tÒ# |°qÓ'8Üœs 2›wž�AØ'9¸!Ñ'<�J˜q˜tÓ$ñ (ð !Ø& q™zØŸ>™>Þ2>Ñ.ÀDÜ! $§+¡+¨|¸TÓBñ
ð 	
r%   )r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Úidentityr   r/   r5   r[   Ú__static_attributes__Ú__classcell__)r"   s   @r#   r   r      sK   ø† ñð ,1¯>©>È$Ð^b÷ '#ðX ØØØØØ÷
ð 
ò.(÷
b
ð b
r%   r   c                 ó  • Sn[        U5      n[        SU-  5       Hl  n	XY   n
X¦:w  a  X   ) X'   X   (       a  US-  nOUS-  nXGS-
     nXx:  a  XG   nX9   nU H+  nX   (       a  X==   XÛ-  -  ss'   M  X==   UW-  -  ss'   M-     Mn     g ©Nr   r   r   ©r@   rE   )rV   rW   rQ   rN   Úshapley_coeffrS   Ú	noop_codeÚset_sizeÚMrT   Ú	delta_indÚon_coeffÚ	off_coeffÚoutÚjs                  r#   rI   rI   È   s¢   € à€HÜˆD‹	€AÜ�1�a‘4Ž[ˆà*Ñ-ˆ	ØÓ!Ø#™Ð.ˆD‰OØ�Ø˜A‘‘à˜A‘�ð !¨A¡Ñ.ˆØ‹<Ø%Ñ/ˆIØ‰jˆÛˆAØ�wØ“ ¡Ñ/•à“  y¡Ñ0•ó	 ò r%   c                 óÈ  • Sn[        U5      n[        SU-  5       HÃ  n	XY   n
X¦:w  a  X   ) X'   X   (       a  US-  nOUS-  nX9   n[        U5       H‰  n[        US-   U5       Hs  nX   (       d  X   (       d  X´U   -  nO:X   (       d	  X   (       d  X   (       a  X   (       d  U* XGS-
     -  nO
X´US-
     -  nXU4==   U-  ss'   XU4==   U-  ss'   Mu     M‹     MÅ     g rf   rg   )rV   rW   rQ   rN   rh   rS   ri   rj   rk   rT   rl   ro   rp   ÚkÚdeltas                  r#   rJ   rJ   â   sí   € à€HÜˆD‹	€AÜ�1�a‘4Ž[ˆà*Ñ-ˆ	ØÓ!Ø#™Ð.ˆD‰OØ�Ø˜A‘‘à˜A‘�ð ‰jˆÜ�q–ˆAÜ˜1˜q™5 !–_�Ø—w t§wØ°Ñ"9Ñ9‘EØŸ' d§g°4·7À4Ç7Ø ˜D =¸A±Ñ#>Ñ>‘Eà°¸1±Ñ"=Ñ=�EØ˜a˜4Ó  EÑ)Ó Ø˜a˜4Ó  EÑ)Õ ó %ó ò r%   c                 óÐ  • [         R                  " UR                  S   [        S9n/ n[	        [        U5      5       H‘  n[         R                  " X!USS24   -  5      S   nUSS  H  nUR                  U* S-
  5        M     [        U5      S:X  a   UR                  [        R                  5        OUR                  USS 5        XSS24   nM“     [         R                  " U5      $ )zgReturn an delta index encoded array of all the masks possible while following the given partition tree.r   r9   Nr   éÿÿÿÿ)rB   rC   rG   rH   rE   r@   ÚwhereÚappendr   rF   ÚextendÚarray)Úpartition_treeÚ	all_masksrW   Ú
delta_indsrT   rQ   rp   s          r#   r   r   þ   sÆ   € ô �8Š8�I—O‘O AÑ&¬dÑ3€DØ€JÜ”3�y“>Ö"ˆÜ�xŠx˜¨ªA¨™Ñ.Ó/°Ñ2ˆà�c�r“ˆAØ×Ñ˜q˜b 1™fÖ%ñ äˆt‹9˜‹>Ø×Ñœk×?Ñ?Õ@à×Ñ˜d 2 3˜iÔ(ØšA˜‰Šñ #ô �8Š8�JÓÐr%   c                 óî  • U R                   S   S-   n[        U 5      n/ n[        R                  " U[        S9nUR                  U5        UR                  U) 5        [        U5       Vs/ s H  n/ / /PM	     nn[        [        U 5      S-
  USSXbXU5	        [        R                  " U5      n[        U5      n[        R                  " [        U5      5      [        R                  " U5         nU H2  u  pš[        [        U	5      5       H  nX‰U      X•'   XŠU      X¥'   M     M4     X7   nU VVs/ s H1  u  pÍ[        R                  " U5      [        R                  " U5      /PM3     nnnX¾4$ s  snf s  snnf )zSReturn an array of all the masks possible while following the given partition tree.r   r   r9   )rG   r	   rB   rC   rH   rw   rE   Ú_partition_masks_recurser@   ry   r   rM   Úargsort)rz   rk   Úmask_matrixr{   Úm00rT   Ú
inds_listsÚorderÚinverse_orderÚ
inds_list0Ú
inds_list1r   ÚonÚoffÚpartition_masks_indss                  r#   r   r     sP  € à×Ñ˜QÑ !Ñ#€AÜ˜^Ó,€KØ€IÜ
�(Š(�1œDÑ
!€CØ×Ñ�SÔØ×Ñ�c�TÔä$)¨!¤HÓ-¢H˜q�2�r“(¡H€JÐ-ÜœS Ó0°1Ñ4°c¸1¸aÀÐZhÐmvÔwä—’˜Ó#€Iô % YÓ/€EÜ—I’Iœc %›jÓ)¬"¯*ª*°UÓ*;Ñ<€Mã",Ñˆ
Ü”s˜:“Ö'ˆAØ)°Q©-Ñ8ˆJ‰MØ)°Q©-Ñ8ˆJ‹Mó (ñ #-ð  Ñ&€OÙISÔTÊ¹g¸bœRŸXšX b›\¬2¯8ª8°C«=Ó9ÉÐÑTØÐ0Ð0ùò+ .ùó( Us   Á)E,Ä.8E1c	                 óB  • U S:  a3  X@U-      S   R                  U5        X@U-      S   R                  U5        g [        X`S4   U-
  5      n	[        X`S4   U-
  5      n
UR                  5       nUS S === XYU-   S S 24   -  sss& UR                  5       nUS S === XZU-   S S 24   -  sss& [        U5      nUR                  U5        [        U5      nUR                  U5        [	        X‘X.XEXgU5	        [	        X«XãXEXgU5	        [	        XœXÓXEXgU5	        [	        X¡X-XEXgU5	        g )Nr   r   )rw   rD   Úcopyr@   r~   )Úindexr�   Úind00Úind11r‚   r€   rz   rk   r{   Ú
left_indexÚright_indexÚm10Úm01Úind01Úind10s                  r#   r~   r~   4  s4  € ØˆqƒyØ˜1‘9Ñ˜aÑ ×'Ñ'¨Ô.Ø˜1‘9Ñ˜aÑ ×'Ñ'¨Ô.Øô �^¨1 HÑ-°Ñ1Ó2€JÜ�n¨A XÑ.°Ñ2Ó3€Kð �(‰(‹*€CØ‰ƒFˆk q™.ª!Ð+Ñ,Ñ,ƒFØ
�(‰(‹*€CØ‰ƒFˆk¨™/ª1Ð,Ñ-Ñ-ƒFô �	‹N€EØ×Ñ�SÔÜ�	‹N€EØ×Ñ�SÔô ˜Z¨e¸JÐUcÐhqÔrÜ˜[¨u¸ZÐVdÐirÔsÜ˜Z¨e¸JÐUcÐhqÔrÜ˜[¨u¸ZÐVdÐirÕsr%   c                 óB  • [         R                  " SU -  U 4[        S9n[         R                  " U [        S9n[        SSU -  S-   5       HQ  nUS-  S:X  a/  [        SU * S5       H  nX$   S:X  d  M  X$S-
     S-  X$S-
  '     O   OUS   S-  US'   X!US-
  SS24'   MS     U$ )z°Produces an array of all binary patterns of size nbits in gray code order.

This is based on code from: http://code.activestate.com/recipes/576592-gray-code-generatoriterator/
r   r9   r   ru   N)rB   rC   rH   rE   ©Únbitsro   ÚliÚtermrT   s        r#   Úgray_code_masksrš   T  s²   € ô
 �(Š(�A�u‘H˜eÐ$¬DÑ
1€CÜ	�Š�%œtÑ	$€Bä�a˜!˜u™*¨Ñ)Ö*ˆØ�!‰8�q‹=Ü˜2 ˜v rÖ*�Ø‘5˜A•:Ø " q¡5¡	¨A¡�B˜1‘u‘IÙò +ð
 ˜‘V˜a‘ZˆBˆr‰FàˆD�1‰H’aˆKÓñ +ð €Jr%   c                 óx  • [         R                  " SU -  [        S9[        R                  -  n[         R
                  " U [        S9n[        SU -  S-
  5       H^  nUS-  S:X  a=  [        SU * S5       H)  nX$   S:X  d  M  X$S-
     S-  X$S-
  '   XS-
  -   XS-   '     ME     MI  US   S-  US'   U S-
  XS-   '   M`     U$ )zÅProduces an array of which bits flip at which position.

We assume the masks start at all zero and -1 means don't do a flip.
This is a more efficient representation of the gray_code_masks version.
r   r9   r   ru   )rB   ÚonesrD   r   rF   rC   rH   rE   r–   s        r#   r3   r3   i  sÆ   € ô �'Š'�!�U‘(¤#Ñ
&¬×)JÑ)JÑ
J€CÜ	�Š�%œtÑ	$€BÜ�q˜E‘z QÑ&Ö'ˆØ�!‰8�q‹=Ü˜2 ˜v rÖ*�Ø‘5˜A•:Ø " q¡5¡	¨A¡�B˜1‘u‘IØ$)°©U¡O�C˜q™‘MÚó	 +ð ˜‘V˜a‘ZˆBˆr‰FØ! A™IˆC�q‘‹Mñ (ð €Jr%   )ÚloggingÚnumpyrB   Únumbar   Ú r   Úmodelsr   Úutilsr   r   r	   r
   Ú
_explainerr   Ú	getLoggerÚlogr   rI   rJ   r   r   r~   rš   r3   © r%   r#   Ú<module>r§      s†   ðÛ ã Ý å Ý ÷ó õ "à×Ò˜Ó€ôr
�Yô r
ðj ñ1ó ð1ð2 ñ*ó ð*ò6 ò&1òFtò@ó*r%   