How to get the clusters from Affinity propagation algorithm?

Question:

I want the data points after using AP clustering as a 3d nparray. First index to access the clusters, second and third index to access the clustered data. I am using the sklearn library function.
af = AffinityPropagation().fit(X)

Asked By: rk6t7

||

Answers:

You can access the cluster labels for each data point by calling the labels_ attribute on the fitted Affinity Propagation object, like so:

cluster_labels = af.labels_

You can then use the cluster labels to index into your original data, X, to get the data points in each cluster. For example, to get the data points in cluster 0, you can do:

cluster_0_data = X[cluster_labels == 0]

You can use a nested list comprehension to get the 3D numpy array of the data points after using Affinity Propagation clustering, like so:

clustered_data = np.array([X[cluster_labels == i] for i in np.unique(cluster_labels)])

This will give you a 3D numpy array where the first index corresponds to the cluster number, and the second and third indices correspond to the coordinates of the data points in that cluster.

Answered By: Zanmato
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.