
- 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 quantile() Function
The NumPy quantile() function computes the q-th quantile (or percentile) of the data along a specified axis. A quantile is a value below which a given percentage of observations fall. For example, the 0.5 quantile (50th percentile) is the median of the data, while the 0.25 quantile (25th percentile) is the first quartile, and the 0.75 quantile (75th percentile) is the third quartile.
Syntax
Following is the syntax of the NumPy quantile() function −
numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, weights=None, interpolation=None)
Parameters
Following are the parameters of the NumPy quantile() function −
- a: Input array, it can be a NumPy array, list, or a scalar value.
- q: The quantile(s) to compute, which can be a single number or a list/array of numbers. It should be in the range [0, 1] (for example, 0.25 for the 25th percentile, 0.5 for the median).
- axis (optional): The axis along which to compute the quantile. Default is None, which means that the quantile is computed over the entire array.
- out (optional): A location into which the result is stored. If provided, it must have the same shape as the expected output.
- overwrite_input (optional): If True, the input array is modified in place. Default is False.
- weights: If weights=None, then all data in a are assumed to have a weight equal to one. Only method=inverted_cdf supports weights.
- keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
- interpolation(optional): Deprecated name for the method keyword argument.
- method (optional): The interpolation method to use when the desired quantile lies between two data points. It can be one of the following:
- linear(default): Performs linear interpolation between the two closest data points.
- lower: Returns the lower of the two data points.
- higher: Returns the higher of the two data points.
- midpoint: Returns the midpoint of the two data points.
- nearest: Returns the nearest data point.
Return Values
This function returns the q-th quantile(s) of the input array along the specified axis. The result is a scalar or array depending on the input and the value of the q parameter.
Example
Following is a basic example to compute the 50th percentile (median) of an array using the NumPy quantile() function −
import numpy as np # input array x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # applying quantile result = np.quantile(x, 0.5) # 50th percentile (median) print("Quantile Result (50th percentile):", result)
Output
Following is the output of the above code −
Quantile Result (50th percentile): 5.0
Example: Specifying a Different Quantile
The quantile() function can also compute other quantiles, such as the 25th or 75th percentile. In the following example, we have computed the 25th and 75th percentiles −
import numpy as np # input array x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # applying quantile for 25th and 75th percentiles result = np.quantile(x, [0.25, 0.75]) print("Quantile Result (25th and 75th percentiles):", result)
Output
Following is the output of the above code −
Quantile Result (25th and 75th percentiles): [3. 7.]
Example: Multi-dimensional Array
The quantile() function operates on multi-dimensional arrays. In the following example, we have created a 2D NumPy array, and computed the 50th percentile (median) along a specific axis −
import numpy as np # 2D array x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # applying quantile along axis 0 (columns) result = np.quantile(x, 0.5, axis=0) print("Quantile Result along axis 0 (median):", result)
Output
Following is the output of the above code −
Quantile Result along axis 0 (median): [4. 5. 6.]
Example: Using Different Methods for Interpolation
In the following example, we have used different interpolation methods to compute the 50th percentile of an array. We have demonstrated the linear, lower, higher, and midpoint methods −
import numpy as np # input array x = np.array([1, 3, 5, 7, 9]) # applying quantile with different methods result_linear = np.quantile(x, 0.5, method='linear') result_lower = np.quantile(x, 0.5, method='lower') result_higher = np.quantile(x, 0.5, method='higher') result_midpoint = np.quantile(x, 0.5, method='midpoint') print("Linear Method:", result_linear) print("Lower Method:", result_lower) print("Higher Method:", result_higher) print("Midpoint Method:", result_midpoint)
Output
Following is the output of the above code −
Linear Method: 5.0 Lower Method: 3.0 Higher Method: 7.0 Midpoint Method: 5.0
Example: Plotting 'quantile()' Function
In the following example, we have plotted the behavior of the quantile function for a range of input values. We have calculated and plotted the 25th, 50th, and 75th percentiles −
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 1, 100) # input range y_25 = np.quantile(x, 0.25) y_50 = np.quantile(x, 0.5) y_75 = np.quantile(x, 0.75) plt.plot(x, np.full_like(x, y_25), label="25th percentile") plt.plot(x, np.full_like(x, y_50), label="50th percentile") plt.plot(x, np.full_like(x, y_75), label="75th percentile") plt.title("Quantile Function") plt.xlabel("Input") plt.ylabel("Quantile Value") plt.legend() plt.grid() plt.show()
Output
The plot demonstrates the constant nature of each quantile (25th, 50th, 75th) as input values remain unchanged for each quantile −
