In this Python article, I will explain the **NumPy average function in Python**, its syntax, the parameters required, and the return value. I will also explain the difference between the **np.average and np.mean() function**.

**To calculate the mean of elements in an array, the NumPy average function in Python is highly versatile. It not only computes simple averages but also weighted averages, where different elements carry distinct weights. Additionally, it skillfully handles multidimensional arrays, allowing averaging across specified axes, making it a vital tool in data analysis and scientific computing.**

## NumPy average function in Python

The **np.average()** function in Python is a part of the NumPy library, a fundamental package for scientific computing. This function calculates the average of the elements in an array.

It offers more flexibility than the standard mean calculation, as it can compute a weighted average if weights are provided.

### Syntax and parameter required in the np.average() function

The basic syntax of the NumPy average function in Python is:

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

Here,

arr | The Python array that contains the data. |

axis | The axis along which to average the array. The default is None, averaging all the data. |

weights | An array of weights associated with the elements of a. By default, all data have equal weight. |

returned | If True, returns a tuple of the average and the sum of the weights. Useful for weighted averages. |

## np.average() in Python use cases

There can be many different use cases of the **np.average()** in Python.

### 1. NumPy array average in Python

The simplest use of Python **np.average()** is to find the mean of an array.

```
import numpy as np
temperatures = np.array([70, 72, 68, 65, 67, 69, 71])
average_temp = np.average(temperatures)
print(f"Average Temperature: {average_temp} °F")
```

**Output:**

`Average Temperature: 68.85714285714286 °F`

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

### 2. Weighted Average of NumPy array in Python

One of the key features of **np.average() in Python** is its ability to compute weighted averages.

```
import numpy as np
scores = np.array([85, 90, 95, 80])
weights = np.array([0.2, 0.25, 0.35, 0.2])
weighted_average = np.average(scores, weights=weights)
print(f"Weighted Average Score: {weighted_average}")
```

**Output:**

`Weighted Average Score: 88.75`

After executing the code in Pycharm, one can see the output in the below screenshot.

### 3. Average of 2d array in Python NumPy

We can find the average of all the data in the 2D array using the **np.average()** function in Python.

```
import numpy as np
Salaries = np.array([[41000, 51000, 31000],
[71000, 50700, 20800]])
monthly_avg_Salaries = np.average(Salaries)
print(f"Average Monthly Salaries offered by the company: {monthly_avg_Salaries}")
```

**Output:**

`Average Monthly Salaries offered by the company: 44250.0`

After executing the code in Pycharm, one can see the output in the below screenshot.

### 4. np.average in Python with different axes

The **np.average()** Python NumPy function can operate along specified axes in a multidimensional array.

**Case 1:** find the average of rows in a 2D array through the NumPy average function in Python. Here, we will use the axis=0 parameter.

```
import numpy as np
sales = np.array([[12000, 15000, 13000], # East region sales
[10000, 11000, 9000]]) # West region sales
monthly_avg_sales = np.average(sales, axis=0)
print(f"Average Monthly Sales (East, West): {monthly_avg_sales}")
```

**Output:**

`Average Monthly Sales (East, West): [11000. 13000. 11000.]`

After executing the code in Pycharm, one can see the output in the below screenshot.

**Case 2:** find the NumPy average columns in a 2D array through the axis=1 parameter within the NumPy average function in Python.

```
import numpy as np
sales = np.array([[14000, 15000, 13000], # East region sales
[10000, 11000, 9000]]) # West region sales
region_avg_sales = np.average(sales, axis=1)
print(f"Average Sales per Region: {region_avg_sales}")
```

**Output:**

`Average Sales per Region: [14000. 10000.]`

After executing the code in Pycharm, one can see the output in the below screenshot.

## np.average vs np.mean in Python

While **np.mean and np.average** can compute averages, the key difference is that **np.average() in Python** can compute weighted averages, whereas **np.mean() in Python** always treats elements equally.

```
import numpy as np
incomes = np.array([50000, 60000, 55000, 70000, 65000])
mean_income = np.mean(incomes)
average_income = np.average(incomes)
print(f"Mean Income: {mean_income}")
print(f"Average Income (using np.average): {average_income}")
```

**Output:**

```
Mean Income: 60000.0
Average Income (using np.average): 60000.0
```

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

## Conclusion

Understanding the **NumPy average function in Python** with its syntax, parameters required, and different use cases can help one to find the mean of the values easily. And, also knowing the basic difference between the **np.average and np.mean** functions can help one to utilize them wisely.

You may also like to read:

- np.abs() in Python
- NumPy average filter in Python
- Python repeat array n times
- NumPy unique function in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.