title: increasing speed of python loop for ML algorithm
body: <p>I am trying to perform maximum likelihood algorithm on an image where mean vectors, and covariance matrices are known. The problem is that the calculation is really really slow...and I work on quite big images (5100X7020).</p>
  
  <p>The following piece of code shows the calculation of the probability map of a given class for pixels in the image</p>
  
  <pre><code>import numpy as np
  from numpy.linalg import inv
  from numpy.linalg import det
  ...
  
  probImage1 = []
  probImage1Vector = []
  
  norm = 1.0 / (np.power((2*np.pi), i/2) * np.sqrt(np.linalg.det(covMatrixClass1)))
  for x in xrange(x_img):
      for y in xrange(y_img):
          X = realImage[x,y]
          mult1 = np.multiply(-0.5,np.transpose(X - meanVectorClass1))
          mult2 = np.dot(np.linalg.inv(covMatrixClass1),(X - meanVectorClass1))
          multMult = np.dot(mult1,mult2)
          expo = np.exp(multMult)     
          probImage1Vector.append(np.multiply(norm,expo))
      probImage1.append(probImage1Vector)
      probImage1Vector = [] 
  </code></pre>
  
  <p>Do you have any idea how to speed up this algorithm?</p>
  
  <p>Thanks in advance</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I decreased time calculation time by placing the matrix inverse calculation outside the for loop, but still it is still very slow...
  Itself iterating over the image and getting the pixel value doesnt take too much time, but even a simple matrix subtraction slow it down significantly...</p>
  
  <p>i,j = covMatrixClass1.shape</p>
  
  <p>probImage1 = []
  probImage1Vector = []</p>
  
  <pre><code>norm = 1.0 / (np.power((2*np.pi), i/2) *      np.sqrt(np.linalg.det(covMatrixClass1)))
  covMatrixInverz = np.linalg.inv(covMatrixClass1)
  for x in xrange(x_img):
      for y in xrange(y_img):
          X = realImage[x,y]
          pixelValueDifference = X - meanVectorClass1
          mult1 = np.multiply(-0.5,np.transpose(pixelValueDifference))
          mult2 = np.dot(covMatrixInverz,pixelValueDifference)
          multMult = np.dot(mult1,mult2)
          expo = np.exp(multMult)     
          probImage1Vector.append(np.multiply(norm,expo))
      probImage1.append(probImage1Vector)
      probImage1Vector = []
  </code></pre>
  
  <p>PLEASE, any advice how to speed up this code is appriciated!</p>