o
    xi5                     @   s|   d Z ddlmZ ddlZddlZddlZddlmZm	Z	 ede
d ddd	Z				
	dddZ						dddZdS )z;Define plots for clustering models built with scikit-learn.    )simplefilterN)	calculateutilsignore)actioncategory	Clustererc                 C   sf   t d| d t| tjjr$t| | t d t| |||dd nt| ||dd t d d	S )
aX  Generates all sklearn clusterer plots supported by W&B.

    The following plots are generated:
        elbow curve, silhouette plot.

    Should only be called with a fitted clusterer (otherwise an error is thrown).

    Args:
        model: (clusterer) Takes in a fitted clusterer.
        X_train: (arr) Training set features.
        cluster_labels: (list) Names for cluster labels. Makes plots easier to read
                            by replacing cluster indexes with corresponding names.
        labels: (list) Named labels for target variable (y). Makes plots easier to
                        read by replacing target values with corresponding index.
                        For example if `labels=['dog', 'cat', 'owl']` all 0s are
                        replaced by dog, 1s by cat.
        model_name: (str) Model name. Defaults to 'Clusterer'

    Returns:
        None: To see plots, go to your W&B run page then expand the 'media' tab
              under 'auto visualizations'.

    Example:
    ```python
    wandb.sklearn.plot_clusterer(kmeans, X, cluster_labels, labels, "KMeans")
    ```
    z

Plotting .zLogged elbow curve.T)labelskmeansF)r   zLogged silhouette plot.N)wandbtermlog
isinstancesklearnclusterKMeanselbow_curve
silhouette)modelX_traincluster_labelsr
   
model_name r   \/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/sklearn/plot/clusterer.py	clusterer   s   

r      Tc           	      C   sp   t | dstd dS tj| d}tj}t| }|r2|r4|r6t| ||||}t	d|i dS dS dS dS )aw  Measures and plots variance explained as a function of the number of clusters.

    Useful in picking the optimal number of clusters.

    Should only be called with a fitted clusterer (otherwise an error is thrown).

    Please note this function fits the model on the training set when called.

    Args:
        model: (clusterer) Takes in a fitted clusterer.
        X: (arr) Training set features.

    Returns:
        None: To see plots, go to your W&B run page then expand the 'media' tab
              under 'auto visualizations'.

    Example:
    ```python
    wandb.sklearn.plot_elbow_curve(model, X_train)
    ```
    
n_clusterszAn_clusters attribute not in classifier. Cannot plot elbow method.Nr   r   )
hasattrr   r   r   test_missing
test_typestest_fittedr   r   log)	r   Xcluster_rangesn_jobsshow_cluster_timenot_missingcorrect_types	is_fittedelbow_curve_chartr   r   r   r   8   s   


r   	euclideanc           
      C   sr   t j| d}t j| d}t | }|r3|r5|r7t|tjr |j}t	| |||||}	t
d|	i dS dS dS dS )a#  Measures & plots silhouette coefficients.

    Silhouette coefficients near +1 indicate that the sample is far away from
    the neighboring clusters. A value near 0 indicates that the sample is on or
    very close to the decision boundary between two neighboring clusters and
    negative values indicate that the samples might have been assigned to the wrong cluster.

    Should only be called with a fitted clusterer (otherwise an error is thrown).

    Please note this function fits the model on the training set when called.

    Args:
        model: (clusterer) Takes in a fitted clusterer.
        X: (arr) Training set features.
        cluster_labels: (list) Names for cluster labels. Makes plots easier to read
                               by replacing cluster indexes with corresponding names.

    Returns:
        None: To see plots, go to your W&B run page then expand the 'media' tab
              under 'auto visualizations'.

    Example:
    ```python
    wandb.sklearn.plot_silhouette(model, X_train, ["spam", "not spam"])
    ```
    r   silhouette_plotN)r   r   r    r!   r   pd	DataFramevaluesr   r   r   r"   )
r   r#   r   r
   metricr   r'   r(   r)   silhouette_chartr   r   r   r   f   s   "
r   )Nr   )NNNr   T)NNNNr+   T)__doc__warningsr   pandasr-   r   r   wandb.integration.sklearnr   r   FutureWarningr   r   r   r   r   r   r   <module>   s*    
*
/