[Python] HDBSCAN Interpretation and Logic

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have made a basic HDBSCAN model (picture output below) but I need to figure out names for the individual clusters. Is there a way I can get something like a decision tree or the parameters for each cluster as that is what I need to classify these clusters properly. For example: category -1 has to have COMP_2_Pass = 1 and REBOUND_3 < -2300 or something that shows filtering logic like this. Thanks

    GRAPH

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    import umap.umap_ as umap
    import hdbscan
    from sklearn.preprocessing import StandardScaler
    #Drop label column
    X = df_cleaned.drop(columns=['Failure_area'])

    #Scale features
    X_scaled = StandardScaler().fit_transform(X)

    #UMAP for 2D projection
    reducer = umap.UMAP(random_state=42)
    X_umap = reducer.fit_transform(X_scaled)

    # HDBSCAN clustering
    clusterer = hdbscan.HDBSCAN(min_cluster_size=40, min_samples= 55)
    cluster_labels = clusterer.fit_predict(X_scaled)

    # Add results to DataFrame
    df_clustered = df_cleaned.copy()
    df_clustered['HDBSCAN_cluster'] = cluster_labels

    from matplotlib.colors import ListedColormap

    # Get unique cluster labels
    unique_labels = np.unique(cluster_labels)
    n_clusters = len(unique_labels)

    # Create a distinct color map with one color per cluster
    # Assign gray to noise cluster
    colors = ['#0096FF'] + [plt.cm.tab20(i) for i in range(1, n_clusters)]
    cmap = ListedColormap(colors)
    #cmap = plt.cm.get_cmap('tab20', n_clusters) # Use tab20, tab10, or other qualitative colormaps

    # Plot
    plt.figure(figsize=(10, 7))
    scatter = plt.scatter(
    X_umap[:, 0], X_umap[:, 1],
    c=cluster_labels,
    cmap=cmap,
    s=10
    )

    plt.title("UMAP + HDBSCAN Clustering of Entire Dataset")
    plt.xlabel("UMAP1")
    plt.ylabel("UMAP2")
    plt.grid(True)

    # Add colorbar with correct ticks
    cbar = plt.colorbar(scatter, ticks=unique_labels)
    cbar.set_label('Cluster ID')

    plt.show()

    print(df_clustered['HDBSCAN_cluster'].value_counts().sort_values(ascending=False))

    Continue reading...

Compartilhe esta Página