简体   繁体   中英

Numpy Methods to improve my Moving Average function

I have a function to calculate the moving average of numpy arrays imported from a file. This function works fine, but I was wondering if anyone knows a quicker method, using one of numpy methods to have the same outcome??

Data:

b = [[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [6, 7, 8],
 [4, 5, 6]]

def mod_movAvg(arr):
    rowNum, colNum = arr.shape
    res = np.zeros((rowNum - 1, colNum))
    for col in range(colNum):
        for row in range(rowNum - 1):
            res[row][col] = 0.5*(arr[row][col] + arr[row+1][col])
    return res

output:

[[1.5 2.5 3.5]
 [2.5 3.5 4.5]
 [4.5 5.5 6.5]
 [5.  6.  7. ]]

Convolution is the keyword here. You have a 2D array but perform the convolution only along one axis, so maybe this question is relevant.

  • for 1D convolution you could use numpy.convolve()
  • for 2D convolution you could use scipy.signal.covolve2d()

In you case you move the kernel with shape (2,1)

[[0.5],[0.5]]

over the array to get the values

res[row][col] = 0.5*arr[row][col] + 0.5*arr[row+1][col]

Applied to your example:

import scipy.signal as sg

b = [[1, 2, 3],
     [2, 3, 4],
     [3, 4, 5],
     [6, 7, 8],
     [4, 5, 6]]

res = sg.convolve2d(b, [[0.5], [0.5]], mode='valid')

This approach is fast and easy to generalize:

kernel = (3, 2)
sg.convolve2d(arr, np.full(kernel, 1/np.size(kernel)), mode='valid')

First, b can be converted to a numpy matrix (which are stored more efficiently in memory) using:

b = np.matrix(b)

Then, you can just do what you want more efficiently using:

result = 0.5 * (b[:-1,:] + b[1:,:])

result will be a numpy matrix too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM