
    ӆhJ                         S SK r S SKrS SKrS SKrS SKJr  0 q	S r
S rS rS rS r0 qS rS	 rS
 rS rS rS rS rSS jrSS jrS rg)    N)tqdmc
                 &   [         R                  " S5        XX4Xg4n
SnS[        ;   aO  [        S [	        [        S   U
5       5       5      (       a'  [
        R                  " [        S   U:H  5      (       a  Sn[        X5      u  pUR                  S   UR                  S   :X  d   eU" 5       n[
        R                  " UR                  5      n[
        R                  " UR                  5      n[
        R                  " UR                  5      n[        UR                  S   5      S-  n[        R                  S	S
5      n[        R                  SS
5      n[        [        [        U5      5      S5       H  nU(       a  UU   U U   :X  a
  UU   UU'   M   U U   S:X  a  UR                  UUUS-    5      S   UU'   MH  XS
S
& X>S
S
& [
        R                  " UUS
S
24   * U-   5      nUS
S
2US
U U    4   R!                  5       US
S
2US
U U    4'   US
S
2US
U U    4   R!                  5       UUUS
U U    4'   UR#                  X5        UR                  UUUS-    5      S   UU'   M     U [        S	'   U[        S'   U[        S'   U
[        S'   U" XO5      $ )a  The model is retrained for each test sample with the important features set to a constant.

If you want to know how important a set of features is you can ask how the model would be
different if those features had never existed. To determine this we can mask those features
across the entire training and test datasets, then retrain the model. If we apply compare the
output of this retrained model to the original model we can see the effect produced by knowning
the features we masked. Since for individualized explanation methods each test sample has a
different set of most important features we need to retrain the model for every test sample
to get the change in model performance when a specified fraction of the most important features
are withheld.
IThe retrain based measures can incorrectly evaluate models in some cases!Fargsc              3   ,   #    U  H
  u  pXL v   M     g 7fN .0abs      jC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\shap/benchmark/measures.py	<genexpr>!remove_retrain.<locals>.<genexpr>    s     C"B$!qv"B   	attr_testT   ư>nmaskNyp_masked_testz"Retraining for the 'remove' metricr   )warningswarn_remove_cacheallzipnpto_arrayshapezeros
const_randgetr   rangelenpredictargsortmeanfit)r   X_trainy_trainX_testy_testr   model_generatormetrictrained_modelrandom_stater   cache_matchmodel_maskedX_train_tmp
X_test_tmpr   tie_breaking_noise
last_nmasklast_yp_masked_testiorderings                        r   remove_retrainr9      s    MM]^ foFDKC#mF&;T"BCCC}]hOimvOvHwHwKw/OG ==v||A... #$L ((7==)K&,,'JXXfll+N#GMM!$45<""7D1J'++,<dC%F$&JK:a=E!H4 3A 6N11X] - 5 5fQQ6G H KN1 %N"qMzz9QT?"25G"GHH3:1hzq>R;R3S3X3X3ZK8JeAh//029!Xja=Q:Q2R2W2W2YJq(:U1X../ [2 , 4 4ZAE5J KA NN1 L$ #M'&4M"#!*M+ M&&))    c
                    [        X5      u  pUR                  S   UR                  S   :X  d   eUR                  5       n
[        UR                  S   U	5      S-  nUR	                  S5      n[        [        U5      5       H?  nX   S:  d  M  [        R                  " X]SS24   * U-   5      nXSX       XUSX    4'   MA     UR                  U
5      nU" XO5      $ )zKEach test sample is masked by setting the important features to a constant.r   r   r   N)
r   r   copyr    r&   r"   r#   r   r%   r$   )r   r(   r)   r*   r+   r   r,   r-   r.   r/   r3   r4   	mean_valsr7   r8   r   s                   r   remove_maskr>   L   s     w/OG ==v||A... J#GMM!$4lCdJQI3v;8a<zz9T?"25G"GHH2;Zux<P2QJ(:UX../  
 #**:6N&))r:   c
                 :   [        X5      u  pUR                  S   UR                  S   :X  d   e[        R                  " UR                  5      n
U
[        R
                  " U
R                  S   5      S-  -  n
UR                  5       n[        R                  " UR                  5      n[        UR                  S   U	5      S-  nUR                  S5      n[        [        U5      5       H  nX   S:  d  M  [        R                  " X_SS24   * U-   5      nUX   S nUSX    n[        R                  R                  U
USS24   SS2U4   5      nU
USS24   SS2U4   nUU   UU-  X?U4   UU   -
  -  -   nUXU4'   M     UR                  U5      nU" XL5      $ )aj  The model is reevaluated for each test sample with the important features set to an imputed value.

Note that the imputation is done using a multivariate normality assumption on the dataset. This depends on
being able to estimate the full data covariance matrix (and inverse) accuractly. So X_train.shape[0] should
be significantly bigger than X_train.shape[1].
r   r   r   Nr   r   r   covTeyer<   r   r    r&   r"   r#   r%   linalginvr$   )r   r(   r)   r*   r+   r   r,   r-   r.   r/   Cr3   r   r4   r=   r7   r8   observe_indsimpute_indsCoo_invCioimputes                         r   remove_imputerL   c   s    w/OG ==v||A... 	wyyA
	d	""AJXXfll+N#GMM!$4lCdJQI3v;8a<zz9T?"25G"GHH#EHJ/L":UX.K iimmAlAo$6q,$GHGKN#A|O4C{+cGmvo?VYbcoYp?p.qqF)/J+~&   #**:6N&))r:   c
                    [        X5      u  pUR                  S   UR                  S   :X  d   eSn
UR                  u  p[        R                  " USU
/5      R	                  X-  U5      n[        U5      S-  n[        R                  R                  [        R                  " U5      XS9n[        U5       HZ  nU U   S:  d  M  [        R                  " UUSS24   * U-   5      nXSS24   SS2USU U    4   UUU
-  US-   U
-  2USU U    4'   M\     UR                  U5      n[        R                  " UX45      R                  S5      nU" UU5      $ )zlThe model is reevaluated for each test sample with the important features set to resample background values.r   d   r   	n_samplesr/   r   Nr   r   r   tilereshaper    sklearnutilsresamplearanger"   r%   r$   r&   )r   r(   r)   r*   r+   r   r,   r-   r.   r/   nsamplesNMr3   r4   indsr7   r8   r   s                      r   remove_resampler\      sZ    w/OG ==v||A... H <<DA!X/77aHJ#A-==!!"))A,(!^D1X8a<zz9QT?"25G"GHHRY`aZaRb8JeAh''SJq8|q1u&88(:U1X:NNO  #**:6NZZ>CCAFN&.))r:   c
                    [         R                  " S5        [        X$5      u  p$UR                  S   UR                  S   :X  d   eUR	                  5       n
UR                  S5      n[        UR                  S   5      S-  n[        [        U5      5       H?  nX   S:  d  M  [        R                  " XmSS24   * U-   5      nXSX       XUSX    4'   MA     UR	                  5       n[        [        U5      5       H?  nX   S:  d  M  [        R                  " X}SS24   * U-   5      nXSX       XUSX    4'   MA     U" 5       nUR                  X5        UR                  U5      nU	" UU5      $ )ar  An approximation of holdout that only retraines the model once.

This is also called ROAR (RemOve And Retrain) in work by Google. It is much more computationally
efficient that the holdout method because it masks the most important features in every sample
and then retrains the model once, instead of retraining the model for every test sample like
the holdout metric.
r   r   r   r   Nr   r   r   r   r<   r&   r    r"   r#   r   r%   r'   r$   )nmask_train
nmask_testr(   r)   r*   r+   
attr_trainr   r,   r-   r2   X_train_meanr4   r7   r8   r3   r1   yp_test_maskeds                     r   batch_remove_retrainrd      sp    MM]^w/OG ==v||A... ,,.K<<?L#GMM!$45<3w< >Azz:d#3"36H"HIH9EO_Q\Q_F`9aK8$4kn556 ! J3v;=1zz9T?"25G"GHH7C_z}D]7^J(?Z]334   #$L[*!))*5N&.))r:   c
                 B   [         R                  " S5        XX4Xg4n
SnS[        ;   aO  [        S [	        [        S   U
5       5       5      (       a'  [
        R                  " [        S   U:H  5      (       a  Sn[        X5      u  pUR                  S   UR                  S   :X  d   eU" 5       n[
        R                  " UR                  5      n[
        R                  " UR                  5      n[
        R                  " UR                  5      n[        UR                  S   5      S-  n[        R                  S	S
5      n[        R                  SS
5      n[        [        [        U5      5      S5       H  nU(       a  UU   U U   :X  a
  UU   UU'   M   U U   UR                  S   :X  a  UR                  UUUS-    5      S   UU'   MU  XS
S
& X>S
S
& [
        R                  " UUS
S
24   * U-   5      nUS
S
2UU U   S
 4   R!                  5       US
S
2UU U   S
 4'   US
S
2UU U   S
 4   R!                  5       UUUU U   S
 4'   UR#                  X5        UR                  UUUS-    5      S   UU'   GM     U [        S	'   U[        S'   U[        S'   U
[        S'   U" XO5      $ )a  The model is retrained for each test sample with the non-important features set to a constant.

If you want to know how important a set of features is you can ask how the model would be
different if only those features had existed. To determine this we can mask the other features
across the entire training and test datasets, then retrain the model. If we apply compare the
output of this retrained model to the original model we can see the effect produced by only
knowning the important features. Since for individualized explanation methods each test sample
has a different set of most important features we need to retrain the model for every test sample
to get the change in model performance when a specified fraction of the most important features
are retained.
r   Fr   c              3   ,   #    U  H
  u  pXL v   M     g 7fr   r	   r
   s      r   r   keep_retrain.<locals>.<genexpr>   s     A"@$!qv"@r   r   Tr   r   nkeepNr   z Retraining for the 'keep' metricr   )r   r   _keep_cacher   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r&   r'   )rh   r(   r)   r*   r+   r   r,   r-   r.   r/   r   r0   r1   r2   r3   r   r4   
last_nkeepr6   r7   r8   s                        r   keep_retrainrk      s    MM]^ foFDKA#k&&94"@AAAbff[YdMeirMrFsFsKw/OG ==v||A... #$L ((7==)K&,,'JXXfll+N#GMM!$45<$/J%//*:DA%F$&HI:a=E!H4 3A 6N11X++ - 5 5fQQ6G H KN1 %N"qMzz9QT?"25G"GHH3:1huQxz>R;R3S3X3X3ZK8E!HJ//029!XeAhj=Q:Q2R2W2W2YJq(58:../ [2 , 4 4ZAE5J KA NN1 J$ !K$2K !(KK&))r:   c
                    [        X5      u  pUR                  S   UR                  S   :X  d   eUR                  5       n
[        R                  " UR                  5      n[        UR                  S   U	5      S-  nUR                  S5      n[        [        U5      5       HL  nX   UR                  S   :  d  M  [        R                  " X^SS24   * U-   5      nXX   S    XXU   S 4'   MN     UR                  U
5      nU" XK5      $ )z`The model is reevaluated for each test sample with the non-important features set to their mean.r   r   r   N)r   r   r<   r   r   r    r&   r"   r#   r%   r$   )rh   r(   r)   r*   r+   r   r,   r-   r.   r/   r3   r   r4   r=   r7   r8   s                   r   	keep_maskrm     s    w/OG ==v||A... JXXfll+N#GMM!$4lCdJQI3v;8fll1o%zz9T?"25G"GHH2;UXZ<P2QJ(8:../  
 #**:6N&))r:   c
                 T   [        X5      u  pUR                  S   UR                  S   :X  d   e[        R                  " UR                  5      n
U
[        R
                  " U
R                  S   5      S-  -  n
UR                  5       n[        R                  " UR                  5      n[        UR                  S   U	5      S-  nUR                  S5      n[        [        U5      5       H  nX   UR                  S   :  d  M  [        R                  " X_SS24   * U-   5      nUSX    nUX   S n[        R                  R                  U
USS24   SS2U4   5      nU
USS24   SS2U4   nUU   UU-  X?U4   UU   -
  -  -   nUXU4'   M     UR                  U5      nU" XL5      $ )an  The model is reevaluated for each test sample with the non-important features set to an imputed value.

Note that the imputation is done using a multivariate normality assumption on the dataset. This depends on
being able to estimate the full data covariance matrix (and inverse) accuractly. So X_train.shape[0] should
be significantly bigger than X_train.shape[1].
r   r   r   Nr@   )rh   r(   r)   r*   r+   r   r,   r-   r.   r/   rF   r3   r   r4   r=   r7   r8   rG   rH   rI   rJ   rK   s                         r   keep_imputero   )  s    w/OG ==v||A... 	wyyA
	d	""AJXXfll+N#GMM!$4lCdJQI3v;8fll1o%zz9T?"25G"GHH#Jeh/L"58:.K iimmAlAo$6q,$GHGKN#A|O4C{+cGmvo?VYbcoYp?p.qqF)/J+~&   #**:6N&))r:   c
                    [        X5      u  pUR                  S   UR                  S   :X  d   eSn
UR                  u  p[        R                  " USU
/5      R	                  X-  U5      n[        U5      S-  n[        R                  R                  [        R                  " U5      XS9n[        U5       HZ  nU U   U:  d  M  [        R                  " UUSS24   * U-   5      nXSS24   SS2UU U   S 4   UUU
-  US-   U
-  2UU U   S 4'   M\     UR                  U5      n[        R                  " UX45      R                  S5      nU" UU5      $ )zpThe model is reevaluated for each test sample with the non-important features set to resample background values.r   rN   r   rO   NrQ   )rh   r(   r)   r*   r+   r   r,   r-   r.   r/   rX   rY   rZ   r3   r4   r[   r7   r8   r   s                      r   keep_resamplerq   P  sZ    w/OG ==v||A... H <<DA!X/77aHJ#A-==!!"))A,(!^D1X8a<zz9QT?"25G"GHHRY`aZaRb8E!HJ''SJq8|q1u&88(58::NNO  #**:6NZZ>CCAFN&.))r:   c
                    [         R                  " S5        [        X$5      u  p$UR                  S   UR                  S   :X  d   eUR	                  5       n
UR                  S5      n[        UR                  S   5      S-  n[        [        U5      5       HL  nX   UR                  S   :  d  M  [        R                  " XmSS24   * U-   5      nXX   S    XXU   S 4'   MN     UR	                  5       n[        [        U5      5       HL  nX   UR                  S   :  d  M  [        R                  " X}SS24   * U-   5      nXX   S    XXU   S 4'   MN     U" 5       nUR                  X5        UR                  U5      nU	" UU5      $ )ac  An approximation of keep that only retraines the model once.

This is also called KAR (Keep And Retrain) in work by Google. It is much more computationally
efficient that the keep method because it masks the unimportant features in every sample
and then retrains the model once, instead of retraining the model for every test sample like
the keep metric.
r   r   r   r   Nr^   )nkeep_train
nkeep_testr(   r)   r*   r+   ra   r   r,   r-   r2   rb   r4   r7   r8   r3   r1   rc   s                     r   batch_keep_retrainru   n  s    MM]^w/OG ==v||A... ,,.K<<?L#GMM!$45<3w< >GMM!,,zz:d#3"36H"HIH9E{~O_F`9aK8N$4556 ! J3v;=6<<?*zz9T?"25G"GHH7CZ]_D]7^J(a=?334   #$L[*!))*5N&.))r:   c                     [        X5      u  pU R                  S   UR                  S   :X  d   eUR                  U5      nU" U[        U5      R	                  S5      5      $ )zRThe how well do the features plus a constant base rate sum up to the model output.r   )r   r   r$   
strip_listsum)	r(   r)   r*   r+   r   r,   r-   r.   yp_tests	            r   local_accuracyrz     s^    w/OG ==v||A... ##F+G':i044Q788r:   c                      U  Vs/ s H0  n[        U[        R                  5      (       a  UR                  OUPM2     sn$ s  snf r   )
isinstancepd	DataFramevalues)r   r   s     r   r   r     s3    DHIDq
1bll33AHH:DIIIs   7?c                     [         R                  R                  5       n[         R                  R                  U5        [         R                  R                  U 5      n[         R                  R                  U5        U$ )z*Generate a random array with a fixed seed.)r   randomseedrand)sizer   old_seedouts       r   r    r      sI    yy~~HIINN4
))..
CIINN8Jr:   c                     [         R                  R                  5       n[         R                  R                  U5        [         R                  R                  U 5        [         R                  R                  U5        g)z,Shuffle an array in-place with a fixed seed.N)r   r   r   shuffle)arrr   r   s      r   const_shuffler     sD    yy~~HIINN4IIcIINN8r:   c                 :    [        U [        5      (       a  U S   $ U $ )zqThis assumes that if you have a list of outputs you just want the second one (the second class is the '1' class).r   )r|   list)attrss    r   rw   rw     s    %Qxr:   )i]  )r   numpyr   pandasr}   sklearn.utilsrT   	tqdm.autor   r   r9   r>   rL   r\   rd   ri   rk   rm   ro   rq   ru   rz   r   r    r   rw   r	   r:   r   <module>r      sr        >*B*.$*N*<$*N >*B*,$*N*<$*N
9Jr:   