ó
    †ñ:iø,  ã                   óV   • S SK rSSKJr  SSKJr  SSKJrJr  SSK	J
r
   " S S	\
5      rg)
é    Né   )Úlinks)ÚModel)ÚMaskedModelÚpartition_tree_shuffleé   )Ú	Explainerc                   ó€   ^ • \ rS rSrSr\R                  S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S jr
S rSrU =r$ )ÚPermutationExplaineré	   a¤  This method approximates the Shapley values by iterating through permutations of the inputs.

This is a model agnostic explainer that guarantees local accuracy (additivity) by iterating completely
through an entire permutation of the features in both forward and reverse directions (antithetic sampling).
If we do this once, then we get the exact SHAP values for models with up to second order interaction effects.
We can iterate this many times over many random permutations to get better SHAP value estimates for models
with higher order interactions. This sequential ordering formulation also allows for easy reuse of
model evaluations and the ability to efficiently avoid evaluating the model when the background values
for a feature are the same as the current input value. We can also account for hierarchical data
structures with partition trees, something not currently implemented for KernalExplainer or SamplingExplainer.
NTc                 ó  >• [         R                  R                  U5        Uc  [        S5      e[        TU ]  XX5US9  [        U R                  [        5      (       d  [        U R                  5      U l        [        U5      S:”  a|   " S SU R                  5      nU R                  R                  R                  UR                  l        X€l
        UR                  5        H  u  pšX R                  R                  U	'   M     gg)a'  Build an explainers.Permutation 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(binary_mask, x)``.
    It takes a single input sample and a binary mask 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 using 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.Tabular(data, clustering="correlation")`` object.

seed: None or int
    Seed for reproducibility

**call_args : valid argument to the __call__ method
    These arguments are saved and passed to the __call__ method as the new default values for these arguments.

Nzmasker cannot be None.)ÚlinkÚlinearize_linkÚfeature_namesr   c                   ó:   ^ • \ rS rSrSSSSSSS.U 4S jjrSrU =r$ )	Ú;PermutationExplainer.__init__.<locals>.PermutationExplaineré>   éô  FÚautoN©Ú	max_evalsÚmain_effectsÚerror_boundsÚ
batch_sizeÚoutputsÚsilentc          
      ó.   >• [         TU ]  " UUUUUUUS.6$ )Nr   ©ÚsuperÚ__call__©	Úselfr   r   r   r   r   r   ÚargsÚ	__class__s	           €Ú_/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/shap/explainers/_permutation.pyr    ÚDPermutationExplainer.__init__.<locals>.PermutationExplainer.__call__?   s,   ø€ ô !™7Ò+ØØ"+Ø%1Ø%1Ø#-Ø 'Ø%òð ó    © )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r    Ú__static_attributes__Ú__classcell__©r$   s   @r%   r   r   >   s!   ø† ð "Ø!&Ø!&Ø%Ø Ø ÷ö r'   r   )ÚnpÚrandomÚseedÚ
ValueErrorr   Ú__init__Ú
isinstanceÚmodelr   Úlenr$   r    Ú__doc__ÚitemsÚ__kwdefaults__)r"   r6   Úmaskerr   r   r   r2   Ú	call_argsr   ÚkÚvr$   s              €r%   r4   ÚPermutationExplainer.__init__   sÊ   ø€ ô4 	�	‰	�‰�tÔà‰>ÜÐ5Ó6Ð6ä‰Ñ˜¨TÐ`mÐÑnä˜$Ÿ*™*¤e×,Ñ,Ü˜tŸz™zÓ*ˆDŒJô ˆy‹>˜AÓô t§~¡~ô ð* 59·N±N×4KÑ4K×4SÑ4SÐ ×)Ñ)Ô1Ø1ŒNØ!Ÿ™Ö)‘�Ø23—‘×,Ñ,¨QÓ/ò *ð3 r'   r   Fr   r   c          
      ó.   >• [         TU ]  " UUUUUUUS.6$ )z7Explain the output of the model on the given arguments.r   r   r!   s	           €r%   r    ÚPermutationExplainer.__call__Y   s,   ø€ ô ‰wÒØØØ%Ø%Ø!ØØò
ð 	
r'   c          
      óx  • [        U R                  U R                  U R                  U R                  /UQ76 nUS:X  a  S[        U5      -  nSn	[        U R                  SS5      b“  [        U R                  R                  [        R                  5      (       a  U R                  R                  n	OI[        U R                  R                  5      (       a  U R                  R                  " U6 n	O[        S5      eUR                  5       n
[        R                  " [        U5      [        S9nSXº'   [        R                  " S[        U
5      -  S	-   [         S9n[         R"                  US
'   US[        U
5      -  S	-   -  nSnSnS
nSn[        U
5      S
:”  Ga´  [%        U5       GHa  nU	b  ['        X«U	5        O[        R(                  R+                  U
5        S	nU
 H  nUUU'   US	-  nM     U
 H  nUUU'   US	-  nM     U" US
US9nUck  [        R                  " [        U5      4UR,                  S	S -   5      nU(       a4  [        R                  " SU-  [        U5      4UR,                  S	S -   5      nS
nU
 H<  nUU==   UUS	-      UU   -
  -  ss'   U(       a  UUS	-      UU   -
  UU   U'   US	-  nM>     US	-  nU
 H<  nUU==   UU   UUS	-      -
  -  ss'   U(       a  UU   UUS	-      -
  UU   U'   US	-  nM>     US	-  nGMd     US
:X  a!  [/        SU SS[        U
5      -  S	-    S35      eUS
   nU(       a  UR1                  X¤S9nO‘[        R                  " S	[         S9nU" US
S	S9nUS
   n[        R                  " [        U5      4UR,                  S	S -   5      nU(       a4  [        R                  " SU-  [        U5      4UR,                  S	S -   5      nUSU-  -  UUR2                  UU	Uc  SOUR5                  S
5      [7        U R                  S5      (       a  U R                  R8                  S.$ SS.$ )z_Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes).r   é   NÚ
clusteringzeThe masker passed has a .clustering attribute that is not yet supported by the Permutation explainer!)ÚdtypeTr   r   r   )Ú
zero_indexr   z
max_evals=zV is too low for the Permutation explainer, it must be at least 2 * num_features + 1 = Ú!)r   Úoutput_names)ÚvaluesÚexpected_valuesÚmask_shapesr   rD   Ú	error_stdrH   )r   r6   r;   r   r   r7   Úgetattrr5   rD   r0   ÚndarrayÚcallableÚNotImplementedErrorÚvarying_inputsÚzerosÚboolÚintÚdelta_mask_noop_valueÚranger   r1   ÚshuffleÚshaper3   r   rK   ÚstdÚhasattrrH   )r"   r   r   r   r   r   r   Úrow_argsÚfmÚrow_clusteringÚindsÚ	inds_maskÚmasksÚnpermutationsÚ
row_valuesÚrow_values_historyÚhistory_posÚmain_effect_valuesÚ_ÚiÚindÚexpected_values                         r%   Úexplain_rowÚ PermutationExplainer.explain_rown   s‹  € ô ˜Ÿ™ T§[¡[°$·)±)¸T×=PÑ=PÐ\ÐS[Ò\ˆð ˜ÓØ¤ R£Ñ(ˆIð ˆÜ�4—;‘; ¨dÓ3Ñ?Ü˜$Ÿ+™+×0Ñ0´"·*±*×=Ñ=Ø!%§¡×!7Ñ!7‘Ü˜$Ÿ+™+×0Ñ0×1Ñ1Ø!%§¡×!7Ò!7¸Ð!B‘ä)Ø{óð ð
 × Ñ Ó"ˆÜ—H’HœS ›W¬DÑ1ˆ	Øˆ	‰Ü—’˜œS ›Y™¨Ñ*´#Ñ6ˆÜ×4Ñ4ˆˆa‰Ø! a¬#¨d«)¡m°aÑ&7Ñ8ˆØˆ
Ø!ÐØˆØ!ÐÜˆt‹9�qŒ=Ü˜=×)�à!Ñ-ô +¨4¸NÕKä—I‘I×%Ñ% dÔ+ð �Û�CØ"�E˜!‘HØ˜‘F’Añ  ó  �CØ"�E˜!‘HØ˜‘F’Añ  ñ
 ˜U¨q¸ZÑH�àÑ%Ü!#§¢¬3¨r«7¨*°w·}±}ÀQÀRÐ7HÑ*HÓ!I�Jæ#Ü-/¯XªXà ! MÑ 1Ü # B£ðð &Ÿm™m¨A¨BÐ/ñ	0ó.Ð*ð �Û�CØ˜s“O w¨q°1©u¡~¸À¹
Ñ'BÑB“OÞ#Ø?FÀqÈ1Áu¹~ÐPWÐXYÑPZÑ?ZÐ*¨;Ñ7¸Ñ<Ø˜‘F’Añ	  ð
 ˜qÑ �Û�CØ˜s“O w¨q¡z°G¸AÀ¹E±NÑ'BÑB“OÞ#Ø?FÀq¹zÈGÐTUÐXYÑTYÉNÑ?ZÐ*¨;Ñ7¸Ñ<Ø˜‘F’Añ	  ð
 ˜qÑ “ñ] *ð`  Ó!Ü Ø   ð  ,Bð  CDô  GJð  KOó  GPñ  CPð  STñ  CTð  BUð  UVð  Wóð ð % Q™ZˆNö Ø%'§_¡_°T _Ð%QÐ"øä—H’H˜Q¤cÑ*ˆEÙ˜¨1¸Ñ;ˆGØ$ Q™ZˆNÜŸš¤3 r£7 *¨w¯}©}¸Q¸RÐ/@Ñ"@ÓAˆJÞÜ%'§X¢Xà˜MÑ)Ü˜B›ðð —m‘m A BÐ'ñ	(ó&Ð"ð ! A¨Ñ$5Ñ6Ø-ØŸ>™>Ø.Ø(Ø!3Ñ!;™ÐAS×AWÑAWÐXYÓAZÜ7>¸t¿z¹zÈ>×7ZÑ7Z˜DŸJ™J×3Ñ3ñ
ð 	
ð aeñ
ð 	
r'   c                 óH   • U " XUR                   S   -  US9nUR                  $ )aY  Legacy interface to estimate the SHAP values for a set of samples.

Parameters
----------
X : numpy.array or pandas.DataFrame or any scipy.sparse matrix
    A matrix of samples (# samples x # features) on which to explain the model's output.

npermutations : int
    Number of times to cycle through all the features, re-evaluating the model at each step.
    Each cycle evaluates the model function 2 * (# features + 1) times on a data matrix of
    (# background data samples) rows. An exception to this is when PermutationExplainer can
    avoid evaluating the model because a feature's value is the same in X and the background
    dataset (which is common for example with sparse features).

Returns
-------
array or list
    For models with a single output this returns a matrix of SHAP values
    (# samples x # features). Each row sums to the difference between the model output for that
    sample and the expected value of the model output (which is stored as expected_value
    attribute of the explainer). For models with vector outputs this returns a list
    of such matrices, one for each output.

r   )r   r   )rX   rI   )r"   ÚXra   r   r   Úbatch_evalsr   Úexplanations           r%   Úshap_valuesÚ PermutationExplainer.shap_valuesá   s*   € ñ2 ˜1¸¿¹À¹
Ñ(BÐQ]Ñ^ˆØ×!Ñ!Ð!r'   c                 ó   • g)Nz&shap.explainers.PermutationExplainer()r(   )r"   s    r%   Ú__str__ÚPermutationExplainer.__str__ý   s   € Ø7r'   )r$   r6   )é
   FFTF)r)   r*   r+   r,   r8   r   Úidentityr4   r    rj   rp   rs   r-   r.   r/   s   @r%   r   r   	   sT   ø† ñ
ð #(§.¡.ÀÐUYÐ`d÷@4ðL ØØØØØ÷
ð 
ò*q
ôf"÷88ð 8r'   r   )Únumpyr0   Ú r   Úmodelsr   Úutilsr   r   Ú
_explainerr	   r   r(   r'   r%   Ú<module>r|      s"   ðÛ å Ý ß 7Ý !ôu8˜9õ u8r'   