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.
What is the importance of Percentile?
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
The syntax for Finding Numpy Percentile
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.
Examples for the Implementation for Numpy Percentile
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
Example 1: Calculate percentile for 1D Numpy array
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)
Here I am getting the value of 3.5 as 50% percentile.
Example 2: Numpy percentile for 2D Numpy array.
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.
Creation of 2D Numpy array
array_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
Case 1: Calculating percentile using all the elements.
In order To find the percentile using all the values, you have to just pass it to the numpy.percentile().
The output will be a single value.
Case 2: Calculate percentile for each column
Calculating NumPy percentile for each column requires an extra argument and that is axis =0.
It calculates percentile horizontally.
Case 2: Calculate percentile for each row
It is the same method as it was in case 2. The only difference is the value of the axis. Here it 1.
The percentile will be calculated by using the vertical elements.
Example 3: Outputting the Percentile results to Zero array
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)
Example 4: Calculating the Percentile using the Copied Array
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)
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.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.