
- 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 nanquantile() Function
The NumPy nanquantile() function computes the q-th quantile (or percentile) of the data along a specified axis, ignoring NaN (Not a Number) values. If multiple probability levels are given, first axis of the result corresponds to the quantiles.
The other axes are the axes that remain after the reduction of input data. If the input data contains integers or floats smaller than float64, the output data-type is float64. Otherwise, the output data-type is the same as that of the input. If out is specified, that array is returned instead.
In NumPy, the quantile() and nanquantile() functions allow for the calculation of the q-th quantile value. The only difference is that the nanquantile() function excludes NaN values from the computation, whereas the quantile() function includes them in the calculation.
Syntax
Following is the syntax of the NumPy nanquantile() function −
numpy.nanquantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False)
Parameters
Following are the parameters of the NumPy nanquantile() function −
- a: Input array, which can be a NumPy array, list, or scalar value. The input data may contain NaN values.
- 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.
- keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
- 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, ignoring NaN values. 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 nanquantile() function −
import numpy as np # input array with NaN values x = np.array([1, 2, 3, 4, np.nan, 6, 7, 8, 9]) # applying nanquantile result = np.nanquantile(x, 0.5) # 50th percentile (median) print("NanQuantile Result (50th percentile):", result)
Output
Following is the output of the above code −
NanQuantile Result (50th percentile): 5.0
Example: Multi-dimensional Array
The nanquantile() function operates on multi-dimensional arrays while ignoring NaN values. 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 with NaN values x = np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9]]) # applying nanquantile along axis 0 (columns) result = np.nanquantile(x, 0.5, axis=0) print("NanQuantile Result along axis 0 (median):", result)
Output
Following is the output of the above code −
NanQuantile 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, ignoring NaN values. We have demonstrated the linear, lower, higher, and midpoint methods −
import numpy as np # input array with NaN values x = np.array([1, 3, 5, np.nan, 9]) # applying nanquantile with different methods result_linear = np.nanquantile(x, 0.5, method='linear') result_lower = np.nanquantile(x, 0.5, method='lower') result_higher = np.nanquantile(x, 0.5, method='higher') result_midpoint = np.nanquantile(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 'nanquantile()' Function
In the following example, we have plotted the behavior of the nanquantile function for a range of input values, ignoring NaN 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.nanquantile(x, 0.25) y_50 = np.nanquantile(x, 0.5) y_75 = np.nanquantile(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("NanQuantile 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 −
