U
    Ùf/  ã                   @   sŠ   d dl mZ d dlZd dlZzd dlmZ d dlZW n e	k
rR   e 
d¡ Y nX ddlmZ ddlmZ ddlmZmZ ddd„ZdS )é    )ÚdivisionNzmatplotlib could not be loaded!é   )Úlabels)Úcolorsé   )Úconvert_nameÚapproximate_interactionsÚautoú#1E88E5ú#333333é   Tc           2         s(	  |dkrt j}|s8|| krdnd}tj|d}| ¡ }n| ¡ }tt|ƒƒ d¡rf|dkr`|j	}|j
}tt|ƒƒ d¡rŽ|dkr†|j	}|j
}n|dkrš|}|dkrºdd„ t|jd ƒD ƒ}t|jƒdkrÚt |t|ƒd¡}t|jƒdkrút |t|ƒd¡}t| ||ƒ} t|jƒd	krît| ƒd
krît| d ||ƒ}t| d ||ƒ}||krf|dd…|dd…f }n|dd…|dd…f d
 }t|||||||d||d
 ||kr¾| td ||  ¡ n| td || || f ¡ |rêt ¡  dS |jd |jd kstdƒ‚|jd |jd ks*tdƒ‚t |jd ¡}tj |¡ ||| f  tj¡}||| f }||| f }t|d ƒtkr¸i ‰tt|ƒƒD ]}|| ˆ|| < q”tˆ ¡ ƒ}t|ƒtkrÌ|g}||  }|dkrît| ||ƒd }t|||ƒ}d}d}|dk	r0|dd…|f }|dd…|f } t | tj¡d¡}!t | tj¡d¡}"t| d ƒtkr i ‰ tt|ƒƒD ]}|| ˆ | | < qvtˆ  ¡ ƒ}#d}n.|!d dkrÎ|"d dkrÎ|"|! dk rÎd}|r0|!|"kr0t  | tj¡¡}!t !| tj¡¡}"t "|!|"t#|"|! d
 ƒ¡}$t$j  %|$|j&d ¡}|
dkrÔ|
dkrHd}
| '¡ }%t(|%d tƒr||% tj¡}%|%t )|%¡  }%t *|%¡}%t|%ƒd
krÔt +t ,t -|%¡¡¡}&|
|& }'|tjj.t|ƒd|' |'d
  7 }t )|¡}(t /|(¡})|dk	r€|||f  tj¡}*|* '¡ }+|!|" d |+t )|*¡< |"|*|+|"k< |!|*|+|!k < |j0||) ||) |	d|*|) |||!|"|t|ƒdkd},|, 1|*|) ¡ n |j0|||	d||t|ƒdkd},|| kr |dk	r t| d ƒtkr ‡ fdd„|#D ƒ}-t|-ƒd
kr|-d  d8  < |-d  d7  < tj2|,|-d}.|. 3|#¡ n
t 2|,¡}.|.j4|| dd |.j5j6d d! |r^|.j5j6dd" |. 7d¡ |.j8 9d¡ |.j5 :¡  ;|j< =¡ ¡}/|.j5 >|/j?d# d$ ¡ |dk	s´|dk	rt|ƒtkræ| @d%¡ræt |t|d d&… ƒ¡}t|ƒtkr| @d%¡rt |t|d d&… ƒ¡}|dks2|t  |¡krNt  |¡|t  |¡ d$  }|dksh|t !|¡kr„t !|¡t !|¡| d$  }| A||¡ | B¡ }0|dk	rè|j0|0d t C|( D¡ ¡ ||( dd
|+|( |||!|"d'	},|, 1|*|( ¡ n,|j0|0d t C|( D¡ ¡ ||( dd
||d( | A|0¡ |jE||dd) |jtd* | |dd) |dk	r`|jF||dd) |jG Hd+¡ |jI Hd,¡ |jJd-  9d¡ |jJd.  9d¡ |j6||d d/ |jJ 
¡ D ]}1|1 K|¡ q²t|d ƒtkrö|jL‡fd0d„|D ƒ|d1d d2 |	r$tM N¡  tM Od3tP¡ t ¡  W 5 Q R X dS )4a
   Create a SHAP dependence plot, colored by an interaction feature.

    Plots the value of the feature on the x-axis and the SHAP value of the same feature
    on the y-axis. This shows how the model depends on the given feature, and is like a
    richer extenstion of the classical parital dependence plots. Vertical dispersion of the
    data points represents interaction effects. Grey ticks along the y-axis are data
    points where the feature's value was NaN.


    Parameters
    ----------
    ind : int or string
        If this is an int it is the index of the feature to plot. If this is a string it is
        either the name of the feature to plot, or it can have the form "rank(int)" to specify
        the feature with that rank (ordered by mean absolute SHAP value over all the samples).

    shap_values : numpy.array
        Matrix of SHAP values (# samples x # features).

    features : numpy.array or pandas.DataFrame
        Matrix of feature values (# samples x # features).

    feature_names : list
        Names of the features (length # features).

    display_features : numpy.array or pandas.DataFrame
        Matrix of feature values for visual display (such as strings instead of coded values).

    interaction_index : "auto", None, int, or string
        The index of the feature used to color the plot. The name of a feature can also be passed
        as a string. If "auto" then shap.common.approximate_interactions is used to pick what
        seems to be the strongest interaction (note that to find to true stongest interaction you
        need to compute the SHAP interaction values).
        
    x_jitter : float (0 - 1)
        Adds random jitter to feature values. May increase plot readability when feature
        is discrete.

    alpha : float
        The transparency of the data points (between 0 and 1). This can be useful to the
        show density of the data points when using a large dataset.

    xmin : float or string
        Represents the lower bound of the plot's x-axis. It can be a string of the format
        "percentile(float)" to denote that percentile of the feature's value used on the x-axis.

    xmax : float or string
        Represents the upper bound of the plot's x-axis. It can be a string of the format
        "percentile(float)" to denote that percentile of the feature's value used on the x-axis.

    ax : matplotlib Axes object
         Optionally specify an existing matplotlib Axes object, into which the plot will be placed.
         In this case we do not create a Figure, otherwise we do.

    N)g      @é   )é   r   )Úfigsizez'pandas.core.frame.DataFrame'>c                 S   s   g | ]}t d  t|ƒ ‘qS )ZFEATURE)r   Ústr)Ú.0Úi© r   ú</tmp/pip-target-lpfmz8o1/lib/python/shap/plots/dependence.pyÚ
<listcomp>c   s     z#dependence_plot.<locals>.<listcomp>r   é   r   r   F)Úfeature_namesÚinteraction_indexÚdisplay_featuresÚaxÚshowÚxminÚxmaxZMAIN_EFFECTZINTERACTION_EFFECTzF'shap_values' and 'features' values must have the same number of rows!zA'shap_values' must have the same number of columns as 'features'!r	   r   é_   Té
   )Úsizeg       @iô  )	ÚsÚ	linewidthÚcÚcmapÚalphaÚvminÚvmaxZnormÚ
rasterized)r!   r"   Úcolorr%   r(   c                    s   g | ]}ˆ | ‘qS r   r   ©r   Ún)Ú	cname_mapr   r   r   Ý   s     g      Ð?)Zticksé   é   )Ú	labelsize)Úlengthgffffffæ?é   Z
percentileéÿÿÿÿ)Úmarkerr"   r#   r$   r%   r&   r'   )r3   r"   r)   r%   )r)   ÚfontsizeZ	VALUE_FORÚbottomÚleftÚrightÚtop)r)   Z
labelcolorr/   c                    s   g | ]}ˆ | ‘qS r   r   r*   )Úname_mapr   r   r     s     Úvertical)Zrotationr4   Úignore)Qr   Zred_blueÚplZfigureZgcaZ
get_figurer   ÚtypeÚendswithÚcolumnsÚvaluesÚrangeÚshapeÚlenÚnpZreshaper   Údependence_plotZ
set_ylabelr   r   ÚAssertionErrorZarangeÚrandomÚshuffleZastypeZfloat64ÚlistÚkeysr   ZnanpercentileÚfloatZnanminZnanmaxZlinspaceÚintÚ
matplotlibZBoundaryNormÚNÚcopyÚ
isinstanceÚisnanÚuniqueÚminÚdiffÚsortZranfÚinvertZscatterZ	set_arrayZcolorbarZset_ticklabelsZ	set_labelr   Ztick_paramsZ	set_alphaÚoutlineZset_visibleZget_window_extentZtransformedZdpi_scale_transÚinvertedZ
set_aspectÚheightÚ
startswithZset_xlimZget_xlimZonesÚsumZ
set_xlabelÚ	set_titleZxaxisZset_ticks_positionZyaxisZspinesZset_edgecolorZ
set_xticksÚwarningsÚcatch_warningsÚsimplefilterÚRuntimeWarning)2ÚindZshap_valuesÚfeaturesr   r   r   r)   Z
axis_colorr$   Zdot_sizeZx_jitterr%   Útitler   r   r   r   r   ZfigZind1Zind2Zproj_shap_valuesZoindsZxvZxdr!   r   ZxnamesÚnameZcategorical_interactionZ
color_normZcvÚcdZclowZchighZcnamesZboundsZxvalsZsmallest_diffZjitter_amountZxv_nanZ	xv_notnanZcvalsZ	cvals_impÚpZtick_positionsÚcbZbboxZxlimZspiner   )r,   r9   r   rE      s†   <

       ý
ÿÿ

*

 
"


        
ý 
ÿ


      ý    þ

 
rE   )NNr	   r
   r   Nr   r   r   NNNNT)Ú
__future__r   ÚnumpyrD   r]   Zmatplotlib.pyplotZpyplotr<   rM   ÚImportErrorÚwarnÚ r   r   Úcommonr   r   rE   r   r   r   r   Ú<module>   s2   
                      ý