python - Partial sum over an array given a list of indices -
i have 2d matrix , need sum subset of matrix elements, given 2 lists of indices imp_list
, bath_list
. here i'm doing right now:
s = 0.0 in imp_list: j in bath_list: s += k[i,j]
which appears slow. better solution perform sum?
if you're working large arrays, should huge speed boost using numpy's own indexing routines on python's for
loops.
in general case can use np.ix_
select subarray of matrix sum:
k[np.ix_(imp_list, bath_list)].sum()
note np.ix_
carries overhead, if 2 lists contain consecutive or evenly-spaced values, it's worth using regular slicing index array instead (see method3()
below).
here's data illustrate improvements:
k = np.arange(1000000).reshape(1000, 1000) imp_list = range(100) # [0, 1, 2, ..., 99] bath_list = range(200) # [0, 1, 2, ..., 199] def method1(): s = 0 in imp_list: j in bath_list: s += k[i,j] return s def method2(): return k[np.ix_(imp_list, bath_list)].sum() def method3(): return k[:100, :200].sum()
then:
in [80]: method1() == method2() == method3() out[80]: true in [91]: %timeit method1() 10 loops, best of 3: 9.93 ms per loop in [92]: %timeit method2() 1000 loops, best of 3: 884 µs per loop in [93]: %timeit method3() 10000 loops, best of 3: 34 µs per loop
Comments
Post a Comment