I'm trying to classify cells into populations. When I use:
gmix = mixture.GMM(n_components=3, covariance_type='full')
gmix.fit(samples)
The means output, from the code below, changes in order, unless I set:
np.radom.seed(0)
.
print ("gmix.means \n", gmix.means_)
colors = ['r' if i==0 else ('g' if i==1 else ('b' if i ==2 else 'm'))for i in gmix.predict(samples)]
I would like the classes sorted by the X axis mean (first item of each class) ie:
[[ 3.25492404e+02 2.88403293e-02]
[ 3.73942908e+02 3.25283512e-02]
[ 5.92577646e+02 4.40595768e-02]]
So in the code above red would always be 325, green 372 and blue 592. At the moment I don't think there is anything sorting the output.
I tried:
gmix.means_ = np.sort(gmix.means_, axis = 0)
But then the gmix.covars_ and gmix.weights_ also need to be sorted accordingly, which is where I'm stuck!
Many thanks!
Edit 4/5/16:
Thanks for the help and steering me in the right direction. Here is my poorly written but working version:
sort_indices = gmix.means_.argsort(axis = 0)
order = sort_indices[:, 0]
print('\norder:', order)
gmix.means_ = gmix.means_[order,:]
gmix.covars_ = gmix.covars_[order, :]
print ("\n sorted gmix.covars \n", gmix.covars_)
print ("\n\nori gmix.weights \n", gmix.weights_)
w = np.split(gmix.weights_,3)
w = np.asarray(w)
w = np.ravel(w[order,:])
gmix.weights_ = w