NumPy - Descriptive Statistics
Descriptive Statistics in NumPy
Descriptive statistics in NumPy refers to summarizing and understanding the main features of a dataset through various statistical measures. It includes operations like calculating the mean (average), median, standard deviation, variance, and percentiles.
NumPy provides functions like numpy.mean(), numpy.median(), numpy.std(), and numpy.percentile() to quickly calculate these statistics, helping you understand the central tendency, spread, and distribution of the data.
The NumPy mean() Function
The numpy.mean() function calculates the arithmetic mean of the elements along the specified axis. If no axis is specified, it computes the mean of the flattened array.
The mean is a measure of central tendency, representing the average of all the values in the dataset.
Example: Calculating the Mean
In the following example, we are calculating the mean of an array of numbers using the numpy.mean() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the mean of the array
mean_value = np.mean(data)
print(f"Mean of the array: {mean_value}")
Following is the output obtained −
Mean of the array: 3.0
The NumPy median() Function
The numpy.median() function computes the median of the elements along the specified axis. If no axis is specified, it computes the median of the flattened array.
The median is the middle value in a sorted dataset and is useful when dealing with skewed distributions.
Example: Calculating the Median
In the following example, we are calculating the median of an array using the numpy.median() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the median of the array
median_value = np.median(data)
print(f"Median of the array: {median_value}")
This will produce the following result −
Median of the array: 3.0
Finding the Mode of a Dataset
NumPy does not have a direct function to compute the mode. However, you can use the scipy.stats.mode() function from the SciPy library to calculate the mode. The mode represents the most frequent value in a dataset.
Example: Calculating the Mode
In this example, we are using the scipy.stats.mode() function to find the mode of the array −
import numpy as np
from scipy import stats
data = np.array([1, 2, 3, 4, 5])
# Calculate the mode of the array
mode_value = stats.mode(data)
print(f"Mode of the array: {mode_value.mode[0]}")
Following is the output of the above code −
/home/cg/root/6745741fe1e0a/main.py:6: FutureWarning: Unlike other reduction functions (e.g. 'skew', 'kurtosis'), the default behavior of 'mode' typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of 'keepdims' will become False, the 'axis' over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set 'keepdims' to True or False to avoid this warning. mode_value = stats.mode(data) Mode of the array: 1
The NumPy var() Function
The numpy.var() function calculates the variance of the elements along the specified axis. Variance measures the spread of the data points.
Variance indicates how far the data points are from the mean, providing a measure of the data's dispersion.
Example: Calculating the Variance
In the example below, we are calculating the variance of an array using the numpy.var() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the variance of the array
variance_value = np.var(data)
print(f"Variance of the array: {variance_value}")
The output obtained is as shown below −
Variance of the array: 2.0
The NumPy std() Function
The numpy.std() function computes the standard deviation of the elements along the specified axis. Standard deviation is the square root of the variance and provides a measure of the dispersion of the data points.
Example: Calculating the Standard Deviation
In this example, we are calculating the standard deviation of an array using the numpy.std() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the standard deviation of the array
std_value = np.std(data)
print(f"Standard Deviation of the array: {std_value}")
After executing the above code, we get the following output −
Standard Deviation of the array: 1.4142135623730951
Finding the Minimum and Maximum Values
The numpy.min() and numpy.max() functions return the minimum and maximum values in the array, respectively. The minimum value is the smallest data point, and the maximum value is the largest data point in the array.
Example: Finding the Minimum and Maximum Values
In the following example, we are calculating the minimum and maximum values of an array using the numpy.min() and numpy.max() functions −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the minimum and maximum of the array
min_value = np.min(data)
max_value = np.max(data)
print(f"Minimum of the array: {min_value}")
print(f"Maximum of the array: {max_value}")
The result produced is as follows −
Minimum of the array: 1 Maximum of the array: 5
Calculating the Range of the Dataset
The range of a dataset is the difference between the maximum and minimum values. You can calculate it using numpy.ptp() function. The range gives an indication of how spread out the values are in the dataset.
Example: Calculating the Range
In this example, we are calculating the range of the array using the numpy.ptp() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the range of the array
range_value = np.ptp(data)
print(f"Range of the array: {range_value}")
We get the output as shown below −
Range of the array: 4
Calculating Percentiles
The numpy.percentile() function computes the q-th percentile of the data along the specified axis. Percentiles divide the dataset into 100 equal parts, helping us understand the distribution of the data.
Example
In the following example, we are calculating the 25th, 50th (median), and 75th percentiles of an array using the numpy.percentile() function −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the 25th, 50th, and 75th percentiles
percentile_25 = np.percentile(data, 25)
percentile_50 = np.percentile(data, 50)
percentile_75 = np.percentile(data, 75)
print(f"25th percentile: {percentile_25}")
print(f"50th percentile (median): {percentile_50}")
print(f"75th percentile: {percentile_75}")
The results are:
25th percentile: 2.0 50th percentile (median): 3.0 75th percentile: 4.0
Interquartile Range (IQR) Calculation
The Interquartile Range (IQR) is the range between the 75th percentile and the 25th percentile. It measures the spread of the middle 50% of the data. The IQR is a useful measure to understand the variability within the central 50% of the data.
Example: Calculating the Interquartile Range (IQR)
In the following example, we are calculating the Interquartile Range (IQR) of an array by subtracting the 25th percentile from the 75th percentile −
import numpy as np
# Define an array
data = np.array([1, 2, 3, 4, 5])
# Calculate the interquartile range
iqr_value = np.percentile(data, 75) - np.percentile(data, 25)
print(f"Interquartile Range (IQR): {iqr_value}")
Following is the output obtained −
Interquartile Range (IQR): 2.0