1

我正在尝试绘制层次聚类的树状图,并将其聚类在 2d 散点图中。

为此,我使用以下代码:

def plot_dendrogram(model, **kwargs):
    # Create linkage matrix and then plot the dendrogram

    # create the counts of samples under each node
    counts = np.zeros(model.children_.shape[0])
    n_samples = len(model.labels_)
    for i, merge in enumerate(model.children_):
        current_count = 0
        for child_idx in merge:
            if child_idx < n_samples:
                current_count += 1  # leaf node
            else:
                current_count += counts[child_idx - n_samples]
        counts[i] = current_count

    linkage_matrix = np.column_stack([model.children_, model.distances_,
                                      counts]).astype(float)

    # Plot the corresponding dendrogram
    dendrogram(linkage_matrix, **kwargs)

for linkage in ['ward', 'complete', 'average', 'single']:
    cluster = AgglomerativeClustering(linkage=linkage, distance_threshold=0, n_clusters=None)  
    y_kmeans = cluster.fit(X)
    #y_kmeans = cluster.fit_predict(X)
    plt.figure(figsize=(5, 5))
    plt.scatter(X[:, 0], X[:, 1], c=cluster.fit_predict(X), s=50, cmap='viridis')
    #sil[k] = metrics.silhouette_score(X, y_kmeans, metric='sqeuclidean')
    plt.figure(figsize=(5, 5))
    plot_dendrogram(y_kmeans, truncate_mode='level', p=2)
    plt.xlabel("Number of points in node (or index of point if no parenthesis).")
    plt.show()

我明白了:

在此处输入图像描述 在此处输入图像描述

问题是如何绘制 k=2 的正确散点图,如树状图,以便可视化图表中的集群。

4

0 回答 0