Итак, я попытался узнать об иерархической кластеризации, но я всегда получал код ошибки на spyder:
AttributeError: 'AgglomerativeClustering' object has no attribute 'distances_'
Это код
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_)
Я обновил обучение Scikit. до самой новой, но та же ошибка все еще существует, так что я могу сделать что-нибудь? или что-то не так в этом коде