How do you calculate percentiles with Python/ NumPy?

By ayush goel in 22 Sep 2023 | 04:17 pm
ayush goel

ayush goel

Student
Posts: 346
Member since: 21 Sep 2023

How do you calculate percentiles with Python/ NumPy?

22 Sep 2023 | 04:17 pm
0 Likes
divas goyal

divas goyal

Student
Posts: 453
Member since: 22 Sep 2023

You can calculate percentiles in Python using the `numpy.percentile()` function from the NumPy library. This function allows you to find the value below which a given percentage of data falls. Here's how you can use it:


# Sample dataset

data = np.array([12, 45, 67, 23, 8, 42, 78, 56, 31, 19])


# Calculate the 25th percentile

percentile_25 = np.percentile(data, 25)

print("25th Percentile:", percentile_25)


# Calculate the 50th percentile (median)

median = np.percentile(data, 50)

print("Median (50th Percentile):", median)


# Calculate the 75th percentile

percentile_75 = np.percentile(data, 75)

print("75th Percentile:", percentile_75)

```


In this example:


1. We import NumPy as `np`.

2. We define a sample dataset called `data`.

3. We use the `np.percentile()` function to calculate specific percentiles:

   - `np.percentile(data, 25)` calculates the 25th percentile.

   - `np.percentile(data, 50)` calculates the 50th percentile (which is the median).

   - `np.percentile(data, 75)` calculates the 75th percentile.


You can replace `data` with your own dataset to calculate percentiles for your specific data.




22 Sep 2023 | 05:48 pm
0 Likes

Report

Please describe about the report short and clearly.