【问题标题】:I need to vectorize the following in order for the code can run faster我需要对以下内容进行矢量化,以使代码运行得更快
【发布时间】:2016-10-13 23:00:30
【问题描述】:

这部分我能够矢量化并摆脱嵌套循环。

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 = []
     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)

【问题讨论】:

    标签: python python-2.7 python-3.x numpy


    【解决方案1】:

    根据我所见,我猜 Python 不是您的第一种编程语言。我这么说的原因是在python中,通常我们不必处理操作索引。您直接对返回的值或键进行操作。确保不要将此视为冒犯,我自己也从 C++ 做同样的事情。习惯很难改掉;)。

    如果您对性能感兴趣,Raymond Hettinger 有一个很好的演示文稿,介绍了如何在 Python 中进行优化和美观: https://www.youtube.com/watch?v=OSGv2VnC0go

    至于你需要帮助的代码,这对你有帮助吗?不幸的是,它未经测试,因为我需要离开...... 参考: Iterating over a numpy array

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.true_divide.html

     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)
    

    请给我一些反馈!

    问候,

    埃里克·拉方丹

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-13
      • 2022-09-30
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      相关资源
      最近更新 更多