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 the following examples:
- Get the mean of a ID array with NumPy
- Get the mean of a 2D array with NumPy
- Get the mean of a 2D array with NumPy (axis = 0)
- Get the mean of a 2D array with NumPy (axis = 1)
Let us see the examples:
Get the mean of a ID array with NumPy
# Calculate the mean of the elements of a ID array with NumPy import numpy as np n = np.array([7, 9, 11, 3]) print(np.mean(n))
Output
7.5
Get the mean of a 2D array with NumPy
# Calculate the mean of the elements of a 2D array with NumPy
import numpy as np
n = np.array([[7, 9, 20],
[11, 3, 8]])
print(np.mean(n))
Output
9.666666666666666
Get the mean of a 2D array with NumPy (axis = 0)
# Calculate the mean of the elements of a 2D array with NumPy
# axis = 0
import numpy as np
n = np.array([[7, 9, 20],
[11, 3, 8]])
# axis = 0 is vertical/down
# axis = 0 calculates totals for each column for sum/ mean effect
print(np.mean(n, axis = 0))
Output
[ 9. 6. 14.]
Get the mean of a 2D array with NumPy (axis = 1)
# Calculate the mean of the elements of a 2D array with NumPy
# axis = 1
import numpy as np
n = np.array([[7, 9, 20],
[11, 3, 8]])
# axis = 1 is horizontal/ across
# axis = 1 calculates totals for each row for sum/ mean effect
print(np.mean(n, axis = 1))
Output
[12. 7.33333333]
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 the following examples:
- Get the median of a ID array with NumPy
- Get the median of a 2D array with NumPy
- Get the median of a 2D array with NumPy (axis = 0)
- Get the median of a 2D array with NumPy (axis = 1)
Let us see the examples:
Get the median of a ID array with NumPy
# Calculate the median of the elements of a ID array with NumPy import numpy as np n1 = np.array([15, 20, 25, 30]) n2 = np.array([15, 20, 25, 30, 35]) print(np.median(n1)) print(np.median(n2))
Output
22.5 25.0
Get the median of a 2D array with NumPy
# Calculate the median of the elements of a 2D array with NumPy
import numpy as np
n = np.array([[15, 20, 25, 30, 40],
[45, 60, 85, 90, 135]])
print(np.median(n))
Output
42.5
Get the median of a 2D array with NumPy (axis = 0)
# Calculate the median of the elements of a 2D array with NumPy
# axis = 0
import numpy as np
n = np.array([[15, 20, 25, 30, 40],
[45, 60, 85, 90, 135],
[75, 77, 55, 50, 190],
[48, 70, 95, 66, 115],
])
# axis = 0 is vertical/down
# axis = 0 calculates totals for each column for sum/ mean/ median effect
print(np.median(n, axis = 0))
Output
[ 46.5 65. 70. 58. 125. ]
Get the median of a 2D array with NumPy (axis = 1)
# Calculate the median of the elements of a 2D array with NumPy
# axis = 1
import numpy as np
n = np.array([[15, 20, 25, 30, 40],
[45, 60, 85, 90, 135],
[75, 77, 55, 50, 190],
[48, 70, 95, 66, 115],
])
# axis = 1 is horizontal/ across
# axis = 1 calculates totals for each row for sum/ mean/ median effect
print(np.median(n, axis = 1))
Output
[25. 85. 75. 70.]
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:
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
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