HOWTO · NumPy
NumPy mean() vs average()
This tutorial demonstrates the difference between numpy.mean() and numpy.average() functions in Python. Learn how to calculate averages effectively with clear examples and understand when to use each method for your data analysis needs.
On this page
For a plain arithmetic mean, numpy.mean() and numpy.average() return the same kind of unweighted result: each value contributes equally. The practical difference is that numpy.average() also accepts weights, so you can give some values more influence. Use np.mean() for an ordinary mean and np.average() when the calculation needs weights.
Understanding numpy.mean()
The numpy.mean() function computes the arithmetic mean of the values in an array. In the one-dimensional example below, it adds the five values and divides by five. The alias np.mean() is commonly used after importing NumPy as np.
With no weights supplied, every value contributes equally. This makes numpy.mean() a clear choice for a straightforward average, such as finding the mean of measurements where each measurement should count the same amount.
Exploring numpy.average()
numpy.average() can compute the same unweighted mean when called without weights. Its additional capability is a weighted mean: pass a weights array with a corresponding weight for each data value. A larger weight gives that value more influence. The weighted result is the sum of each value multiplied by its weight, divided by the sum of the weights.
The weights argument is useful when values represent different quantities or importance levels. For example, if one score represents a larger share of a final grade, assign it a proportionally larger weight. Make sure each weight corresponds to the value it describes.
Key Differences Between mean() and average()
This runnable example compares the unweighted and weighted calculations using the same values. Run it in a Python environment with NumPy installed.
import numpy as np
scores = np.array([10, 20, 30, 40, 50])
weights = np.array([1, 1, 1, 1, 5])
print(f"Unweighted mean: {np.mean(scores):g}")
print(f"Weighted average: {np.average(scores, weights=weights):g}")
Unweighted mean: 30
Weighted average: 38.8889
For the unweighted calculation, the five scores count equally. In the weighted calculation, the final score has five times the weight of each earlier score, so it has more influence on the result. The code uses the np.mean() and np.average() aliases; their full names are numpy.mean() and numpy.average().
Choose based on the calculation you need: use np.mean() when all values should count equally, and use np.average() when you need to supply weights. Without weights, these functions are interchangeable for the arithmetic mean shown here; that does not make their available behavior identical in every use.
Conclusion
Both functions calculate an unweighted arithmetic mean when used without weights. numpy.mean() is a direct choice for that task, while numpy.average() adds the option to calculate a weighted mean. Decide whether every value should count equally; if not, provide aligned weights to np.average().
FAQ
What is the main difference between numpy.mean() and numpy.average()?
Both can calculate an unweighted arithmetic mean. numpy.average() also accepts weights, while numpy.mean() does not provide that weighting option.
When should I use np.mean()?
Use np.mean() when each value should contribute equally to the arithmetic mean.
Can I use np.average() without weights?
Yes. Without a weights argument, np.average() calculates an unweighted mean, like np.mean() in this example.
How do weights change an average?
Each value’s influence depends on its corresponding weight. The weighted result is calculated by summing the value-weight products and dividing by the total weight.