Find Mean of a List of NumPy Arrays in Python
Numpy is a very powerful python library for numerical data processing. It mostly takes in the data in form of arrays and applies various functions including statistical functions to get the result out of the array. In this article we will see how to get the mean value of a given array.
with mean
The mean function can take in an array and give the mathematical mean value of all the elements in it. So we design a for loop to keep track of the length of the input and go through each array calculating its mean.
Example
import numpy as np # GIven Array Arrays_In = [np.array([11, 5, 41]), np.array([12, 13, 26]), np.array([56, 20, 51])] # Resultihg Array Arrays_res = [] # With np.mean() for x in range(len(Arrays_In)): Arrays_res.append(np.mean(Arrays_In[x])) # Result print("The means of the arrays: \n",Arrays_res)
Output
Running the above code gives us the following result −
The means of the arrays: [19.0, 17.0, 42.333333333333336]
with Average
It is a very similar approach as above except that we use the average function instead of mean function. It gives the exact same result.
Example
import numpy as np # GIven Array Arrays_In = [np.array([11, 5, 41]), np.array([12, 13, 26]), np.array([56, 20, 51])] # Resultihg Array Arrays_res = [] # With np.average() for x in range(len(Arrays_In)): Arrays_res.append(np.average(Arrays_In[x])) # Result print("The means of the arrays: \n",Arrays_res)
Output
Running the above code gives us the following result −
The means of the arrays: [19.0, 17.0, 42.333333333333336]
Advertisements