I have a numpy array of (858, 891) shape:
[[0.41128411 0.41473465 0.41853683 ... 0.49271687 0.49322761 0.49389444]
[0.40997746 0.41332964 0.41707197 ... 0.49285828 0.49338905 0.49406584]
[0.40780573 0.41098776 0.41461104 ... 0.49297105 0.49353365 0.49422911]
...
[0.62126761 0.61681614 0.61088054 ... 0.66176147 0.66263369 0.66313708]
[0.62241648 0.61767095 0.61128526 ... 0.66124009 0.66208772 0.6625708 ]
[0.62377412 0.61880136 0.6120562 ... 0.66079591 0.66163067 0.66210466]]
I need to find a mean of 4 columns like this and stack it with the mean of next 4 columns:
a = arr[:, 0:4]
b = arr[:, 4:8]
a = a.mean(axis=1)
b = b.mean(axis=1)
new = np.column_stack((a,b))
array([[0.41670609, 0.42968413],
[0.41529677, 0.42850663],
[0.41293143, 0.42641906],
...,
[0.61311844, 0.57922079],
[0.6136733 , 0.57750982],
[0.61455882, 0.57644654]])
How can I loop over the original array to get a new array with the shape of (858,223)? The last iteration should be over 3 columns.
CodePudding user response:
Use variable x
arr[:, x:x 4]
with loop
for x in range(0, arr.shape[1], 4):
and keep mean
on list
and later convert this list
to array
.
import numpy as np
arr = np.random.rand(858, 891)
# - before loop -
results = []
# - loop -
for x in range(0, arr.shape[1], 4):
mean = arr[:, x:x 4].mean(axis=1)
results.append(mean)
# - after loop -
new = np.column_stack(results)
print(new)