Contents

## Numpy Array Average

Using Numpy, you can calculate average of elements of total Numpy Array, or along some axis, or you can also calculate weighted average of elements.

To find the average of an numpy array, you can use numpy.average() statistical function.

## Syntax

The syntax of average() function is as shown in the following.

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

We shall learn more about the parameters specified in the above syntax, with the help of following examples.

## Examples

### 1. Average of 2D NumPy Array

In this example, we take a (2, 2) array with numbers and find the average of this array using numpy.average() function.

**Python Program**

```
import numpy as np
arr = np.array([[4, 5], [3, 7]])
avg = np.average(arr)
print('array\n', arr)
print('average\n', avg)
```

Run Code Copy**Output**

```
array
[[4 5]
[3 7]]
average
4.75
```

### 2. Average of NumPy Array along an Axis

You can also find the average of an array along axis.

In this example, we will specify the axis of interest using **axis** parameter.

**Python Program**

```
import numpy as np
arr = np.array([[4, 5], [3, 7]])
avg = np.average(arr, axis=1)
print('array\n', arr)
print('average along axis=1\n', avg)
```

Run Code Copy**Output**

```
array
[[4 5]
[3 7]]
average along axis=1
[4.5 5. ]
```

### 3. Average of Numpy Array with Weights

You can also specify weights while calculating the average of elements in array. These weights will be multiplied with the elements and then the average of the resulting is calculated.

**Python Program**

```
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)
```

Run Code Copy**Output**

```
array
[[4 5]
[3 7]]
average along axis=1 with weights
[4.8 6.2]
```

## Summary

In this Numpy Tutorial, we learned how to calculate average of numpy array elements using numpy.average() function.