U
    Ùfü
  ã                   @   s‚   d dl Zd dlZd dlZzd dlmZ d dlZW n ek
rN   e 	d¡ Y nX ddl
mZ ddl
mZ ddlmZ ddd„ZdS )é    Nzmatplotlib could not be loaded!é   )Úlabels)Úcolorsé   )Úconvert_nameÚpcaç      ð?Tc                 C   sl  |dkr dd„ t |jd ƒD ƒ}t| ||ƒ} | dkrD| d¡}d}n|dd…| f }||  }t|ƒtkrˆ|dkrˆtj d¡}| 	|¡}	n(t
|d	ƒr¦|jd dkr¦|}	n
td
|ƒ tj|	dd…df |	dd…df |tj|dd t d¡ t ¡ }
|
jd| dd |
j d¡ t ¡  dd¡ |
j ¡  t ¡ j ¡ ¡}|
j |jd d ¡ |
 d¡ |rht ¡  dS )a¡   Use the SHAP values as an embedding which we project to 2D for visualization.

    Parameters
    ----------
    ind : int or string
        If this is an int it is the index of the feature to use to color the embedding.
        If this is a string it is either the name of the feature, 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), or "sum()" to mean the sum of all the SHAP values,
        which is the model's output (minus it's expected value).

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

    feature_names : None or list
        The names of the features in the shap_values array.

    method : "pca" or numpy.array
        How to reduce the dimensions of the shap_values to 2D. If "pca" then the 2D
        PCA projection of shap_values is used. If a numpy array then is should be
        (# samples x 2) and represent the embedding of that values. 

    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.
    Nc                 S   s   g | ]}t d  t|ƒ ‘qS )ZFEATURE)r   Ústr)Ú.0Úi© r   ú;/tmp/pip-target-lpfmz8o1/lib/python/shap/plots/embedding.pyÚ
<listcomp>+   s     z"embedding_plot.<locals>.<listcomp>r   zsum()zsum(SHAP values)r   r   ÚshapezUnsupported embedding method:r   )ÚcZcmapÚalphaZ	linewidthÚoffzSHAP value for
é   )ÚsizeFg      @é   gffffffæ?é
   ) Úranger   r   ÚsumÚtyper	   ÚsklearnÚdecompositionZPCAZfit_transformÚhasattrÚprintÚplZscatterr   Zred_blueZaxisZcolorbarZ	set_labelÚoutlineZset_visibleZgcfZset_size_inchesZaxZget_window_extentZtransformedZdpi_scale_transÚinvertedZ
set_aspectÚheightZ	set_alphaÚshow)ÚindZshap_valuesZfeature_namesÚmethodr   r"   ZcvalsÚfnamer   Zembedding_valuesÚcbZbboxr   r   r   Úembedding_plot   s@    

    þ

r'   )Nr   r   T)ÚnumpyÚnpr   ÚwarningsZmatplotlib.pyplotZpyplotr   Z
matplotlibÚImportErrorÚwarnÚ r   r   Úcommonr   r'   r   r   r   r   Ú<module>   s   
