NumPy Minimum, Maximum and Percentiles
NumPy provides functions for identifying the smallest and largest values in an array and understanding the distribution of numerical data. In this lesson, you will learn how to use minimum and maximum functions, find the positions of extreme values, calculate percentiles and quantiles, and apply these concepts to practical data analysis.
1. Understanding Minimum and Maximum
The minimum is the smallest value in a dataset, while the maximum is
the largest value. NumPy provides np.min() and
np.max() to calculate these values efficiently.
Output:
Important Functions
np.min(array)returns the smallest value.np.max(array)returns the largest value.np.amin(array)is an alias for the minimum operation.np.amax(array)is an alias for the maximum operation.
2. Finding Minimum and Maximum in Two-Dimensional Arrays
NumPy can calculate the minimum and maximum across an entire matrix or along a specified axis.
Output:
| Expression | Result Meaning |
|---|---|
np.min(marks) |
Smallest value across the complete matrix. |
np.max(marks) |
Largest value across the complete matrix. |
np.min(marks, axis=0) |
Minimum of each column. |
np.max(marks, axis=1) |
Maximum of each row. |
3. Finding the Position of Extreme Values
Sometimes the value itself is not enough. You may also need to know
where the minimum or maximum occurs. NumPy provides
np.argmin() and np.argmax() for this purpose.
Output:
np.argmin() and
np.argmax() return the first occurrence.
Finding Indices Along an Axis
Output:
4. Understanding the Range
The range describes the difference between the largest and smallest values in a dataset. It provides a simple measure of how widely values are spread.
Output:
The range is calculated by subtracting the minimum from the maximum.
5. Introduction to Percentiles
A percentile describes a position within an ordered dataset. The 25th percentile is the value below which approximately 25 percent of observations fall, according to the selected percentile calculation method.
Percentiles are useful for comparing scores, understanding income distributions, examining test results, and identifying unusually high or low observations.
Output:
Common Percentiles
- 25th percentile: First quartile, also called Q1.
- 50th percentile: Median, also called Q2.
- 75th percentile: Third quartile, also called Q3.
- 90th percentile: A high-position value often used in performance analysis.
6. Using np.percentile()
The np.percentile() function calculates a percentile using
a percentage value from 0 to 100.
Output:
With the default linear method, the calculated percentile can fall between two existing observations. The 50th percentile is the median, while the 100th percentile is the maximum for ordinary finite data.
Calculate Several Percentiles at Once
Output:
7. Understanding Quantiles
Quantiles divide a distribution using proportions between 0 and 1.
NumPy's np.quantile() is closely related to
np.percentile(), but it uses fractional values instead
of percentages.
Output:
| Function | Input Scale | Example |
|---|---|---|
np.percentile() |
0 to 100 | np.percentile(scores, 75) |
np.quantile() |
0 to 1 | np.quantile(scores, 0.75) |
8. Quartiles and Interquartile Range
Quartiles divide ordered data into four parts. The interquartile range (IQR) is the difference between the third quartile and the first quartile.
The IQR describes the spread of the middle half of the data and is less affected by extreme values than the full range.
Output:
Quartiles describe positions in the distribution; IQR measures the distance between Q1 and Q3.
9. Minimum, Maximum and Percentiles Along an Axis
For multidimensional data, percentiles can be calculated along rows
or columns using the axis parameter.
Output:
Using axis=1 calculates one result for each student row.
Using axis=0 calculates one result for each subject column.
10. Practical Project: Student Score Analysis
In this project, we will compare three students by examining their minimum marks, maximum marks, median marks, and percentile values.
Output:
Project Learning Outcomes
- Identify the minimum and maximum marks for each student.
- Use the median to understand a student's central score.
- Use the 75th percentile to examine higher score positions.
- Compare individual results with overall dataset extremes.
11. Common Mistakes and Best Practices
Confusing Values and Indices
np.min() returns the minimum value, while
np.argmin() returns its index. Similarly,
np.max() returns a value and np.argmax()
returns an index.
Using the Wrong Percentile Scale
np.percentile() uses values from 0 to 100, whereas
np.quantile() uses values from 0 to 1.
Ignoring the Axis
In multidimensional arrays, choose the axis carefully to obtain row-wise, column-wise, or overall results.
Assuming Percentiles Must Exist in the Data
With linear interpolation, a percentile may fall between two observations rather than matching an existing value.
Comparing Different Percentile Methods
If you compare results from different software or statistical packages, verify that the same percentile method is being used.
12. Practice Exercises
Exercise 1: Minimum and Maximum
Create an array containing [18, 45, 12, 67, 34, 89].
Find its minimum, maximum, and range.
Exercise 2: Find Indices
Use np.argmin() and np.argmax() to find
the positions of the smallest and largest values in an array.
Exercise 3: Calculate Percentiles
For [10, 20, 30, 40, 50, 60, 70, 80], calculate the
25th, 50th, and 75th percentiles.
Exercise 4: Quantiles and IQR
Calculate Q1 and Q3 using np.quantile(). Subtract Q1
from Q3 to find the interquartile range.
Exercise 5: Two-Dimensional Analysis
Create a matrix of marks for four students. Find the minimum and maximum for each student and each subject.
Exercise 6: Student Comparison
Create a marks matrix for Samadhan, Pradnya, and Samiksha. Calculate each student's median and 90th percentile.
Summary
In this lesson, you learned to identify the smallest and largest
values in NumPy arrays using np.min() and
np.max(). You also practiced finding the positions of
extreme values with np.argmin() and np.argmax().
You explored percentiles and quantiles using np.percentile()
and np.quantile(), including quartiles, the median, and the
interquartile range. You also learned how to calculate these results
along a selected axis and apply them to student score analysis.