## What is Numpy Percentile?

**The percentile method in the numpy module is used to calculate the nth percentile of the given data (array elements) along the specified axis.** We basically use percentile in statistics which gives you a number that describes the value that a given percent of the values are lower than.

## Syntax

`numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, keepdims=False)`

## Parameters

**a**:**array_like**– This is the Input array.**q: array_like of float**– This is the percentile or sequence of percentile we need to compute. It should be between 0 to 100, both inclusive.**axis :**{int, tuple of int, None} – It is optional input. This is the axis along which we calculate the percentile. By default, we compute the percentile along with the flattened version of the array.-
**out: ndarray –**It is also an optional input. It is the alternative output array which we make to place the result. It should have the same shape and buffer length as the expected output, but the type can be cast by itself if it is required. **overwrite_input: bool –**It is also an optional input. If the boolean value is True, we can modify the input array through intermediate calculations to save the memory.**keepdims: bool**– It is also an optional input. If the value is set to be True, the reduced axes are left in the result as dimensions with one size. With this, the result will be correct against the original array.

## Return Value

If *q* is a single percentile and the * axis is set to None*, then the output is always a scalar or

**array with percentile values along the specified axis.**## Examples to Find Numpy Percentile

Let us understand the percentile function of the numpy module in detail with the help of examples:

### 1. Numpy Percentile using 1-d Array

We will be using a **1-D array** to calculate the array’s percentile by taking the input array.

```
#using 1-D array
#numpy.percentile() method
import numpy as np
arr = [5,6,9,87,2,3,5,7,2,6,5,2,3,4,69,4]
print("Array : ",arr)
x = np.percentile(arr, 50)
print("50 percentile : ",x)
```

**Output:**

```
Array : [5, 6, 9, 87, 2, 3, 5, 7, 2, 6, 5, 2, 3, 4, 69, 4]
50 percentile : 5.0
```

**Explanation:**

Here, firstly, we have imported the numpy module in Python as np. Secondly, we have taken a 1-D array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile() in which we have given arr and 50 percentile as the parameter and stored that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

### 2. Using 2-D array

We will be using a **2-D array** to calculate the percentile of the array by taking the input array.

```
#using 2-D array
#numpy.percentile() method
import numpy as np
arr = [[5,6,8],[6,9,2]]
print("Array : ",arr)
x = np.percentile(arr, 50)
print("50 percentile : ",x)
```

**Output:**

```
Array : [[5, 6, 8], [6, 9, 2]]
50 percentile : 6.0
```

**Explanation:**

First, we have imported the numpy module in Python as np. Secondly, we have taken a 2-D array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile() in which we have given arr and 50 percentile as the parameter and stored that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

### 3. Numpy Percentile using axis = 0 in 2-D array

We will be using **axis = 0** in a 2-D array to calculate the percentile of the array by taking the input array.

```
#using 2-D array axis = 0
#numpy.percentile() method
import numpy as np
arr = [[5,6,8],[6,9,2]]
print("Array : ",arr)
x = np.percentile(arr, 25, axis = 0)
print("50 percentile : ",x)
```

**Output:**

```
Array : [[5, 6, 8], [6, 9, 2]]
50 percentile : [5.25 6.75 3.5 ]
```

**Explanation:**

First, we have imported the numpy module in Python as np. Secondly, we have taken a 2-D array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile(), giving arr, 25 percentile, and axis = 0 as the parameter and storing that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

### 4. Using axis = 1 in 2-d array

We will be using **axis = 1** in a 2-D array to calculate the percentile of the array by taking the input array.

```
#using 2-D array axis = 1
#numpy.percentile() method
import numpy as np
arr = [[5,6,8],[6,9,2]]
print("Array : ",arr)
x = np.percentile(arr, 25, axis = 1)
print("50 percentile : ",x)
```

**Output:**

```
Array : [[5, 6, 3], [6, 7, 2]]
50 percentile : [4. 4. ]
```

**Explanation:**

First, we have imported the numpy module in Python as np. Secondly, we have taken a 2-D array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile(), giving arr, 25 percentile, and axis = 1 as the parameter and storing that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

### 5. Numpy Percentile using axis=1 and keepdims = true in a 2-D array

We will be using **axis = 1 **and **Keepdims = True** in a 2-D array to calculate the percentile of the array by taking the input array.

```
#using 2-D array axis = 1 and Keepdims = True
#numpy.percentile() method
import numpy as np
arr = [[10,9,4],[3,2,1]]
print("Array : ",arr)
x = np.percentile(arr, 50, axis = 1, keepdims = True)
print("50 percentile : ",x)
```

**Output:**

```
Array : [[10, 9, 4], [3, 2, 1]]
50 percentile : [[9.]
[2.]]
```

**Explanation:**

First, we have imported the numpy module in Python as np. Secondly, we have taken a 2-D array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile(), giving arr, 50 percentile, axis = 1, and keepdims= True as the parameter. The value has stored that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

### 6. Using Out parameter in 2-D array

We will be using **axis = 0** and **out** in a 2-D array to calculate the percentile of the array by taking the input array.

```
import numpy as np
arr = [[10,9,4],[3,2,1]]
print("Array : ",arr)
m = np.percentile(arr, 50, axis=0)
out = np.zeros_like(m)
x = np.percentile(arr, 50, axis=0, out=out)
print("50 percentile : ",x)
```

**Output:**

```
Array : [[10, 9, 4], [3, 2, 1]]
50 percentile : [6.5 5.5 2.5]
```

**Explanation:**

First, we have imported the numpy module in Python as np. Secondly, we have taken a 2-d array. Thirdly, we have printed the input array. Fourthly, we have used the percentile method as np.percentile(), giving arr, 50 percentile, axis = 0, and out(output array of the same shape and buffer length) as the parameter. The value has stored that value in the x variable. At last, we have printed the value of x. Hence, the output is printed on the screen.

## Must Read

## Numpy Percentile vs Quantile

**Percentile** – Percentile method in the numpy module through which we can calculate the nth percentile of the given data (array elements) along the specified axis.

**Numpy Quantile** – Quantile method in the numpy module through which we can calculate the qth quantile of the given data(array elements) along the specified axis.

Let us understand with the help of example:

```
#numpy percentile vs numpy quantile
import numpy as np
arr = [10,20,30,40,50]
print("Array : ",arr)
print("\n")
print("25 percentile : ",np.percentile(arr, 25))
print("50 percentile : ",np.percentile(arr, 50))
print("75 percentile : ",np.percentile(arr, 75))
print("\n")
print(".25 Quantile : ",np.Quantile(arr, .25))
print(".50 Quantile : ",np.Quantile(arr, .50))
print(".75 Quantile : ",np.Quantile(arr, .75))
```

**Output:**

```
Array : [10, 20, 30, 40, 50]
25 percentile : 20.0
50 percentile : 30.0
75 percentile : 40.0
.25 Quantile : 20.0
.50 Quantile : 30.0
.75 Quantile : 40.0
```

**Explanation:**

Here, firstly, we have imported the numpy module as np. Secondly, we have taken an input array in the arr variable. Thirdly, we applied the percentile function, calculating the 25th, 50th, and 75th percentiles and printing the output. Fourthly, we applied the quantile function, calculating the .25th, .50th, and .75th percentiles and printing the output. Hence, we can see the output and know there is only a difference, which says **1 percentile = .01 quantile.**

## Conclusion

In this tutorial, we learned about percentile calculation with the help of the percentile method in the numpy standard library. All the parameters are explained with examples in detail. You can use any parameter according to your needs in your program or project.