Because all of the errors have the same relative weight. Supplying a weight parameter does not change the actual values you are averaging, it just indicates the weight with which each value value contributes to the average. In other words, after multiplying each value passed by its corresponding weight, np.average divides by the sum of the weights provided.

>>> import numpy as np
>>> np.average([1, 2, 3], weights=[0.2, 0.2, 0.2])
2.0
>>> np.average([1, 2, 3])
2.0

Effectively, the average formula for an n-dimensional array-like container is

                               

where each weight is assumed to be equal to 1 when not provided to numpy.average.

Answer from miradulo on Stack Overflow
Top answer
1 of 3
43

Because all of the errors have the same relative weight. Supplying a weight parameter does not change the actual values you are averaging, it just indicates the weight with which each value value contributes to the average. In other words, after multiplying each value passed by its corresponding weight, np.average divides by the sum of the weights provided.

>>> import numpy as np
>>> np.average([1, 2, 3], weights=[0.2, 0.2, 0.2])
2.0
>>> np.average([1, 2, 3])
2.0

Effectively, the average formula for an n-dimensional array-like container is

                               

where each weight is assumed to be equal to 1 when not provided to numpy.average.

2 of 3
4

My answer is late, but I hope this will be of use to others looking at this post in the future.

The above answers are spot on with respect to why the results are the same. However, there is a fundamental flaw in how you are calculating your weighted average. The uncertainties in your data ARE NOT the weights that numpy.average expects. You have to calculate your weights first and provide them to numpy.average. This can be done as:

weight = 1/(uncertainty)^2.

(see, for example, this description.)

Therefore, you would calculate your weighted average as:

wts_2e13 = 1./(np.power(bias_error_2e13, 2.)) # Calculate weights using errors

wts_half = 1./(np.power(error_half, 2.)) # Calculate weights using half errors

test = np.average(bias_2e13, weights = wts_2e13)

test_2 = np.average(bias_2e13, weights = wts_half)

giving you the answers of 2.2201767077906709 in both cases for reasons explained well in the above answers.

๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ understanding-weighted-average-with-numpy-cfb245fced2a
Understanding Weighted Average with NumPy | by why amit | Medium
February 9, 2025 - The weighted average gives more priority to the scores with higher weights. ... The numerator multiplies values by their weights and adds them up. The denominator sums all the weights.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v2.5 Manual
Each value in a contributes to the average according to its associated weight. The array of weights must be the same shape as a if no axis is specified, otherwise the weights must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to calculate the weighted average of a numpy array in python?
How to Calculate the Weighted Average of a Numpy Array in Python? - Be on the Right Side of Change
January 9, 2023 - The weighted average is the sum of all array elements, properly weighted, divided by the sum of all weights. ... Quick solution: Before we discuss the solution in great detail, hereโ€™s the solution that solves this exact problem: import numpy ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v2.3 Manual
Each value in a contributes to the average according to its associated weight. The array of weights must be the same shape as a if no axis is specified, otherwise the weights must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ average()
Python Numpy average() - Compute Mean Value
November 11, 2024 - Compute the weighted average using the average() function. ... import numpy as np data = np.array([10, 20, 30, 40, 50]) weights = np.array([1, 2, 3, 4, 5]) weighted_mean = np.average(data, weights=weights) print(weighted_mean)
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ average
NumPy average()
The numpy.average() method computes the weighted average along the specified axis. import numpy as np # create an array array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v2.2 Manual
Each value in a contributes to the average according to its associated weight. The array of weights must be the same shape as a if no axis is specified, otherwise the weights must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one.
๐ŸŒ
Omz Software
omz-software.com โ€บ pythonista โ€บ numpy โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v1.8 Manual
Compute the weighted average along the specified axis. ... >>> data = range(1,5) >>> data [1, 2, 3, 4] >>> np.average(data) 2.5 >>> np.average(range(1,11), weights=range(10,0,-1)) 4.0
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ compute-the-weighted-average-of-a-given-numpy-array
Compute the weighted average of a given NumPy array | GeeksforGeeks
August 29, 2020 - In NumPy, we can compute the weighted of a given array by two approaches first approaches is with the help of numpy.average() function in which we pass the weight array in the parameter.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v2.6.dev0 Manual
Each value in a contributes to the average according to its associated weight. The array of weights must be the same shape as a if no axis is specified, otherwise the weights must have dimensions and shape consistent with a along the specified axis. If weights=None, then all data in a are assumed to have a weight equal to one.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-numpy-average
Average of NumPy Array - Examples
The weights are multiplied with the elements, and their sum is divided by the total of the weights. import numpy as np arr = np.array([[4, 5], [3, 7]]) avg = np.average(arr, axis=1, weights=[0.2, 0.8]) print('array\n', arr) print('average along axis=1 with weights\n', avg) array [[4 5] [3 7]] ...
๐ŸŒ
DEV Community
dev.to โ€บ chrisgreening โ€บ calculating-weighted-averages-with-numpy-and-python-4m79
Calculating weighted averages with numpy and Python! - DEV Community
August 24, 2023 - To get the weighted average across the entire university using numpy all we have to do is incorporate the weights into the np.average:
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-stat-exercise-6.php
NumPy: Compute the weighted of a given array - w3resource
# Importing the NumPy library import numpy as np # Creating an array 'x' using arange with 5 elements x = np.arange(5) # Displaying the original array 'x' print("\nOriginal array:") print(x) # Creating weights from 1 to 5 using arange weights = np.arange(1, 6) # Calculating the weighted average of the array 'x' using np.average() and 'weights' r1 = np.average(x, weights=weights) # Calculating the weighted average manually r2 = (x * (weights / weights.sum())).sum() # Asserting if the results from np.average() and manual calculation are close assert np.allclose(r1, r2) # Displaying the calculated weighted average of the array 'x' print("\nWeighted average of the said array:") print(r1)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ compute-the-weighted-average-of-a-given-numpy-array
NumPy average() Function
August 9, 2023 - The NumPy average() function computes the weighted average or mean of the elements in an array along a specified axis. The weighted average allows for each element to have its own weight, which can modify the contribution of each element to ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v2.1 Manual
If axis is a tuple of ints, averaging is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before. ... An array of weights associated with the values in a. Each value in a contributes to the average according to its associated weight.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.21 โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v1.21 Manual
If axis is a tuple of ints, averaging is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before. ... An array of weights associated with the values in a. Each value in a contributes to the average according to its associated weight.
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.10.1 โ€บ reference โ€บ generated โ€บ numpy.average.html
numpy.average โ€” NumPy v1.10 Manual
October 18, 2015 - Compute the weighted average along the specified axis. ... >>> data = range(1,5) >>> data [1, 2, 3, 4] >>> np.average(data) 2.5 >>> np.average(range(1,11), weights=range(10,0,-1)) 4.0