Numpy Weighted Average np.average(array, axis=0, weights=[0.1,0.1,0.8])

How to Calculate the Weighted Average of a Numpy Array in Python?

You already know numpy’s average function, don’t you? This article goes a step further and shows you how to calculate the weighted average of a numpy array.

You can pass four arguments to the numpy average function:

  • The numpy array which can be multi-dimensional.
  • (Optional) The axis along which you want to average. If you don’t specify the argument, the averaging is done over the whole array.
  • (Optional) The weights of each column of the specified axis. If you don’t specify the argument, the weights are assumed to be homogeneous.
  • (Optional) The return value of the function. Only if you set this to True, you will get a tuple (average, weights_sum) as a result. This may help you to normalize the output. In most cases, you can skip this argument.

Here is an example how to average along the columns of a 2D numpy array with specified weights for both rows.

import numpy as np

# daily stock prices
# [morning, midday, evening]
solar_x = np.array(
[[2, 3, 4], # today
[2, 2, 5]]) # yesterday

# midday - weighted average
print(np.average(solar_x, axis=0, weights=[3/4, 1/4])[1])

What is the output of this puzzle?
*Beginner Level* (solution below)

You can also solve this puzzle in our puzzle-based learning app (100% FREE): Test your skills now!

Simple Explanation

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle introduces the average function from the numpy library. When applied to a 1D numpy array, this function returns the average of the array values. When applied to a 2D numpy array, it simply flattens the array. The result is the average of the flattened 1D array.

In the puzzle, we have a matrix with two rows and three columns. The matrix gives the stock prices of the solar_x stock. Each row represents the prices for one day. The first column specifies the morning price, the second the midday price, and the third the evening price.

Now suppose, we do not want to know the average of the flattened matrix but the average of the price in the midday. Moreover, we want to overweight the most recent stock price. Today accounts for three-quarters and yesterday for one-quarter of the final average value.

Numpy enables this via the weights parameter in combination with the axis parameter. The weights parameter defines the weight for each value participating in the average calculation. The axis parameter specifies the direction along which the average should be calculated. In a 2D matrix, the row is specified as axis=0 and the column as axis=1. We want to know three average values, for the morning, midday, and evening. We calculate the average along the row, i.e., axis=0. This results in three average values. Now we take the second element to get the midday variance.

Before you can master numpy, you need to master Python first. Subscribe to my free Python Email course. No spam, only valuable content: 6 Python Cheat Sheets, 20+ Python Lectures and learning on autopilot. It’s 100% Free.

Related Video



Leave a Comment