I need to vectorize the following in order for the code can run faster
<p>This portion I was able to vectorize and get rid of a nested loop.</p>

<pre><code>def EMalgofast(obsdata, beta, pjt):
     n = np.shape(obsdata)[0]
     g = np.shape(pjt)[0]
     zijtpo = np.zeros(shape=(n,g))
     for j in range(g):
         zijtpo[:,j] = pjt[j]*stats.expon.pdf(obsdata,scale=beta[j])

     zijdenom = np.sum(zijtpo, axis=1)
     zijtpo = zijtpo/np.reshape(zijdenom, (n,1))

     pjtpo = np.mean(zijtpo, axis=0)
</code></pre>

<p>I wasn't able to vectorize the portion below. I need to figure that out</p>

<pre><code>     betajtpo_1 = []
     for j in range(g):
         num = 0
         denom = 0
         for i in range(n):
             num = num + zijtpo[i][j]*obsdata[i]
             denom = denom + zijtpo[i][j]
         betajtpo_1.append(num/denom)

     betajtpo = np.asarray(betajtpo_1)

     return(pjtpo,betajtpo)
</code></pre>

<p>I'm guessing Python is not your first programming language based on what I see.  The reason I'm saying this is that in python, normally we don't have to deal with manipulating indexes.  You act directly on the value or the key returned.  Make sure not to take this as an offense, I do the same coming from C++ myself.  It's a hard to remove habits ;).</p>

<p>If you're interested in performance, there is a good presentation by Raymond Hettinger on what to do in Python to be optimised and beautiful :
<a href="https://www.youtube.com/watch?v=OSGv2VnC0go" rel="nofollow">https://www.youtube.com/watch?v=OSGv2VnC0go</a></p>

<p>As for the code you need help with, is this helping for you?  It's unfortunatly untested as I need to leave...
ref:
<a href="http://stackoverflow.com/questions/6967463/iterating-over-a-numpy-array">Iterating over a numpy array</a></p>

<p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.true_divide.html" rel="nofollow">http://docs.scipy.org/doc/numpy/reference/generated/numpy.true_divide.html</a></p>

<pre><code> def EMalgofast(obsdata, beta, pjt):
     n = np.shape(obsdata)[0]
     g = np.shape(pjt)[0]
     zijtpo = np.zeros(shape=(n,g))
     for j in range(g):
         zijtpo[:,j] = pjt[j]*stats.expon.pdf(obsdata,scale=beta[j])

     zijdenom = np.sum(zijtpo, axis=1)
     zijtpo = zijtpo/np.reshape(zijdenom, (n,1))

     pjtpo = np.mean(zijtpo, axis=0)
     betajtpo_1 = []

     #manipulating an array of numerator and denominator instead of creating objects each iteration
     num=np.zeros(shape=(g,1))
     denom=np.zeros(shape=(g,1))
     #generating the num and denom real value for the end result
     for (x,y), value in numpy.ndenumerate(zijtpo):
         num[x],denom[x] = num[x] + value *obsdata[y],denom[x] + value 

     #dividing all at once after instead of inside the loop
     betajtpo_1= np.true_divide(num/denom)

     betajtpo = np.asarray(betajtpo_1)

     return(pjtpo,betajtpo)
</code></pre>

<p>Please leave me some feedback !</p>

<p>Regards,</p>

<p>Eric Lafontaine</p>

python python-2.7 python-3.x numpy