# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Using the Unsupervised Tuning Methods
#
# In this notebook you can find a number of examples of how to use the unsupervied tuning methods. Note the examples rely on the iris data set which can be loaded in directly from the Eagle_ML package. Although the iris data set has species associated with each outcome these will be ignored during the clustering examples.
#
# Currently impletmented clustering models for find_optimal_clusters() include:
# - "kmeans"
# - "agglomerativeclustering"
# - "dbscan"
#
# However eval_clusters() can take other sklearn cluster model objects

from eagles.Unsupervised import unsupervised_tuner as ut
from eagles.data_sets import data_loader as dl

# Load in the iris data set and set the columns that will be used for clustering examples

iris = dl.load_iris()
ft_cols = [col for col in iris.columns if col != 'species']
del iris['species']
print(ft_cols)
print(iris.shape)

# ## Using the find_optimal_clusters() functions
#
# This function allows the user to pass in the datat to be clustered and finds an "optimal" number of clusters. When using kmeans or agglomerative clusters the metrics that can be used to find the optimal number of clusters include:
# - max_sil: Determines the optimal number of cluster by finding the number of clusters that results in the max silhouette score 
# - knee_wss: Determines the optimal number of clusters by using the KneeLocator method provided by the kneed package on the WSS as compared to the number of clusters.
#
# Documentation for the KneeLocator method can be found via: https://github.com/arvkevi/kneed.
#
# When DBSCAN is used the optimal number of clusters is determined by the method itself. See documentation provided at https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html.
#

# ## Basic usage of the find_optimal_clusters() function with the kmeans algorithm.

data = ut.find_optimal_clusters(
    data=iris,
    ft_cols=ft_cols,
    cluster_method="kmeans",
    metric="knee_wss",
    min_num_clusters=2,
    max_num_clusters=10,
    params={},
    scale=None,
    plot_dims=[],
    summary_stats=['mean','std','max'],
    run_stat_comps=True,
    plot_scale='standard',
    random_seed=4,
    log=None,
    log_name=None,
    log_path=None,
    log_note=None,
)

# ## Passing in parameters to the find_optimal_clusters() functions along with logging. 
#
# The log note is optional and will be ignored if nothing is passed. Also when the log path is None the function will create a data directory within the eagles directory itself. 

# +
log_note = """
    This is an example of clustering useing the agglomerative clustering algorithm.
"""

model_params = {
    'linkage':'average'
    ,'affinity':'euclidean'
}

data = ut.find_optimal_clusters(
    data=iris,
    ft_cols=ft_cols,
    cluster_method="agglomerativeclustering",
    metric="max_sil",
    min_num_clusters=2,
    max_num_clusters=10,
    params=model_params,
    scale=None,
    plot_dims=[],
    summary_stats=['median','std','max'],
    run_stat_comps=True,
    plot_scale=None,
    random_seed=4,
    log='log',
    log_name='test.txt',
    log_path=None,
    log_note=log_note,
)
# -

# ## Using the cluster_eval() function 

data = ut.eval_clusters(
    data=iris,
    n_clusters=3,
    method="kmeans",
    params={},
    scale=None,
    ft_cols=ft_cols,
    plot_dims=[],
    summary_stats=["mean", "max"],
    run_stat_comps=True,
    plot_scale="minmax",
    log="log",
    log_name=None,
    log_path=None,
    log_note=None,
)



StarcoderdataJupyterScriptsDedupFiltered