AgglomerativeClustering,没有名为 distances_ 的属性

Geb*_*lma 5 python scikit-learn

所以我尝试了解层次聚类,但我总是在spyder上收到错误代码:

AttributeError: 'AgglomerativeClustering' object has no attribute 'distances_'
Run Code Online (Sandbox Code Playgroud)

这是代码

from sklearn.cluster import AgglomerativeClustering
    import pandas as pd

    df = pd.DataFrame({
            'x':[41, 36, 32, 34, 32, 31, 24, 30, 45, 52, 51, 52, 55, 53, 55, 61, 64, 69, 72],
            'y':[39, 36, 30, 52, 54, 46, 55, 59, 63, 70, 66, 63, 58, 23, 30, 30, 31, 32, 29]
            })
    clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=0)
    clustering.fit(df)

    import numpy as np
    from matplotlib import pyplot as plt
    from scipy.cluster.hierarchy import dendrogram

    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)



    plt.title('Hierarchical Clustering Dendogram')
           #plot the top 3 levels of the dendrogram
    plot_dendrogram(clustering)
    plt.xlabel("index data")
    plt.show()
    #print(clustering.labels_)
Run Code Online (Sandbox Code Playgroud)

我已经将scikit Learning升级到最新的,但同样的错误仍然存​​在,那么我能做什么呢?或者这段代码有问题吗

小智 5

官方文件sklearn.cluster.AgglomerativeClustering()

distances_ :形状为 (n_nodes-1,) 的类似数组,children_ 中相应位置的节点之间的距离。 仅在使用 distance_threshold 或compute_distances 设置为 True 时才计算。

我有同样的问题,我通过设置参数修复它compute_distances=True


Geb*_*lma 0

显然,在上传这个问题之前我可能会错过一些步骤,所以这是我为了解决这个问题而执行的步骤:

  1. 通过 anaconda 提示符卸载 scikit-learn
  2. 安装 scikit-learn 返回
  3. 如果不知何故你的间谍程序消失了,请使用 anaconda 提示符重新安装它
  4. 安装一些缺少的库
  5. 它可以再次工作:)