Is there a way to vectorize this code to eliminate the for loop:
import numpy as np Z = np.concatenate((X, labels[:,None]), axis=1) centroids = np.empty([len(unique(labels))-1,2]) for i in unique(labels[labels>-1]): centroids[i,:]=Z[Z[:,-1]==i][:,:-1].mean(0) centroids
This code produces pseudo centroids from the DBSCAN scikit-learn example, in case you want to play with it to find a vectorized form, i.e.
labels are defined in the example.
Thanks for your help!