Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

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.

import numpy as np marks = np.array([65, 82, 74, 91, 58, 87]) print("Minimum mark:", np.min(marks)) print("Maximum mark:", np.max(marks))

Output:

Minimum mark: 58 Maximum mark: 91

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.

import numpy as np marks = np.array([ [72, 85, 90], [65, 78, 88], [91, 82, 76] ]) print("Overall minimum:", np.min(marks)) print("Overall maximum:", np.max(marks)) print("Column minimums:", np.min(marks, axis=0)) print("Column maximums:", np.max(marks, axis=0)) print("Row minimums:", np.min(marks, axis=1)) print("Row maximums:", np.max(marks, axis=1))

Output:

Overall minimum: 65 Overall maximum: 91 Column minimums: [65 78 76] Column maximums: [91 85 90] Row minimums: [72 65 76] Row maximums: [90 88 91]
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.

import numpy as np scores = np.array([78, 92, 65, 88, 96, 71]) minimum_index = np.argmin(scores) maximum_index = np.argmax(scores) print("Minimum index:", minimum_index) print("Minimum value:", scores[minimum_index]) print("Maximum index:", maximum_index) print("Maximum value:", scores[maximum_index])

Output:

Minimum index: 2 Minimum value: 65 Maximum index: 4 Maximum value: 96
Remember: NumPy uses zero-based indexing. In a one-dimensional array, the first element is at index 0. If the same extreme value occurs more than once, np.argmin() and np.argmax() return the first occurrence.

Finding Indices Along an Axis

import numpy as np data = np.array([ [10, 80, 30], [40, 20, 90], [70, 60, 50] ]) print("Minimum index in each column:", np.argmin(data, axis=0)) print("Maximum index in each row:", np.argmax(data, axis=1))

Output:

Minimum index in each column: [0 1 0] Maximum index in each row: [1 2 0]

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.

import numpy as np temperatures = np.array([22, 28, 31, 25, 35, 27]) minimum = np.min(temperatures) maximum = np.max(temperatures) value_range = maximum - minimum print("Minimum:", minimum) print("Maximum:", maximum) print("Range:", value_range)

Output:

Minimum: 22 Maximum: 35 Range: 13
Understanding the Range Minimum 22 Maximum 35 Range = 35 - 22 = 13

The range is calculated by subtracting the minimum from the maximum.

Note: The range depends only on the minimum and maximum. It does not describe how values are distributed between these two points.

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.

import numpy as np scores = np.array([45, 55, 60, 70, 80, 90, 95]) p25 = np.percentile(scores, 25) p50 = np.percentile(scores, 50) p75 = np.percentile(scores, 75) print("25th percentile:", p25) print("50th percentile:", p50) print("75th percentile:", p75)

Output:

25th percentile: 57.5 50th percentile: 70.0 75th percentile: 87.5

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.

import numpy as np data = np.array([10, 20, 30, 40, 50, 60, 70, 80]) print("10th percentile:", np.percentile(data, 10)) print("50th percentile:", np.percentile(data, 50)) print("90th percentile:", np.percentile(data, 90)) print("100th percentile:", np.percentile(data, 100))

Output:

10th percentile: 17.0 50th percentile: 45.0 90th percentile: 73.0 100th percentile: 80.0

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

import numpy as np data = np.array([12, 18, 25, 31, 42, 50, 67, 80]) percentages = [10, 25, 50, 75, 90] results = np.percentile(data, percentages) for percentage, result in zip(percentages, results): print(f"{percentage}th percentile: {result}")

Output:

10th percentile: 16.2 25th percentile: 23.25 50th percentile: 36.5 75th percentile: 54.25 90th percentile: 70.9

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.

import numpy as np scores = np.array([45, 55, 60, 70, 80, 90, 95]) print("First quartile:", np.quantile(scores, 0.25)) print("Median:", np.quantile(scores, 0.50)) print("Third quartile:", np.quantile(scores, 0.75))

Output:

First quartile: 57.5 Median: 70.0 Third quartile: 87.5
Function Input Scale Example
np.percentile() 0 to 100 np.percentile(scores, 75)
np.quantile() 0 to 1 np.quantile(scores, 0.75)
Easy rule: The 75th percentile and the 0.75 quantile represent the same percentile position when using the same calculation method.

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.

import numpy as np scores = np.array([12, 18, 20, 25, 28, 31, 35, 40, 90]) q1 = np.percentile(scores, 25) q2 = np.percentile(scores, 50) q3 = np.percentile(scores, 75) iqr = q3 - q1 print("Q1:", q1) print("Q2:", q2) print("Q3:", q3) print("IQR:", iqr)

Output:

Q1: 20.0 Q2: 28.0 Q3: 35.0 IQR: 15.0
Quartiles and IQR Minimum Q1 Q2 Q3 Maximum IQR = Q3 - Q1 The IQR measures the spread of the middle 50% of the data.

Quartiles describe positions in the distribution; IQR measures the distance between Q1 and Q3.

Note: Different statistical tools may use different quartile conventions. NumPy's default percentile method is linear interpolation.

9. Minimum, Maximum and Percentiles Along an Axis

For multidimensional data, percentiles can be calculated along rows or columns using the axis parameter.

import numpy as np marks = np.array([ [60, 70, 80, 90], [55, 65, 75, 85], [72, 82, 92, 96] ]) print("Minimum per student:", np.min(marks, axis=1)) print("Maximum per student:", np.max(marks, axis=1)) print("Median per student:", np.percentile(marks, 50, axis=1)) print("Median per subject:", np.percentile(marks, 50, axis=0))

Output:

Minimum per student: [60 55 72] Maximum per student: [90 85 96] Median per student: [75. 70. 87.] Median per subject: [60. 70. 80. 90.]

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.

import numpy as np students = np.array(["Samadhan", "Pradnya", "Samiksha"]) marks = np.array([ [65, 78, 85, 90, 72], [88, 92, 79, 95, 84], [70, 75, 82, 89, 94] ]) for i, student in enumerate(students): student_marks = marks[i] minimum = np.min(student_marks) maximum = np.max(student_marks) median = np.median(student_marks) percentile_75 = np.percentile(student_marks, 75) print("Student:", student) print("Minimum:", minimum) print("Maximum:", maximum) print("Median:", median) print("75th percentile:", percentile_75) print() print("Overall minimum:", np.min(marks)) print("Overall maximum:", np.max(marks))

Output:

Student: Samadhan Minimum: 65 Maximum: 90 Median: 78.0 75th percentile: 85.0 Student: Pradnya Minimum: 79 Maximum: 95 Median: 88.0 75th percentile: 92.0 Student: Samiksha Minimum: 70 Maximum: 94 Median: 82.0 75th percentile: 89.0 Overall minimum: 65 Overall maximum: 95

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.