# Numpy Quantile() Explained With Examples

Hello geeks and welcome in this article, we will cover NumPy quantile(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But first, let us try to understand quantile all alone just by itself.

Modern-day hits like Data science or Machine learning require a basic understanding of statistics. Moreover, Quantile is an important part of statistics. They can be understood as the cut points dividing observations in the sample in the same way. Based on that, we can define NumPy quantile() as the function that helps us calculate the nth quantile of the given data along the specified axis. Up next, we will be looking at the syntax of the function.

Contents

## Syntax of Numpy Quantile()

``numpy.quantile(a, q, axis = None)` `

Here above, we can see the general syntax of our function. Here a and q are the necessary parameter. Several other optional parameters are associated with it, which we will see next.

## Parameter of Numpy Quantile()

a:array_like

It represents the input array on which the various operation needs to performed.

q: array_like of float

This parameter represents the value of the quantile, which needs to be computed. The value must lie between 0 to 1. like (.25, .50, .75, and 1) in the case of quartile.

axis: int, tuple of int, None

It is an optional parameter along which the value needs to computed. By default it will compute the value along the flattened version of array.

out: ndarray

It is another optional parameter and represents an alternative output array in which we need to place the result. It must have the same shape as well as size as the expected output.

Overwrite_input: bool

If this parameter is stated to true. Then it allows input array a to be modified by intermediate calculation to save memory.

## Return

quantile: scalar or ndarray

We get the return as scalar if q is the single quantile with axis=0. If multiple values of quantile are given, then the first axis of the quantile corresponds to quantile.

## Example

Now we have covered almost all the theory part associated with NumPy quantile(). In this section, we will see the various components in an application through different examples. We will start with an elementary level example and gradually move our way up.

```#input
import numpy as ppool
a=[1,23,4,5,6]
print(ppool.quantile(a,.25))
print(ppool.quantile(a,.50))
print(ppool.quantile(a,.75))
```

Output:

``````4.0
5.0
6.0``````

Above, we can see a straightforward example of the quantile. Here we are dealing with a four-group quantile, also called quartile. At first, we have imported the NumPy module. Then, they declared a 1-d array. After which, we have used our syntax and print statement to get the desired output. Like any other statical operation, first, our data is arranged in a particular order, usually ascending. After which the operation is performed.

Now let us look at another example.

```#input
import numpy as ppool
a=[[1,23,4,5,6],
[2,45,5,6,7]]
print(ppool.quantile(a,.10))
print(ppool.quantile(a,.50))
print(ppool.quantile(a,1))
```

Output:

``````1.9
5.5
45``````

In the above example, we have considered a 2-dimensional array. Rest we have followed all the steps the same as the first example. Here we are dealing with a 10 group quantile, which is also known as deciles. Here we have requested output for (.1, .5, and 1) from the program, and our output justifies that.

Next let us see an example that involves some of our optional parameter.

```#input
import numpy as ppool
a=[[1,23,4,5,6],
[2,45,5,6,7]]
print(ppool.quantile(a,.50))
print(ppool.quantile(a,.50,axis=1))

```

Output:

``````5.5
[5. 6.]``````

In the above example, we have considered a similar array as in the above example. But in the syntax, we have made certain changes by adding an optional parameter axis in the first case without the axis where we are trying to find out the quantile with value (.50). Here as we have not specified any axis, the program flattened’s the array and treat it as 1 single array instead of a 2-d array. Whereas in the second case, we have defined the axis as 1, so we get quantile value separately for the 2 sub-arrays.

## Quantile Mapping In NumPy/SciPy

Quantile can be used as a mapping for arrays. As of now, you cannot use Quantile as a mapping from numpy. But by using its similar library Scipy, you can compute Computes empirical quantiles of an array. This quantiles are computed as (1-x)arr[i] + (x)arr[i+1]. Where i is order statistics and x is a function of aphap and betap.

### Parameters

• array: Input array whose quantile you want to compute. Maximum of the 2-dimensional array is accepted
• prob (optional array): Quantile list to compute.
• alphap (float): Plotting positions parameter, default is 0.4.
• betap (float): Plotting positions parameter, default is 0.4.
• axis (int): Axis along which you have to trim.
• limit (tuple): Only trims the values belonging to this limit.

### Returns

``MaskedArray of mquantiles``

Returns the array of mapped quantile values. These values are computed from the formula mentioned above.

### Example

```from scipy.stats.mstats import mquantiles
import numpy as np

x = np.array([7, 17., 59., 05., 12., 34., 45., 7., 35., 10., 16.])
print(mquantiles(a))
```

### Output

``array([ 7.6, 16. , 34.8])``

### Explanation

Firstly, we imported the mquantiles function from the SciPy module. Then we created an array with sample values given above. The array is then used to compute the mapping quantile. From the output, you can check the quantile array.

## Quantile Mapping as a Climatic Bias Correction Technique

Many researchers are creating new bias correction techniques for correcting simulated outputs. These simulated results are generated by downscaling and error correction of the dataset. Regional Climate Models are the best example of Quantile Mapping in Numpy. These RCMs contain many severe outliers and errors in their datasets. Quantile Mapping is used to remove these systematic errors and deduce an optimized climate scenario. Following are the repositories where quantile mapping are customized in Numpy and Scipy –

1. Empirical Statistical Downscaling – Compares the best performing scenarios with each other and implements quantile mapping to provide better performance. The mapping quantiles easily monitor farthest precipitation events over the data.
2. pyCAT – This Github repository features two primary bias correction methods: Basic Quantile Mapping and Scaled Distribution Mapping. Both of these corrections work in deducing climate analysis.