Я пытаюсь использовать Affinity Propagation al go в наборе данных, на который я сейчас смотрю. Вот мой код.
data = np.asarray(df['helpful_count'])
data = data.reshape(-1, 1)
#data = data.reshape(1, -1)
#type(data)
#len(data)
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets import make_blobs
X = data
# Compute Affinity Propagation
af = AffinityPropagation(preference=20).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
n_clusters_ = len(cluster_centers_indices)
print('Estimated number of clusters: %d' % n_clusters_)
Поле моего полезного_счета в моем фрейме данных с именем df выглядит следующим образом:
0
1
9
...
712
702
694
Теперь я пытаюсь отобразить результаты , используя приведенный ниже пример кода.
**import matplotlib.pyplot as plt
from itertools import cycle
plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()
Пример кода с этого сайта.
https://scikit-learn.org/stable/auto_examples/cluster/plot_affinity_propagation.html
Эта строка:
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
Выдает эту ошибку:
IndexError: index 1 is out of bounds for axis 1 with size 1
Как я могу сделать эту работу? Спасибо!