Reputation: 413
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. ]]
Upvotes: 1
Views: 110
Reputation: 4537
Convolution
is the keyword here. You have a 2D array but perform the convolution only along one axis, so maybe this question is relevant.
numpy.convolve()
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')
Upvotes: 1
Reputation: 50488
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.
Upvotes: 1