Deal all,
I have data in the form of a numpy array and want to perform summation on parts of the array:
Example:
X=[[1, 2, 3,4],
[5, 6, 7,8]]
I want the output to be the summation of elements that within 2x2 window.
Output Needed:
[[14],[22]]
This is similar to cross-correlation with ones(2,2) but i want the shift to be more than ones element.
This is just an example and the actual data is very large so i cannot manually calculate them. I need a for loop to do that but struggling to do it.
Your Help is appreciated.
CodePudding user response:
here is one way:
window_size = 2 # 2x2
res = np.array(np.array_split(X.sum(axis=0),window_size)).sum(axis=1)
output:
>> [14 22]
CodePudding user response:
You can reshape
to 2 columns in "Fortran" order and sum
:
X = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
X.reshape(-1, 2, order='F').sum(0)
output: array([14, 22])
With more columns you should probably generalize with:
X.reshape(-1, X.shape[1]//2, order='F').sum(0)