helpers.py
def km_elbow_diagram(n_clusters_range, metric_values,
                     metrics=['Within Clusters Sample Variance'],
                     xlim=None, ylim=None, title=None, stdev=None,
                     leg_loc1='upper right', leg_loc2='lower right',
                     figsize=None):
    """
    Plot a metric or metrics as a function of number of clusters.

    Parameters
    ----------
    n_clusters_range : list
        Number of clusters corresponding to metric values.

    metric_values : list of lists
        Lists of metric values for each metric considered.

    metrics : list of strings, optional
        Labels for metrics, default 'Within Clusters SST'.

    xlim : tuple of floats, optional
        Bounds for x-axis, default None.

    ylim : tuple of floats, optional
        Bounds for y-axis, default None.

    title : string, optional
        Title for the chart.
    """
    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666', '#000000']
    fig, ax1 = plt.subplots()

    if figsize is not None:
        fig.set_size_inches(figsize)

    if title is not None:
        fig.suptitle(title)
    if xlim is not None:
        ax1.set_xlim(xlim)
    if ylim is not None:
        ax1.set_ylim(ylim)

    ax1.set_xlabel('Number of clusters')

    y_max = 0
    x_max = 0

    # Plots results for each metric value
    for i, y_vals in enumerate(metric_values):
        label = metrics[i]
        if i == 0:
            if y_vals[0] == 'na':
                n_clusters_range = n_clusters_range[1:]
                y_vals = y_vals[1:]
            ax1.plot(n_clusters_range, y_vals, marker='o', markersize=4,
                     color = colors[i], label=label)
            ax1.set_ylabel(label)
            ax1.set_xticks(n_clusters_range)
            ax1.legend(loc=leg_loc1)
            if stdev is not None:
                ax1.fill_between(n_clusters_range, y_vals+stdev, y_vals-stdev,
                                 alpha=0.2, color = colors[i+1])
        elif i == 1:
            ax2 = ax1.twinx()
            if y_vals[0] == 'na':
                n_clusters_range = n_clusters_range[1:]
                y_vals = y_vals[1:]
            ax2.plot(n_clusters_range, y_vals, marker='o', markersize=4,
                     color = colors[i], label=label)
            ax2.set_ylabel(label)
            ax2.legend(loc=leg_loc2)

    ax1.grid(b=True)
    plt.show()

    Plot a metric or metrics as a function of number of clusters.

    Parameters
    ----------
    n_clusters_range : list
        Number of clusters corresponding to metric values.

    metric_values : list of lists
        Lists of metric values for each metric considered.

    metrics : list of strings, optional
        Labels for metrics, default 'Within Clusters SST'.

    xlim : tuple of floats, optional
        Bounds for x-axis, default None.

    ylim : tuple of floats, optional
        Bounds for y-axis, default None.

    title : string, optional
        Title for the chart.
    
Plot a metric or metrics as a function of number of clusters.
Plot a metric or metrics as a function of number of clusters .
def km_elbow_diagram(n_clusters_range, metric_values,
                     metrics=['Within Clusters Sample Variance'],
                     xlim=None, ylim=None, title=None, stdev=None,
                     leg_loc1='upper right', leg_loc2='lower right',
                     figsize=None):
    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666', '#000000']
    fig, ax1 = plt.subplots()
    if figsize is not None:
        fig.set_size_inches(figsize)
    if title is not None:
        fig.suptitle(title)
    if xlim is not None:
        ax1.set_xlim(xlim)
    if ylim is not None:
        ax1.set_ylim(ylim)
    ax1.set_xlabel('Number of clusters')
    y_max = 0
    x_max = 0
    for i, y_vals in enumerate(metric_values):
        label = metrics[i]
        if i == 0:
            if y_vals[0] == 'na':
                n_clusters_range = n_clusters_range[1:]
                y_vals = y_vals[1:]
            ax1.plot(n_clusters_range, y_vals, marker='o', markersize=4,
                     color = colors[i], label=label)
            ax1.set_ylabel(label)
            ax1.set_xticks(n_clusters_range)
            ax1.legend(loc=leg_loc1)
            if stdev is not None:
                ax1.fill_between(n_clusters_range, y_vals+stdev, y_vals-stdev,
                                 alpha=0.2, color = colors[i+1])
        elif i == 1:
            ax2 = ax1.twinx()
            if y_vals[0] == 'na':
                n_clusters_range = n_clusters_range[1:]
                y_vals = y_vals[1:]
            ax2.plot(n_clusters_range, y_vals, marker='o', markersize=4,
                     color = colors[i], label=label)
            ax2.set_ylabel(label)
            ax2.legend(loc=leg_loc2)
    ax1.grid(b=True)
    plt.show()
def km_elbow_diagram ( n_clusters_range , metric_values , metrics = [ 'Within Clusters Sample Variance' ] , xlim = None , ylim = None , title = None , stdev = None , leg_loc1 = 'upper right' , leg_loc2 = 'lower right' , figsize = None ) : colors = [ '#1b9e77' , '#d95f02' , '#7570b3' , '#e7298a' , '#66a61e' , '#e6a602' , '#a6761d' , '#666666' , '#000000' ] fig , ax1 = plt . subplots ( ) if figsize is not None : fig . set_size_inches ( figsize ) if title is not None : fig . suptitle ( title ) if xlim is not None : ax1 . set_xlim ( xlim ) if ylim is not None : ax1 . set_ylim ( ylim ) ax1 . set_xlabel ( 'Number of clusters' ) y_max = 0 x_max = 0 for i , y_vals in enumerate ( metric_values ) : label = metrics [ i ] if i == 0 : if y_vals [ 0 ] == 'na' : n_clusters_range = n_clusters_range [ 1 : ] y_vals = y_vals [ 1 : ] ax1 . plot ( n_clusters_range , y_vals , marker = 'o' , markersize = 4 , color = colors [ i ] , label = label ) ax1 . set_ylabel ( label ) ax1 . set_xticks ( n_clusters_range ) ax1 . legend ( loc = leg_loc1 ) if stdev is not None : ax1 . fill_between ( n_clusters_range , y_vals + stdev , y_vals - stdev , alpha = 0.2 , color = colors [ i + 1 ] ) elif i == 1 : ax2 = ax1 . twinx ( ) if y_vals [ 0 ] == 'na' : n_clusters_range = n_clusters_range [ 1 : ] y_vals = y_vals [ 1 : ] ax2 . plot ( n_clusters_range , y_vals , marker = 'o' , markersize = 4 , color = colors [ i ] , label = label ) ax2 . set_ylabel ( label ) ax2 . legend ( loc = leg_loc2 ) ax1 . grid ( b = True ) plt . show ( )
Plot a metric or metrics as a function of number of clusters.
Plot a metric or metrics as a function of number of clusters .
"""
    Plot a metric or metrics as a function of number of clusters.

    Parameters
    ----------
    n_clusters_range : list
        Number of clusters corresponding to metric values.

    metric_values : list of lists
        Lists of metric values for each metric considered.

    metrics : list of strings, optional
        Labels for metrics, default 'Within Clusters SST'.

    xlim : tuple of floats, optional
        Bounds for x-axis, default None.

    ylim : tuple of floats, optional
        Bounds for y-axis, default None.

    title : string, optional
        Title for the chart.
    """ # Plots results for each metric value

n_clusters_range
Number of clusters corresponding to metric values.
Number of clusters corresponding to metric values .
metric_values
Lists of metric values for each metric considered.
Lists of metric values for each metric considered .
metrics
Labels for metrics, default 'Within Clusters SST'.
Labels for metrics default ' Within Clusters SST ' .
xlim
Bounds for x-axis, default None.
Bounds for x - axis default None .
None
ylim
Bounds for y-axis, default None.
Bounds for y - axis default None .
None
title
Title for the chart.
Title for the chart .
stdev
leg_loc1
leg_loc2
figsize