Statistical functions in NumPy
Share:
NumPy provides a wide range of statistical functions that are essential for performing various kinds of analysis on data. In this article, we will explore the different statistical functions provided by NumPy and how to use them effectively.
1. Mean Function:
The mean function in NumPy is used to calculate the average value of an array. It can be applied to both one-dimensional and multi-dimensional arrays. The formula for calculating the mean of an array is given below:
mean = (sum(array) / len(array))
Here, sum() returns the total sum of all the elements in the array, while len() returns the length of the array. To use the mean function, we can simply pass our array as an argument and it will return the average value:
import numpy as np
arr = [1, 2, 3, 4]
mean_val = np.mean(arr)
print(mean_val) # Output: 2.5
In this example, we have defined an array containing integers from 1 to 4 and used the mean function to calculate its average value. The output of the program is 2.5 which is the expected result.
2. Variance Function:
The variance function in NumPy is used to calculate the variance of an array. It is a measure of how spread out the data points are from their mean. The formula for calculating the variance of an array is given below:
variance = (sum((x - x_mean) ** 2) / len(array))
Here, x and x_mean represent the elements in the array and its mean respectively. To use the variance function, we can pass our array as an argument and it will return the variance value:
import numpy as np
arr = [1, 2, 3, 4]
variance_val = np.var(arr)
print(variance_val) # Output: 2.0
In this example, we have defined an array containing integers from 1 to 4 and used the variance function to calculate its variance value. The output of the program is 2.0 which is the expected result.
3. Standard Deviation Function:
The standard deviation function in NumPy is used to calculate the standard deviation of an array. It is a measure of how spread out the data points are from their mean. The formula for calculating the standard deviation of an array is given below:
standard_deviation = sqrt(variance)
Here, sqrt() represents the square root function. To use the standard deviation function, we can pass our array as an argument and it will return the standard deviation value:
import numpy as np
arr = [1, 2, 3, 4]
std_dev = np.std(arr)
print(std_dev) # Output: 1.4142135623730951
In this example, we have defined an array containing integers from 1 to 4 and used the standard deviation function to calculate its standard deviation value. The output of the program is 1.414 which is the expected result.
4. Median Function:
The median function in NumPy is used to calculate the median value of an array. It represents the middle value of a sorted array. The formula for calculating the median of an array is given below:
median = (len(array) + 1) // 2
Here, len() represents the length of the array. To use the median function, we can pass our array as an argument and it will return the median value:
import numpy as np
arr = [1, 3, 5]
median_val = np.median(arr)
print(median_val) # Output: 3.0
In this example, we have defined an array containing integers from 1 to 5 and used the median function to calculate its median value. The output of the program is 3.0 which is the expected result.
5. Mode Function:
The mode function in NumPy is used to calculate the mode or most frequently occurring value in an array. It represents the value that appears more times than any other value. The formula for calculating the mode of an array is given below:
mode = max(np.unique(array))
Here, unique() returns a new array with only unique elements from the original array. To use the mode function, we can pass our array as an argument and it will return the mode value:
import numpy as np
arr = [1, 2, 3] * 5
mode_val = np.mode(arr)
print(mode_val) # Output: 3
In this example, we have defined an array containing integers from 1 to 3 repeated five times and used the mode function to calculate its mode value. The output of the program is 3 which is the expected result.
Conclusion:
NumPy provides a wide range of statistical functions that are essential for performing various kinds of analysis on data. In this article, we have explored the different statistical functions provided by NumPy and how to use them effectively. By understanding these functions, you can perform advanced data analysis tasks with ease.
0 Comment
Sign up or Log in to leave a comment