
- 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 - Statistical Functions
Statistical Functions in NumPy
NumPy offers a wide range of statistical functions that allow you to perform various statistical calculations on arrays. These functions can calculate metrics such as mean, median, variance, standard deviation, minimum, maximum, and more.
The NumPy amin() and amax() Functions
The numpy.amin() function returns the minimum from the elements in the given array along the specified axis. Whereas, the numpy.amax() function returns the maximum from the elements in the given array along the specified axis.
Example
In the following example, we are demonstrating how to use the amin() and amax() functions on a NumPy array −
import numpy as np a = np.array([[3,7,5],[8,4,3],[2,4,9]]) print('Our array is:') print(a) print('\n') print('Applying amin() function:') print(np.amin(a,1)) print('\n') print('Applying amin() function again:') print(np.amin(a,0)) print('\n') print('Applying amax() function:') print(np.amax(a)) print('\n') print('Applying amax() function again:') print(np.amax(a, axis=0))
It will produce the following output −
Our array is: [[3 7 5] [8 4 3] [2 4 9]] Applying amin() function:[3 3 2] Applying amin() function again:[2 4 3] Applying amax() function:9 Applying amax() function again:[8 7 9]
The numpy.ptp() Function
The numpy.ptp() function returns the range (maximum - minimum) of values along an axis.
Example
In the following example, we are using the ptp() function to calculate the range of values in a NumPy array −
import numpy as np a = np.array([[3,7,5],[8,4,3],[2,4,9]]) print('Our array is:') print(a) print('\n') print('Applying ptp() function:') print(np.ptp(a)) print('\n') print('Applying ptp() function along axis 1:') print(np.ptp(a, axis=1)) print('\n') print('Applying ptp() function along axis 0:') print(np.ptp(a, axis=0))
Following is the output obtained −
Our array is: [[3 7 5] [8 4 3] [2 4 9]] Applying ptp() function:7 Applying ptp() function along axis 1:[4 5 7] Applying ptp() function along axis 0:[6 3 6]
The numpy.percentile() Function
Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall.
The numpy.percentile() function computes the q-th percentile of the data along the specified axis. It takes the following arguments −
numpy.percentile(a, q, axis)
Where,
- a: It is the input array.
- q: It is the percentile to compute and it must be between 0-100.
- axis: It is the axis along which the percentile is to be calculated.
Example
In this example, we are calculating the 50th percentile (median) of a NumPy array using the percentile() function −
import numpy as np a = np.array([[30,40,70],[80,20,10],[50,90,60]]) print('Our array is:') print(a) print('\n') print('Applying percentile() function:') print(np.percentile(a,50)) print('\n') print('Applying percentile() function along axis 1:') print(np.percentile(a,50, axis=1)) print('\n') print('Applying percentile() function along axis 0:') print(np.percentile(a,50, axis=0))
This will produce the following result −
Our array is: [[30 40 70] [80 20 10] [50 90 60]] Applying percentile() function:50.0 Applying percentile() function along axis 1:[40. 20. 60.] Applying percentile() function along axis 0:[50. 40. 60.]
Sum and Product of Array Elements
The numpy.sum() function calculates the sum of all elements in the array, while the numpy.prod() function calculates the product of all elements in the array.
Example
In the following example, we are calculating the sum and product of the elements in a NumPy array using the sum() and prod() functions −
import numpy as np # Create a NumPy array data = np.array([1, 2, 3, 4]) # Calculate the sum of the array sum_value = np.sum(data) # Calculate the product of the array prod_value = np.prod(data) print(f"Sum of the array: {sum_value}") print(f"Product of the array: {prod_value}")
It will produce the following output −
Sum of the array: 10 Product of the array: 24
The numpy.median() Function
The numpy.median() function computes the median along the specified axis. If no axis is specified, it computes the median of the flattened array. Median is defined as the value separating the higher half of a data sample from the lower half
Example
In the following example, we are using the median() function to find the median of elements in a NumPy array −
import numpy as np a = np.array([[30,65,70],[80,95,10],[50,90,60]]) print('Our array is:') print(a) print('\n') print('Applying median() function:') print(np.median(a)) print('\n') print('Applying median() function along axis 0:') print(np.median(a, axis=0)) print('\n') print('Applying median() function along axis 1:') print(np.median(a, axis=1))
Following is the output of the above code −
Our array is: [[30 65 70] [80 95 10] [50 90 60]] Applying median() function:65.0 Applying median() function along axis 0:[50. 90. 60.] Applying median() function along axis 1:[65. 80. 60.]
The numpy.mean() Function
The numpy.mean() function returns the arithmetic mean of elements in the array. If no axis is specified, it computes the mean of the flattened array. Arithmetic mean is the sum of elements along an axis divided by the number of elements.
Example
In the following example, we are calculating the mean of elements in a NumPy array using the mean() function −
import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5,6]]) print('Our array is:') print(a) print('\n') print('Applying mean() function:') print(np.mean(a)) print('\n') print('Applying mean() function along axis 0:') print(np.mean(a, axis=0)) print('\n') print('Applying mean() function along axis 1:') print(np.mean(a, axis=1))
The output obtained is as shown below −
Our array is: [[1 2 3] [3 4 5] [4 5 6]] Applying mean() function:3.6666666666666665 Applying mean() function along axis 0:[2.66666667 3.66666667 4.66666667] Applying mean() function along axis 1:[2. 4. 5.]
The numpy.average() Function
The numpy.average() function computes the weighted average of elements in an array according to their respective weight. Weighted average is an average resulting from the multiplication of each component by a factor.
Example
In the example below, we are calculating the average of elements in a NumPy array using the average() function with and without weights −
import numpy as np a = np.array([1,2,3,4]) print('Our array is:') print(a) print('\n') print('Applying average() function without weights:') print(np.average(a)) print('\n') wts = np.array([4,3,2,1]) print('Applying average() function with weights:') print(np.average(a,weights=wts)) print('\n') print('Sum of weights') print(np.average([1,2,3,4],weights=[4,3,2,1], returned=True))
It will produce the following output −
Our array is:[1 2 3 4] Applying average() function without weights:2.5 Applying average() function with weights:2.0 Sum of weights(2.0, 10.0)
The numpy.std() Function
The numpy.std() function returns the standard deviation of elements in the array. The standard deviation is the square root of the average of squared deviations from the mean, i.e., std = sqrt(mean(abs(x - x.mean())**2)).
Example
In the following example, we are using the std() function to calculate the standard deviation of a NumPy array −
import numpy as np print(np.std([1,2,3,4]))
After executing the above code, we get the following output −
1.118033988749895
The numpy.var() Function
The numpy.var() function returns the variance of elements in the array. The variance is the average of squared deviations, i.e., var = mean(abs(x - x.mean())**2).
Example
In the following example, we are using the var() function to calculate the variance of a NumPy array −
import numpy as np print(np.var([1,2,3,4]))
We get the output as shown below −
1.25
Correlation Coefficient
The numpy.corrcoef() function returns the Pearson correlation coefficients of the input array. It is useful for determining the relationship between two variables.
Example
In the following example, we are calculating the correlation coefficient matrix for two arrays using the corrcoef() function −
import numpy as np # Define two arrays data1 = np.array([1, 2, 3, 4, 5]) data2 = np.array([5, 4, 3, 2, 1]) # Calculate the correlation coefficient corr_coef = np.corrcoef(data1, data2) print("Correlation Coefficient Matrix:") print(corr_coef)
The result is as shown below −
Correlation Coefficient Matrix: [[ 1. -1.] [-1. 1.]]
Statistical Funtions
Following are the different Statistical function in Numpy −
Sr.No. | Functions & Description |
---|---|
1 |
amin()
Return the minimum of an array or minimum along an axis |
2 |
amax()
Return the maximum of an array or maximum along an axis |
3 |
nanmin()
Return minimum of an array or minimum along an axis, ignoring any NaNs |
4 |
nanmax()
Return the maximum of an array or maximum along an axis, ignoring any NaNs |
5 |
ptp()
Range of values (maximum - minimum) along an axis
|
6 |
percentile()
Compute the q-th percentile of the data along the specified axis
|
7 |
nanpercentile()
Compute the qth percentile of the data along the specified axis, while ignoring nan values |
8 |
quantile()
Compute the q-th quantile of the data along the specified axis |
9 |
nanquantile()
Compute the qth quantile of the data along the specified axis, while ignoring nan values. Returns the qth quantile(s) of the array elements |
10 |
median()
Compute the median along the specified axis |
11 |
average()
Compute the weighted average along the specified axis |
12 |
mean()
Compute the arithmetic mean along the specified axis |
13 |
std()
Compute the standard deviation along the specified axis |
14 |
var()
Compute the variance along the specified axis |
15 |
nanmean()
Compute the arithmetic mean along the specified axis, ignoring NaNs |
16 |
nanstd()
Compute the standard deviation along the specified axis, while ignoring NaNs |
17 |
nanvar()
Compute the variance along the specified axis, while ignoring NaNs |