如何使这个numpy代码更快?

时间:2016-06-06 00:51:07

标签: python performance numpy for-loop

import numpy as np
w_array=np.random.rand(120,10)
w = 1
for i in xrange(100):
    if i==3:
        w*=w_array[i,5]
    else:
        w *= (1 - w_array[i, :5 + 1].sum())

如何更快地制作此代码?

2 个答案:

答案 0 :(得分:2)

我将演示如何更快地完成这样的事情:

首先是迭代:

In [3]: x=np.arange(10).reshape(5,2)

In [4]: for i in range(5):
   ...:     if i==3:
   ...:         print(x[i,:])
   ...:     else:
   ...:         print(x[i,:].sum())
   ...:         
1
5
9
[6 7]
17

生成相同的值而不进行迭代:

In [9]: x[3,:]
Out[9]: array([6, 7])

In [10]: x[np.arange(5)!=3,:].sum(1)
Out[10]: array([ 1,  5,  9, 17])

这是否足以提示?

答案 1 :(得分:2)

根据@hpaulj的回答,我测试了以下代码。它似乎改善了很多..非常感谢!

import numpy as np
w_array = np.random.rand(120, 10)
def program1(w_array):
    w = 1
    for i in xrange(100):
        if i == 3:
            w *= w_array[i, 5]
        else:
            w *= (1 - w_array[i, :5 + 1].sum())
    return w

def program2(w_array):
    w_array=w_array[:100,:]
    return w_array[3,5]*((1-w_array[np.arange(100)!=3,:5+1].sum(1)).prod())

%timeit result1=program1(w_array)
1000 loops, best of 3: 202 µs per loop
%timeit result2=program2(w_array)
100000 loops, best of 3: 14.2 µs per loop