Compute the weighted average of a given NumPy array


Weighted average is a type of average in which each array element will be multiplied by a weight factor before calculating the mean of the data elements. The weight of each data point determines its contribution to all its overall average.

Calculating weighted average

This is used to calculate the average price of a stock in a portfolio value. The mathematical formula of the weighted average is given as follows.

weighted_average = (w1 * x1 + w2 * x2 + ... + wn * xn) / (w1 + w2 + ... + wn)

Where,

  • x1, x2, …..,xn are the given data points

  • w1, w2, ……, wn are the weighted averages to be multiplied to each data point respectively

  • n is the total number of elements

Weighted average of an Numpy array

In python, Numpy library provides average() function to calculate the weighted average of the given array elements.

Syntax

Following is the syntax for finding the weighted average of the given array elements -

numpy.average(array, weights = weights)

Where,

  • Array is the input array

  • weights are the weight value to be multiplied to the array elements before calculating the average.

Example

In order to find the weighted average of a given array, we have to pass the array and weights as the input arguments. Here, we are passing the elements and weights of a 2D array –

import numpy as np
a = np.array([[34,23],[90,34]])
weights = np.array([[2,3],[5,7]])
print("The input array:",a)
print("The dimension of the array:",np.ndim(a))
avg = np.average(a)
print("The average of the given 2-d array:",avg)
weg = np.average(a,weights = weights)
print("weighted average of the array:",weg)

Output

The input array: [[34 23]
[90 34]]
The dimension of the array: 2
The average of the given 2-d array: 45.25
weighted average of the array: 48.529411764705884

Example

In the following example we are trying to calculate the weighted average of the 1D array –

import numpy as np
a = np.array([3,4,2,3,90,34])
weights = np.array([2,3,1,5,7,6])
print("The input array:",a)
print("The dimension of the array:",np.ndim(a))
avg = np.average(a)
print("The average of the given 1-d array:",avg)
weg = np.average(a,weights = weights)
print("weighted average of the array:",weg) 

Output

The input array: [ 3 4 2 3 90 34]
The dimension of the array: 1
The average of the given 1-d array: 22.666666666666668
weighted average of the array: 36.208333333333336

Example

In this example we are calculating the weighted average of the 3-d array using the average() function –

import numpy as np
a = np.array([[[3,4],[2,3]],[[90,34],[78,23]]])
weights = np.array([[[3,4],[2,3]],[[90,34],[78,23]]])
print("The input array:",a)
print("The dimension of the array:",np.ndim(a))
avg = np.average(a)
print("The average of the given 3-d array:",avg)
weg = np.average(a,weights = weights)
print("weighted average of the array:",weg)

Output

The input array: [[[ 3 4]
[ 2 3]]
[[90 34]
[78 23]]]
The dimension of the array: 3
The average of the given 3-d array: 29.625
weighted average of the array: 67.11814345991561

Updated on: 09-Aug-2023

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements