02 Apr Statistical Operations on Numpy arrays
We can perform statistical operations, like mean, median, and standard deviation on NumPy arrays. Let’s see them one by one:
- Mean
- Median
- Standard Deviation
Mean
Mean is the average of the given values. Here, we will find the mean of the array elements using the mean() method.
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # mean resmean1 = np.mean(n1); resmean2 = np.mean(n2); print("\nArray1 mean = ", resmean1) print("Array2 mean = ", resmean2) |
Output
1 2 3 4 5 6 7 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 mean = 22.5 Array2 mean = 80.0 |
Median
Median is the middle value of the given values. Here, we will find the median of the array elements using the median() method.
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # median resmedian1 = np.median(n1); resmedian2 = np.median(n2); print("\nArray1 median = ", resmedian1) print("Array2 median = ", resmedian2) |
Output
1 2 3 4 5 6 7 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 median = 22.5 Array2 median = 80.0 |
Standard Deviation
Standard Deviation is a measure of the amount of variation or dispersion of the given values. Here, we will find the standard deviation of the array elements using the std() method.
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np # Create two arrays n1 = np.array([15, 20, 25, 30]) n2 = np.array([65, 75, 85, 95]) print("Array1 =", n1) print("Array2 =", n2) # standard deviation resstd1 = np.std(n1); resstd2 = np.std(n2); print("\nArray1 Standard Deviation = ", resstd1) print("Array2 Standard Deviation = ", resstd2) |
Output
1 2 3 4 5 6 7 |
Array1 = [15 20 25 30] Array2 = [65 75 85 95] Array1 Standard Deviation = 5.5901699437494745 Array2 Standard Deviation = 11.180339887498949 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments