U
    fH                     @   s   d dl Zd dlmZ d dlZd dlZd dlZi add Z	dd Z
dd Zd	d
 Zdd Zi add Zdd Zdd Zdd Zdd Zdd Zdd Zd"ddZd#ddZd d! ZdS )$    N)tqdmc
                 C   s4  t d ||||||f}
d}dtkrXtdd ttd |
D rXttd |krXd}t||\}}|jd |jd ks~t| }t	|j}t	|j}t	|j}t
|jd d	 }td
d}tdd}ttt|dD ]"}|r|| | | kr|| ||< q| | dkr>||||d  d ||< q||dd< ||dd< t||ddf  | }|dd|d| |  f  |dd|d| |  f< |dd|d| |  f  |||d| |  f< ||| ||||d  d ||< q| td
< |td< |td< |
td< |||S )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                 s   s   | ]\}}||kV  qd S N .0abr   r   >/tmp/pip-target-lpfmz8o1/lib/python/shap/benchmark/measures.py	<genexpr>   s     z!remove_retrain.<locals>.<genexpr>	attr_testT   ư>nmaskNyp_masked_testz"Retraining for the 'remove' metricr   )warningswarn_remove_cacheallzipnpto_arrayshapeAssertionError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_noiseZ
last_nmasklast_yp_masked_testiorderingr   r   r   remove_retrain	   s@    
. 84 r4   c
                 C   s   t ||\}}|jd |jd ks&t| }
t|jd |	d }|d}tt|D ]T}| | dkrXt	||ddf  | }||d| |   |
||d| |  f< qX|
|
}|||S )zQ Each test sample is masked by setting the important features to a constant.
    r   r   r   N)r   r   r   copyr   r"   r   r   r   r!   r    )r   r$   r%   r&   r'   r   r(   r)   r*   r+   r/   r0   	mean_valsr2   r3   r   r   r   r   remove_maskH   s    
*
r7   c
                 C   sZ  t ||\}}|jd |jd ks&tt|j}
|
t|
jd d 7 }
| }t|j}t	|jd |	d }|
d}tt|D ]}| | dkrt||ddf  | }|| | d }|d| |  }tj|
|ddf dd|f }|
|ddf dd|f }|| || |||f ||    }||||f< q||}|||S )az   The model is revaluated 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   r   ZcovTeyer5   r   r   r"   r   r   r!   Zlinalginvr    )r   r$   r%   r&   r'   r   r(   r)   r*   r+   Cr/   r   r0   r6   r2   r3   observe_indsimpute_indsCoo_invCioimputer   r   r   remove_impute^   s&    
$$
rB   c
                 C   s   t ||\}}|jd |jd ks&td}
|j\}}t|d|
g|
| |}t|d }tjj	t
||
|	d}t|D ]x}| | dkr|t||ddf  | }||ddf dd|d| |  f |||
 |d |
 |d| |  f< q|||}t|||
fd}|||S )zq The model is revaluated for each test sample with the important features set to resample background values.
    r   d   r   Z	n_samplesr+   r   Nr   r   r   r   ZtileZreshaper   sklearnutilsZresampleZaranger   r!   r    r"   )r   r$   r%   r&   r'   r   r(   r)   r*   r+   nsamplesNMr/   r0   indsr2   r3   r   r   r   r   remove_resample   s    
N
rL   c
                 C   sF  t d t||\}}|jd |jd ks0t| }
|d}t|jd d }tt	|D ]T}| | dkr`t
||ddf  | }||d| |   |
||d| |  f< q`| }tt	|D ]T}|| dkrt
||ddf  | }||d||   |||d||  f< q| }||
| ||}|	||S )a   An approximation of holdout that only retraines the model once.

    This is alse 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   r5   r"   r   r   r   r   r!   r#   r    )Znmask_trainZ
nmask_testr$   r%   r&   r'   
attr_trainr   r(   r)   r.   X_train_meanr0   r2   r3   r/   r-   yp_test_maskedr   r   r   batch_remove_retrain   s&    	

**
rQ   c
                 C   s:  t d ||||||f}
d}dtkrXtdd ttd |
D rXttd |krXd}t||\}}|jd |jd ks~t| }t	|j}t	|j}t	|j}t
|jd d	 }td
d}tdd}ttt|dD ](}|r|| | | kr|| ||< q| | |jd krD||||d  d ||< q||dd< ||dd< t||ddf  | }|dd|| | d f  |dd|| | d f< |dd|| | d f  |||| | d f< ||| ||||d  d ||< q| td
< |td< |td< |
td< |||S )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                 s   s   | ]\}}||kV  qd S r   r   r   r   r   r   r      s     zkeep_retrain.<locals>.<genexpr>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"   r#   )rR   r$   r%   r&   r'   r   r(   r)   r*   r+   r   r,   r-   r.   r/   r   r0   Z
last_nkeepr1   r2   r3   r   r   r   keep_retrain   s@    
. 84 rT   c
                 C   s   t ||\}}|jd |jd ks&t| }
t|j}t|jd |	d }|d}tt	|D ]Z}| | |jd k rdt
||ddf  | }||| | d  |
||| | d f< qd||
}|||S )ze The model is revaluated for each test sample with the non-important features set to their mean.
    r   r   r   N)r   r   r   r5   r   r   r   r"   r   r   r!   r    )rR   r$   r%   r&   r'   r   r(   r)   r*   r+   r/   r   r0   r6   r2   r3   r   r   r   	keep_mask  s    
*
rU   c
                 C   s`  t ||\}}|jd |jd ks&tt|j}
|
t|
jd d 7 }
| }t|j}t	|jd |	d }|
d}tt|D ]}| | |jd k rt||ddf  | }|d| |  }|| | d }tj|
|ddf dd|f }|
|ddf dd|f }|| || |||f ||    }||||f< q||}|||S )a~   The model is revaluated 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   Nr8   )rR   r$   r%   r&   r'   r   r(   r)   r*   r+   r<   r/   r   r0   r6   r2   r3   r=   r>   r?   r@   rA   r   r   r   keep_impute  s&    
$$
rV   c
                 C   s   t ||\}}|jd |jd ks&td}
|j\}}t|d|
g|
| |}t|d }tjj	t
||
|	d}t|D ]x}| | |k r|t||ddf  | }||ddf dd|| | d f |||
 |d |
 || | d f< q|||}t|||
fd}|||S )zu The model is revaluated for each test sample with the non-important features set to resample background values.
    r   rC   r   rD   NrE   )rR   r$   r%   r&   r'   r   r(   r)   r*   r+   rH   rI   rJ   r/   r0   rK   r2   r3   r   r   r   r   keep_resample@  s    
N
rW   c
                 C   sR  t d t||\}}|jd |jd ks0t| }
|d}t|jd d }tt	|D ]Z}| | |jd k r`t
||ddf  | }||| | d  |
||| | d f< q`| }tt	|D ]Z}|| |jd k rt
||ddf  | }|||| d  ||||| d f< q| }||
| ||}|	||S )ax   An approximation of keep that only retraines the model once.

    This is alse 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   NrM   )Znkeep_trainZ
nkeep_testr$   r%   r&   r'   rN   r   r(   r)   r.   rO   r0   r2   r3   r/   r-   rP   r   r   r   batch_keep_retrain[  s&    	

**
rX   c           	      C   sD   t | |\} }| jd |jd ks&t||}||t|dS )zX The how well do the features plus a constant base rate sum up to the model output.
    r   )r   r   r   r    
strip_listsum)	r$   r%   r&   r'   r   r(   r)   r*   Zyp_testr   r   r   local_accuracy  s    
r[   c                  G   s   dd | D S )Nc                 S   s(   g | ] }t t|d r |jn|qS )z'pandas.core.frame.DataFrame'>)strtypeendswithvalues)r   r	   r   r   r   
<listcomp>  s     zto_array.<locals>.<listcomp>r   )r   r   r   r   r     s    r   ]  c                 C   s2   t j }t j| t j| }t j| |S )z0 Generate a random array with a fixed seed.
    )r   randomseedZrand)sizerc   old_seedoutr   r   r   r     s
    
r   c                 C   s2   t j }t j| t j|  t j| dS )z2 Shuffle an array in-place with a fixed seed.
    N)r   rb   rc   shuffle)Zarrrc   re   r   r   r   const_shuffle  s    
rh   c                 C   s   t | tr| d S | S dS )zw This assumes that if you have a list of outputs you just want the second one (the second class is the '1' class).
    r   N)
isinstancelist)attrsr   r   r   rY     s    
rY   )ra   )ra   )numpyr   Ztqdm.autonotebookr   gcr   Zsklearn.utilsrF   r   r4   r7   rB   rL   rQ   rS   rT   rU   rV   rW   rX   r[   r   r   rh   rY   r   r   r   r   <module>   s*   ?%%@%%
	
