# Python NumPy Average with Examples

In this Python tutorial, we will discuss Python NumPy Average and also cover the below examples:

Python numpy average vs mean
Python numpy average of columns
Python numpy average 2d array
Python numpy average function
Python numpy average ignore nan
Python numpy average of matrix
Python numpy moving average filter
Python numpy average value

## Python numpy average

In this section, we will discuss Python numpy average.
To find the average of an numpy array, you can use numpy.average() function.
The numpy module of Python provides a function called numpyaverage(), used for calculating the weighted average along the specified axis.

Syntax:

Here is the syntax of the NumPy average

numpy.average
(
arr,
axis=None,
Weights=None,
returned=False
)

It consists of few parameters

arr: Array containing data to be averaged.

Axis: Axis or axes along which to average a. The default, axis=None, will average over all of the elements of the input array. If axis is negative it counts from the last to the first axis.

Weights: It is an optional parameter. Each value in a contributes to the average according to its associated weight.

Returns: Return the average along the specified axis. When returned is True, return a tuple with the average as the first element and the sum of the weights as the second element.

Example:

Let’s take an example to check how to calculate numpy average in python.

import numpy as np

arr = np.arange(1, 5)
avg = np.average(arr)
print(avg)

In the above example first, we will import a NumPy library and create an array by using the function np.arange.

Here is the Screenshot of the following given code

Python numpy average

## Python numpy average vs mean

In this section, we will discuss Python numpy average vs mean.
Both these functions can be used to calculate the arithmetic mean or average.
np.mean() function can have many other parameters like dtype, out, where and more which are not availabe in the np.average() function.

np.average can compute a weighted average if the weights parameter is supplied.
np.average do not take in account masks, so compute the average over the whole set of data. While in case of mean takes in account masks, so compute the mean only over unmasked values.

Example:

import numpy as np

arr = np.arange(1, 5)
avg = np.average(arr)
mea = np.mean(arr)
print(avg)
print(mea)

Here is the Screenshot of the following given code

Python numpy average vs mean

## Python numpy average of columns

In this section, we will discuss the Python numpy average of columns.

average() to calculate mean values across dimensions in an array.
It will return the average of an array elements along the given axis.
x as 0 and then 1 to calculate the average value of each column and then row in numpy.

Syntax:

numpy.average
(
arr,
axis=None,
Weights=None,
returned=False
)

Example:

import numpy as np

arr = np.array([[2,3,4],
[3,6,7],
[5,7,8]])
a= np.average(arr,axis=0)
print(a)

Here is the Screenshot of the following given code

Python numpy average of columns

## Python numpy average 2d array

In this section, we will discuss the Python numpy average 2d array.
To calculate the average of all values in a two-dimensional NumPy array called matrix, use the np.average(matrix) function.
The resulting array has three average values, one per column of the input matrix .

Syntax:

Here is the Syntax of the numpy average

numpy.average
(
arr,
axis=None,
Weights=None,
returned=False
)

Example:

import numpy as np

arr = np.array([[4,5,6],
[4,6,7]])# 2D array

a= np.average(arr)
print(a)

Here is the Screenshot of the following given code

Python numpy average 2d array

## Python numpy average function

In this section, we will discuss the Python numpy average function

The numpy module of Python provides a function called numpy.average(), used for calculating the weighted average along the specified axis.
To find the average of an numpy array, you can use numpy.average() statistical function.
These weights will be multiplied with the elements and then the average of the resulting is calculated.

Syntax:

Here is the Syntax of the NumPy average function

numpy.average
(
arr,
axis=None,
Weights=None,
returned=False
)

Example:

import numpy as np

c = np.array([2, 3, 4, 7]).reshape(2,2)
d = np.average(c, axis=0, weights=[0.3,0.7])# average along axis=0
print(d)

Here is the Screenshot of the following given code

Python numpy average function

Read Python NumPy log

## Python numpy average ignore nan

In this section, we will discuss the Python numpy average ignore nan.
If array have NaN value and we can find out the mean without effect of NaN value. axis: we can use axis=1 means row wise or axis=0 means column wise.
In this method we will calculate our weighted average and create a masked array.
np.average do take in account masks, so compute the average over the whole set of data. While in case of mean takes in account masks, so compute the mean only over unmasked values.

Example:

import numpy as np

avg = np.array([[4,5,6], [7,8,np.NaN], [np.NaN,6,np.NaN], [0,0,0]])
data = np.ma.masked_array(avg, np.isnan(avg))
weights = [1, 1, 1]
average = np.ma.average(data, axis=1, weights=weights)
result = average.filled(np.nan)
print(result)

Here is the Screenshot of the following given code

Python numpy average ignore nan

## Python numpy average of matrix

In this section, we will discuss the Python numpy average matrix.
To calculate the average separately for each column of the 2D matrix, use the function call np. average(matrix, axis=0) setting the axis argument to 0.
It will return average value of matrix.
In this method we can easily use the function numpy.average().

Example:

import numpy as np

x = np.matrix(np.arange(12).reshape((3, 4)))
y = np.average(x)
print(y)

Here is the Screenshot of the following given code

Python numpy average of matrix

## Python numpy average filter

In this section, we will discuss the Python numpy moving average filter.
Weighted moving average puts more emphasis on the recent data than the older data.
Moving average is frequently used in studying time-series data by calculating the mean of the data at specific intervals.
In this method we can use the function numpy.convolve to calculate the moving average for numpy arrays.
The convolve() function is used in signal processing and can return the linear convolution of two arrays.

Example:

import numpy as np

def moving_average(x, w):
return np.convolve(x, np.ones(w), ‘valid’) / w

data = np.array([2,3,8,4,6,7,8,11,14,17,9,7])

print(moving_average(data,4))

Here is the Screenshot of the following given code

Python numpy average filter

Another method to calculate the moving average for NumPy arrays using scipy.convolve() function.

The scipy.convolve() function in the same way. It is assumed to be a little faster. Another way of calculating the moving average using the NumPy module is with the cumsum() function.

Example:

import numpy as np

def average(a, n) :
ret = np.cumsum(a, dtype=float)
ret[n:] = ret[n:] – ret[:-n]
return ret[n – 1:] / n

data = np.array([5,4,3,2,10,11,13,14,15,16,19,7])

print(average(data,4))

Here is the Screenshot of the following given code

Python numpy average moving filter

Another method to calculate the moving average for NumPy arrays using a bottleneck.

The bottleneck module is a compilation of quick NumPy methods. This module has the move_mean() function, which can return the moving average of some data.

Example:

import numpy as np
import bottleneck as bn
import numpy as np
def rollavg_bottlneck(a,n):
return bn.move_mean(a, window=n,min_count = None)

data = np.array([10,5,8,9,15,22,26,11,15,16,18,7])

print(rollavg_bottlneck(data, 4))

Here is the Screenshot of the following given code

Python NumPy moving average bottleneck

## Python numpy average value

In this section, we will discuss the Python numpy average value.
In this method we can easily use the function numpy.average() to calculate the average value of a given array.
This function returns the arithmetic average value of elements in the array.
By default, the average is taken on the flattened array. Else on the specified axis, float 64 is intermediate as well as return values are used for integer inputs.

Syntax:

Here is the Syntax of the NumPy average

numpy.average
(
arr,
axis=None,
Weights=None,
returned=False
)

Example:

import numpy as np

arr = np.array([[1,2,4],
[3,4,7],
[5,7,8]])
a= np.average(arr,axis=0)
print(a)

Here is the Screenshot of the following given code

Python numpy average value

You may like the following Python NumPy articles:

Python sort NumPy array

In this Python tutorial, we will discuss Python NumPy Average and also cover the below examples:

Python numpy average vs mean
Python numpy average of columns
Python numpy average 2d array
Python numpy average function
Python numpy average ignore nan
Python numpy average of matrix
Python numpy moving average filter
Python numpy average value