python-scipyHow do I calculate the average of a numpy array in Python?
To calculate the average of a numpy array in Python, you can use the mean()
function. This function takes in an array as an argument and returns the average of all the values in the array. The following example code shows how to use the mean()
function to calculate the average of a numpy array.
import numpy as np
# Create a numpy array
a = np.array([1, 2, 3, 4, 5])
# Calculate the average
average = np.mean(a)
# Print the average
print(average)
# Output
3.0
The code above consists of the following parts:
- Importing the numpy library as
np
- This allows us to access themean()
function. - Creating a numpy array - This is the array whose average we want to calculate.
- Calculating the average - This is done by calling the
mean()
function and passing in the array as an argument. - Printing the average - This prints the average of the array.
For more information, see the Numpy Documentation.
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python and Numpy to zip files?
- How to use Python, XML-RPC, and NumPy together?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Scipy zeros in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use Python Scipy to perform a Z test?
- How do I create a zero matrix using Python and Numpy?
See more codes...