Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the clusters from Affinity propagation algorithm?

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading