The numpy percentile is a value that tells you how much data points fall below that value. In this entire article, I will show you how to find numpy percentile of an array with examples.
If you know the percentile value then it really helps. You can break large datasets into chunks or groups. Due to it, the calculation becomes very fast and error-free. The formula for the percentile is below.
Percentile = (Number of Values Below “x” / Total Number of Values) × 100
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
The general parameters are the following.
a: Your input array.
q: Percentile or sequence of percentiles to compute. It should be between 0 and 100 inclusive.
axis: Axis or axes along the percentiles should be calculated.
out: Alternative output array in which to place the result.
overwrite_input: If True. It allows the input array to be modified by intermediate calculations.
You can know more about the other parameters in the Numpy Offical Documentation.
In this entire section, I will show the various example for finding the percentile of a NumPy array. But before going to that part let’s import the numpy module. It can be done using the import statement.
import numpy as np
Let’s calculate a single dimension array and use the np.percentile() method on it.
array_1d = np.array([1,2,3,4,9,12])
np.percentile(array_1d,50)
Output
Here I am getting the value of 3.5 as 50% percentile.
Now let’s find the percentile for the two-dimensional NumPy array. As it is a 2D array there are many inner cases also.
For example percentile calculation over on columns or rows only. We will see them.
array_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
In order To find the percentile using all the values, you have to just pass it to the numpy.percentile().
np.percentile(array_2d,50)
The output will be a single value.
Calculating NumPy percentile for each column requires an extra argument and that is axis =0.
np.percentile(array_2d,50,axis=0)
It calculates percentile horizontally.
Output
It is the same method as it was in case 2. The only difference is the value of the axis. Here it 1.
np.percentile(array_2d,50,axis=1)
The percentile will be calculated by using the vertical elements.
Output
In the above example, I was outputting the results in a new variable. Instead, I want the results in another array. To do so I have to create a zero array. Then output the results using the out argument.
However, you should note that the dimension of the zero arrays should be the same as the output array.
Execute the code below.
m = np.percentile(array_2d,50,axis=0)
out = np.zeros_like(m)
np.percentile(array_2d, 50, axis=0, out=out)
Output
In this example, I will use the overwrite_input parameter. It allows you to overwrite the dimension of the input array.
Run the code below.
copy_array = np.copy(array_2d)
np.percentile(copy_array, 50, axis=1, overwrite_input=True)
Output
The above are the examples I have compiled for you. It helps you in a deep understanding of how to find the percentile of the array. I hope you have liked this article. If you have any queries regarding this then you can contact us.