0

I have an array with the following shape (7352, 128, 6)

How can I get the "mean" of the 1st and 2nd dimensions with NumPy in Python?

The resulting shape of the "means" I want to obtain is (1, 6).

RabbitBadger
  • 539
  • 7
  • 19

3 Answers3

3

You can do:

np.mean(x, axis=(0, 1))

You resulting shape would be (6,) though.

Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
1

numpy.mean() with axis argument is proper solution.

import numpy as np

x = np.random.rand(7352, 128, 6)
x_mean = np.mean(x, axis=(0, 1))

print(x_mean.shape)  # -> (6,)
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
1

You can use the axis parameter of the mean function:

img = np.random.rand(7352,128,6)
y_mean = img.mean(axis=0) # mean over y (row)
x_mean = img.mean(axis=1) # mean over x (column)
z_mean = img.mean(axis=(0,1)) # the mean you want (shape is (6,))

This post may also help.

KeKsBoTer
  • 182
  • 7