Mean of 3d array python

Can always use np.einsum:

>>> a = np.arange(11*5*5).reshape(11,5,5)
>>> np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
array([ 12,  37,  62,  87, 112, 137, 162, 187, 212, 237, 262])

Works on higher dimensional arrays (all of these methods would if the axis labels are changed):

>>> a = np.arange(10*11*5*5).reshape(10,11,5,5)
>>> (np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])).shape
(10, 11)

Faster to boot:

a = np.arange(11*5*5).reshape(11,5,5)

%timeit a.reshape(11, 25).mean(axis=1)
10000 loops, best of 3: 21.4 us per loop

%timeit a.mean(axis=(1,2))
10000 loops, best of 3: 19.4 us per loop

%timeit np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
100000 loops, best of 3: 8.26 us per loop

Scales slightly better then the other methods as array size increases.

Using dtype=np.float64 does not change the above timings appreciably, so just to double check:

a = np.arange(110*50*50,dtype=np.float64).reshape(110,50,50)

%timeit a.reshape(110,2500).mean(axis=1)
1000 loops, best of 3: 307 us per loop

%timeit a.mean(axis=(1,2))
1000 loops, best of 3: 308 us per loop

%timeit np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
10000 loops, best of 3: 145 us per loop

Also something that is interesting:

%timeit np.sum(a) #37812362500.0
100000 loops, best of 3: 293 us per loop

%timeit np.einsum('ijk->',a) #37812362500.0
100000 loops, best of 3: 144 us per loop

Contents

  • Introduction
  • Example 1: Mean of all the elements in a NumPy Array
  • Example 2: Mean of elements of NumPy Array along an axis
  • Example 3: Mean of elements of NumPy Array along Multiple Axis
  • Summary

NumPy Mean: To calculate mean of elements in a array, as a whole, or along an axis, or multiple axis, use numpy.mean() function.

In this tutorial we will go through following examples using numpy mean() function.

  • Mean of all the elements in a NumPy Array.
  • Mean of elements of NumPy Array along an axis.
  • Mean of elements of NumPy Array along multiple axis.

Example 1: Mean of all the elements in a NumPy Array

In this example, we take a 2D NumPy Array and compute the mean of the Array.

Python Program

import numpy as np

#initialize array
A = np.array([[2, 1], [5, 4]])

#compute mean
output = np.mean(A)

print(output)

Run

Output

3.0

Run

Mean

Mean = (2 + 1 + 5 + 4)/4
     = 12/4
     = 3.0

Run

Example 2: Mean of elements of NumPy Array along an axis

In this example, we take a 2D NumPy Array and compute the mean of the elements along a single, say axis=0.

Pass the named argument axis to mean() function as shown below.

Python Program

import numpy as np

#initialize array
A = np.array([[2, 1], [5, 4]])

#compute mean
output = np.mean(A, axis=0)

print(output)

Run

Output

[3.5 2.5]

Run

Understanding Axis

As we have provided axis=0 as argument, this axis gets reduced to compute mean along this axis, keeping other axis.

       [    [2, 1],  [5, 4]   ]
axis:  0    1        1 

[2, 1] and [5, 4] are the elements of axis=0.

Run

Mean

Mean = ([2, 1] + [5, 4])/2
     = [(2 + 5)/2, (1 + 4)/2]
     = [7/2, 5/2]
     = [3.5, 2.5]

Run

Example 3: Mean of elements of NumPy Array along Multiple Axis

In this example, we take a 3D NumPy Array, so that we can give atleast two axis, and compute the mean of the Array.

Pass the named argument axis, with tuple of axes, to mean() function as shown below.

Python Program

import numpy as np

#initialize array
A = np.array([[[2, 1], [5, 4]], [[3, 9], [6, 8]]])

#compute mean
output = np.mean(A, axis=(0, 1))

print(output)

Run

Output

[4.  5.5]

Run

Understanding Axis

As we have provided axis=(01 1) as argument, these axis gets reduced to compute mean along this axis, keeping other axis. which is axis: 2.

       [    [  [2, 1], [5, 4]], [  [3, 9], [6, 8] ]  ]
axis:  0    1  2       2        1  2       2

[[2, 1], [5, 4]] and [[3, 9], [6, 8]] are the elements of axis=0.
[2, 1], [5, 4], [3, 9], [6, 8] are the elements of axis=1.

Run

Mean

Mean = ([2, 1] + [5, 4] + [3, 9] + [6, 8])/4
     = [(2 + 5 + 3 + 6)/4, (1 + 4 + 9 + 8)/4]
     = [4.0, 5.5]

Run

Summary

In this tutorial of Python Examples, we learned how to find mean of a Numpy, of a whole array, along an axis, or along multiple axis, with the help of well detailed Python example programs.

How do you find the median of a 3D array in Python?

arr : [array_like]input array. axis : [int or tuples of int]axis along which we want to calculate the median..
Given data points..
Arrange them in ascending order..
Median = middle term if total no. of terms are odd..
Median = Average of the terms in the middle (if total no. of terms are even).

How do you read a 3D array in Python?

import numpy >>> a = numpy. loadtxt('testfile') >>> a array([[ 1., 2.], [ 3., 4.], [ 11., 12.], [ 13., 14.]]) >>> a. reshape((2, 2, 2)) array([[[ 1., 2.], [ 3., 4.]], [[ 11., 12.], [ 13., 14.]]])

What is a 3D array Python?

Introduction to NumPy 3D array. Arrays in NumPy are the data structures with high performance which are suitable for mathematical operations. The three levels of arrays nested inside one another represent the three-dimensional array in python, where each level represents one dimension.

Can you have a 3D array in Python?

Python numpy initialize 3d array In Python to initialize a 3-dimension array, we can easily use the np. array function for creating an array and once you will print the 'arr1' then the output will display a 3-dimensional array.