Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print or plot perfomance metrics for clusterer result #262

Open
mikhailsirenko opened this issue Apr 20, 2023 · 2 comments
Open

Print or plot perfomance metrics for clusterer result #262

mikhailsirenko opened this issue Apr 20, 2023 · 2 comments

Comments

@mikhailsirenko
Copy link
Contributor

Maybe, it could be a good addition to allow for printing/plotting permanence metrics, e.g. silhouette_score or any other that is more applicable in the case of AgglomerativeClustering. Or you prefer to keep those apart?

@quaquel
Copy link
Owner

quaquel commented Apr 20, 2023

Can you give a slightly more elaborate example showing both the current and the desired behavior?

@mikhailsirenko
Copy link
Contributor Author

mikhailsirenko commented Apr 21, 2023

E.g.

def plot_score(data:pd.DataFrame, metric:str, linkage:str, max_clusters:int, score:str='silhouette'):
    """Plot clustering perfomance score for different number of clusters.

    Args:
        data (pd.DataFrame): Data to cluster.
        metric (str): Metric to use for clustering.
        linkage (str): Linkage method to use for clustering.
        max_clusters (int): Maximum number of clusters to try.
        score (str, optional): Score to use. Defaults to 'silhouette'.

    Raises:
        ValueError: If the score is unknown.

    Returns:
        None
    """
    if score == 'silhouette':
        score_function = silhouette_score
    elif score == 'calinski_harabasz':
        score_function = calinski_harabasz_score
    elif score == 'davies_bouldin':
        score_function = davies_bouldin_score
    else:
        raise ValueError(f'Unknown score: {score}')

    scores = {}
    for i in range(2, max_clusters + 1):
        labels = clusterer.apply_agglomerative_clustering(data, i, metric=metric, linkage=linkage)
        scores[i] = score_function(data, labels, metric=metric)
    fig, ax = plt.subplots(figsize=(6, 4))
    ax.plot(list(scores.keys()), list(scores.values()))
    ax.set_xlabel('Number of clusters')
    ax.set_ylabel(f'{score.capitalize()} score')
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants