U
    f	                     @   sR   d dl mZ ddlZzddlZddlZW n ek
r<   Y nX G dd deZdS )   )	Explainer    Nc                   @   s$   e Zd ZdZd	ddZd
ddZdS )LimeTabularExplainera   Simply wrap of lime.lime_tabular.LimeTabularExplainer into the common shap interface.

    Parameters
    ----------
    model : function or iml.Model
        User supplied function that takes a matrix of samples (# samples x # features) and
        computes a the output of the model for those samples. The output can be a vector
        (# samples) or a matrix (# samples x # model outputs).

    data : numpy.array
        The background dataset. 

    mode : "classification" or "regression"
        Control the mode of LIME tabular.
    classificationc                    s   | _ |dkst| _tt|dr0|j}| _tj	j
||d _  |dd }t|jdkrd _d _|dkr fdd	}| _ n   |dd jd  _d
 _d S )N)r   
regressionpandas.core.frame.DataFrame'>)moder      Tr   c                    s(     | dd}d| }t||fS )Nr	   )modelZreshapenpZhstack)Xpredsp0self A/tmp/pip-target-lpfmz8o1/lib/python/shap/explainers/other/lime.pypred)   s    z+LimeTabularExplainer.__init__.<locals>.predF)r   AssertionErrorr   strtypeendswithvaluesdatalimeZlime_tabularr   	explainerlenshapeout_dimflat_out)r   r   r   r   outr   r   r   r   __init__   s     zLimeTabularExplainer.__init__  Nc           
         s   |d kr j d n|}tt dr. j  fddt| jD }t j d D ]X}| jj | | j	t| j|d}t| jD ](}|j
| D ]\}}	|	|| ||f< qqqT| jdkrtt|D ]}||  ||< q| jr|d S |S )Nr	   r   c                    s   g | ]}t  jqS r   )r   Zzerosr   ).0jr   r   r   
<listcomp>8   s     z5LimeTabularExplainer.attributions.<locals>.<listcomp>r   )labelsnum_featuresr   )r   r   r   r   r   ranger   r   Zexplain_instancer   Z	local_expr   r   r    )
r   r   Znsamplesr)   r!   iexpr%   kvr   r&   r   attributions2   s     
z!LimeTabularExplainer.attributions)r   )r#   N)__name__
__module____qualname____doc__r"   r/   r   r   r   r   r   	   s   
r   )r   r   numpyr   r   Zlime.lime_tabularImportErrorr   r   r   r   r   <module>   s   