sklearn GMM classification prediction (component assignment) order
<p>I'm trying to classify cells into populations. When I use:</p>

<p><code>gmix = mixture.GMM(n_components=3, covariance_type='full')
gmix.fit(samples)</code></p>

<p>The means output, from the code below, changes in order, unless I set:
 <code>np.radom.seed(0)</code>.</p>

<p><code>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)]
</code></p>

<p>I would like the classes sorted by the X axis mean (first item of each class) ie:  </p>

<pre><code>[[  3.25492404e+02   2.88403293e-02]  
[  3.73942908e+02   3.25283512e-02] 
[  5.92577646e+02   4.40595768e-02]]
</code></pre>

<p>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. </p>

<p>I tried:</p>

<pre><code>gmix.means_ = np.sort(gmix.means_, axis = 0)
</code></pre>

<p>But then the gmix.covars_ and gmix.weights_ also need to be sorted accordingly, which is where I'm stuck!</p>

<p>Many thanks!</p>

<p>Edit 4/5/16:</p>

<p>Thanks for the help and steering me in the right direction. Here is my poorly written but working version:</p>

<pre><code>    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
</code></pre>