find the average of all elements in an array in python

To find the average of all elements in an array, you can use the numpy library in python. The numpy.mean() function can be used to calculate the average of all elements in an array. Here's an example code snippet:

main.py
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
avg = np.mean(arr)

print(avg)
83 chars
7 lines

This will output:

main.py
3.0
4 chars
2 lines

In this example, we import the numpy library and create an array arr with five elements. We then use the np.mean() function to calculate the average of all elements in the array arr. Finally, we print the result which is 3.0.

related categories

gistlibby LogSnag