I am clustering with Affinity Propagation and then calculating Silhouette Score in the following way:
af = AffinityPropagation(preference=0.7, affinity='precomputed').fit(X) #-11
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
n_clusters = len(np.unique(labels))
n_cluster_list.append(n_clusters)
n_clusters
silhouette_score(frechet, labels, metric="precomputed")
For this question I put the value of preference 0,7 and it works. Works, because with value of 0.7 my number of clusters is more then 1 and less than maximum number of clusters. In other words 1 < clusters < len(labels).
But if I put the value of preference which would compute and say that number of clusters is 1 or len(lables) (97 in my case), I will get the following errors:
ValueError: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive)
ValueError: Number of labels is 97. Valid values are 2 to n_samples - 1 (inclusive)
To overcome this, I have decided to create an if and elif condition and put into the code:
af = AffinityPropagation(preference=0.7, affinity='precomputed').fit(X) #-11
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
if labels == 97: #or len(labels)
continue
elif labels == 1:
continue
n_clusters = len(np.unique(labels))
n_cluster_list.append(n_clusters)
But still getting the error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
n_clusters
Aucun commentaire:
Enregistrer un commentaire