呼啦一阵风
在矢量化方面,您可能会使用基于np.add.at的东西:def yaco_addat(bb,slope,fac,offset): barray = np.zeros((2,2259),dtype=np.float64) nlin_range = np.arange(nlin) npix_range = np.arange(npix) ling_mat = (np.ceil((npix_range-nlin_range[:,None]/slope)*fac)+1-offset).astype(np.int) np.add.at(barray[0,:],ling_mat,1) np.add.at(barray[1,:],ling_mat,bb) return barray但是,我建议您直接使用numba进行优化,使用@jit带有 option 的装饰器nopython=True,它可以为您提供:import numpy as npfrom numba import jitnlin, npix = 478, 480bb = np.random.rand(nlin,npix)slope = -8fac = 4offset= 0def yaco_plain(bb,slope,fac,offset): barray = np.zeros((2,2259),dtype=np.float64) for y in range(nlin): for x in range(npix): ling=(np.ceil((x-y/slope)*fac)+1-offset).astype(np.int) barray[0,ling] += 1 barray[1,ling] += bb[y,x] return barray@jit(nopython=True)def yaco_numba(bb,slope,fac,offset): barray = np.zeros((2,2259),dtype=np.float64) for y in range(nlin): for x in range(npix): ling = int((np.ceil((x-y/slope)*fac)+1-offset)) barray[0,ling] += 1 barray[1,ling] += bb[y,x] return barray让我们检查一下输出np.allclose(yaco_plain(bb,slope,fac,offset),yaco_addat(bb,slope,fac,offset))>>> Truenp.allclose(yaco_plain(bb,slope,fac,offset),yaco_jit(bb,slope,fac,offset))>>> True现在是时候了%timeit yaco_plain(bb,slope,fac,offset)>>> 648 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)%timeit yaco_addat(bb,slope,fac,offset)>>> 27.2 ms ± 92.3 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)%timeit yaco_jit(bb,slope,fac,offset)>>> 505 µs ± 995 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)产生一个优化的函数,它比最初的 2 个循环版本53x快得多,也比第np.add.at一个版本快。希望这可以帮助。