title: How To Increase Sklearn GMM predict() Performance Speed?
body: <p>I am using <em>Sklearn</em> to estimate the Gaussian Mixture Model (GMM) on some data. </p>
  
  <p>After the estimation, I have many query points. I would like to obtain their probabilities of belonging to each of the estimated Gaussian. </p>
  
  <p>The code below works. However, the <code>gmm_sk.predict_proba(query_points)</code> part is very slow as I need to run it multiple times on 100000 sets of samples, where each sample contains 1000 points. </p>
  
  <p>I guess that it happens because it is sequential. Is there a way to make it parallel? Or any other way to make it faster? Maybe on GPU using <em>TensorFlow</em>? </p>
  
  <p>I saw <em>TensorFlow</em> has its own <strong>GMM</strong> algorithm but it was very hard to implement.  </p>
  
  <p>Here is the code that I have written: </p>
  
  <pre><code>import numpy as np
  from sklearn.mixture import GaussianMixture
  import time
  
  
  n_gaussians = 1000
  covariance_type = 'diag'
  points = np.array(np.random.rand(10000, 3), dtype=np.float32)
  query_points = np.array(np.random.rand(1000, 3), dtype=np.float32)
  start = time.time()
  
  #GMM with sklearn
  gmm_sk = GaussianMixture(n_components = n_gaussians, covariance_type=covariance_type)
  gmm_sk.fit(points)
  mid_t = time.time()
  elapsed = time.time() - start
  print("learning took "+ str(elapsed))
  
  temp = []
  for i in range(2000):
      temp.append(gmm_sk.predict_proba(query_points))
  
  end_t = time.time() - mid_t
  print("predictions took " + str(end_t))    
  </code></pre>
  
  <p>I solved it ! using <code>multiprocessing</code>. 
  just replaced </p>
  
  <pre><code>temp = []
  for i in range(2000):
      temp.append(gmm_sk.predict_proba(query_points))
  </code></pre>
  
  <p>with </p>
  
  <pre><code>import multiprocessing as mp
      query_points = query_points.tolist()
      parallel = mp.Pool()
      fv = parallel.map(par_gmm, query_points)
      parallel.close()
      parallel.join()
  </code></pre>