Mean, Variance and Standard Deviation of values NumPy ndarray elements with example

Overview:

  • The mean() function of numpy.ndarray calculates and returns the mean value along a given axis.
  • The mean value is calculated based on the axis specified.
  • If no axis is specified, all the values of the n-dimensional array is considered while calculating the mean value.
  • For details of axis of n-dimensional arrays refer to the cumsum() and cumprod() section.
  • The numpy.ndarray also provides methods var(), std() methods that calculates the variance and standard deviation along any given axis of a ndarray object.

Example:

import numpy as np

 

# Create a 3 dimensional ndarray

nd_array = np.array([[[5,5,5,5],

                     [6,6,6,6],

                     [7,7,7,7]],

 

                    [[8,8,8,8],

                     [9,9,9,9],

                     [9,9,9,9]],

                   

                    [[10,10,10,10],

                     [11,11,11,11],

                     [12,12,12,12]]]

                 )

print("Input Array:")

print(nd_array)

 

print("Shape of the array:")

print(nd_array.shape)

 

print("Dimensions of the array:")

print(nd_array.ndim)

 

print("Mean of a numpy.ndarray object - No axis specified:")

print(nd_array.mean())

 

print("Mean of a numpy.ndarray object  - Along axis 0:")

print(nd_array.mean(axis=0))

 

print("Mean of a numpy.ndarray object  - Along axis 1:")

print(nd_array.mean(axis=1))

 

print("Mean of a numpy.ndarray object  - Along axis 2:")

print(nd_array.mean(axis=2))

 

print("Variance of a numpy.ndarray object - No axis specified:")

print(nd_array.var())

 

print("Variance of a numpy.ndarray object  - Along axis 0:")

print(nd_array.var(axis=0))

 

print("Variance of a numpy.ndarray object  - Along axis 1:")

print(nd_array.var(axis=1))

 

print("Variance of a numpy.ndarray object  - Along axis 2:")

print(nd_array.var(axis=2))

 

print("Standard deviation of a numpy.ndarray object - No axis specified:")

print(nd_array.std())

 

print("Standard deviation of a numpy.ndarray object - Along axis 0:")

print(nd_array.std(axis=0))

 

print("Standard deviation of a numpy.ndarray object - Along axis 1:")

print(nd_array.std(axis=1))

 

print("Standard deviation of a numpy.ndarray object - Along axis 2:")

print(nd_array.std(axis=2))

Output:

Input Array:

[[[ 5  5  5  5]

  [ 6  6  6  6]

  [ 7  7  7  7]]

 

 [[ 8  8  8  8]

  [ 9  9  9  9]

  [ 9  9  9  9]]

 

 [[10 10 10 10]

  [11 11 11 11]

  [12 12 12 12]]]

Shape of the array:

(3, 3, 4)

Dimensions of the array:

3

Mean of a numpy.ndarray object - No axis specified:

8.55555555556

Mean of a numpy.ndarray object  - Along axis 0:

[[ 7.66666667  7.66666667  7.66666667  7.66666667]

 [ 8.66666667  8.66666667  8.66666667  8.66666667]

 [ 9.33333333  9.33333333  9.33333333  9.33333333]]

Mean of a numpy.ndarray object  - Along axis 1:

[[  6.           6.           6.           6.        ]

 [  8.66666667   8.66666667   8.66666667   8.66666667]

 [ 11.          11.          11.          11.        ]]

Mean of a numpy.ndarray object  - Along axis 2:

[[  5.   6.   7.]

 [  8.   9.   9.]

 [ 10.  11.  12.]]

Variance of a numpy.ndarray object - No axis specified:

4.69135802469

Variance of a numpy.ndarray object  - Along axis 0:

[[ 4.22222222  4.22222222  4.22222222  4.22222222]

 [ 4.22222222  4.22222222  4.22222222  4.22222222]

 [ 4.22222222  4.22222222  4.22222222  4.22222222]]

Variance of a numpy.ndarray object  - Along axis 1:

[[ 0.66666667  0.66666667  0.66666667  0.66666667]

 [ 0.22222222  0.22222222  0.22222222  0.22222222]

 [ 0.66666667  0.66666667  0.66666667  0.66666667]]

Variance of a numpy.ndarray object  - Along axis 2:

[[ 0.  0.  0.]

 [ 0.  0.  0.]

 [ 0.  0.  0.]]

Standard deviation of a numpy.ndarray object - No axis specified:

2.16595429885

Standard deviation of a numpy.ndarray object - Along axis 0:

[[ 2.05480467  2.05480467  2.05480467  2.05480467]

 [ 2.05480467  2.05480467  2.05480467  2.05480467]

 [ 2.05480467  2.05480467  2.05480467  2.05480467]]

Standard deviation of a numpy.ndarray object - Along axis 1:

[[ 0.81649658  0.81649658  0.81649658  0.81649658]

 [ 0.47140452  0.47140452  0.47140452  0.47140452]

 [ 0.81649658  0.81649658  0.81649658  0.81649658]]

Standard deviation of a numpy.ndarray object - Along axis 2:

[[ 0.  0.  0.]

 [ 0.  0.  0.]

 [ 0.  0.  0.]]


Copyright 2024 © pythontic.com