AgglomerativeClustering, no attribute called distances_

So I tried to learn about hierarchical clustering, but I alwas get an error code on spyder:

AttributeError: 'AgglomerativeClustering' object has no attribute 'distances_'

This is the code

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_)

I have upgraded the scikit learning to the newest one, but the same error still exist, so is there anything that I can do? or is there something wrong in this code

Upvotes: 5

Views: 9040

Answers (4)

singAsong
singAsong

Reputation: 61

official document of sklearn.cluster.AgglomerativeClustering() says

distances_ : array-like of shape (n_nodes-1,) Distances between nodes in the corresponding place in children_. Only computed if distance_threshold is used or compute_distances is set to True.

I have the same problem and I fix it by set parameter compute_distances=True

Upvotes: 6

sogu
sogu

Reputation: 3096

aggmodel = AgglomerativeClustering(distance_threshold=None,
                                   n_clusters=10,
                                   affinity = "manhattan",
                                   linkage = "complete",
                                  )  
aggmodel = aggmodel.fit(data1)

aggmodel.n_clusters_

#aggmodel.labels_

Upvotes: 0

Atif Rizwan
Atif Rizwan

Reputation: 685

Update sklearn from 21.* to 22.*

pip install -U scikit-learn

Upvotes: 0

Apparently, I might miss some step before I upload this question, so here is the step that I do in order to solve this problem:

  1. Uninstall scikit-learn through anaconda prompt
  2. Instal scikit-learn back
  3. If somehow your spyder is gone, install it again with anaconda prompt
  4. install some missing library
  5. It can work again :)

Upvotes: 0

Related Questions