Speed up a program that calculate the average of the neighbors in a huge array

我与影子孤独终老i 提交于 2019-12-23 01:30:10

问题


I have a problem with the speed of my program. I want to calculate the average of four neighbors in a huge array. Here is a part of my code. Do you have any ideas how to change the last line? Or should I use another array?

for a in np.arange(100000):
    for x in np.arange(size):
        for y in np.arange(size):
            if unchangeableflag[x*size+y] == 0:
                vnew[x*size+y] = (v[(x+1)*size+y] + v[(x-1)*size+y] + v[x*size+y+1] + v[x*size+y-1]) / 4.0

回答1:


You would not need the loop at all. Assuming v, vnew and unchangeableflag are 1-d arrays with size*size entries, you can do

v = v.reshape(size, size)
vnew = vnew.reshape(size, size)
unchangeableflag = unchangeableflag.reshape(size, size)
average = v[1:-1, 2:]
average += v[1:-1, :-2] 
average += v[2:, 1:-1]
average += v[-2:, 1:-1]
average /= 4.0
vnew[1:-1, 1:-1][unchangeableflag[1:-1, 1:-1] == 0] = average

But what are you actually trying to achieve? This looks suspiciously like you could get away with some application of the discrete Laplacian.

(Note that this assumes that v contains floating point numbers. If the dtype of `v´ is sime integral type, you need a slight modification.)




回答2:


You should consider using SciPy's convolution filter or the generic_filter. This is still computationally intensive, but way faster than looping. Normally, when doing this type of averaging, the central element is also included. Note that these solutions also apply to multi-dimensional arrays.

from scipy import ndimage
footprint = scipy.array([[0,0.25,0],[0.25,0,0.25],[0,0.25,0]])
filtered_array = scipy.convolve(array, footprint)

OR

from scipy import ndimage
def myfunction(window):
    return (window[0,1] + window[1,0] + window[1,2] + window[2,1]) / 4
filtered_array = scipy.generic_filter(array, myfunction, size=3)



回答3:


I am unsure but you can remove some invariant.

for a in np.arange(100000):
    for x in np.arange(size):
        for y in np.arange(size):
            t = x*size+y
            if unchangeableflag[t] == 0:
                vnew[t] = (v[t+size] + v[t-size] + v[t+1] + v[t-1]) / 4.0


来源:https://stackoverflow.com/questions/4829738/speed-up-a-program-that-calculate-the-average-of-the-neighbors-in-a-huge-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!