
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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