Chapter 6 – NumPy (Fully Detailed Notes)
NumPy (Numerical Python) is a fundamental library for scientific and numerical computing
in Python.
It provides powerful tools for handling large datasets, performing mathematical operations,
broadcasting,
vectorization, and working with multi-dimensional arrays efficiently.
===========================================================
6.1 SHAPE – Understanding Array Dimensions
===========================================================
• Every NumPy array has an attribute called shape.
• The shape describes the structure of the array in terms of the number of rows, columns,
and dimensions.
Example 1: 1D array
a = [Link]([2, 3, 8])
[Link] → (3,)
Meaning:
• 1 dimension
• Contains 3 elements
Example 2: 2D array
b = [Link]([
[2, 3, 8],
[4, 5, 6]
])
[Link] → (2, 3)
Meaning:
• 2 rows
• 3 columns
Example 3: 3D array
c = [Link]([
[1,2], [3,4]
],
[5,6], [7,8]
])
[Link] → (2, 2, 2)
Meaning:
• 2 blocks
• Each block has 2 rows
• Each row has 2 columns
shape is critical for:
• reshaping arrays
• broadcasting
• matrix multiplication
• slicing and indexing
===========================================================
6.2 SLICING – Extracting Portions of Arrays
===========================================================
Slicing allows selecting specific rows, columns, or parts of arrays.
---------- 1D SLICING ----------
Let:
a = [Link]([10, 20, 30, 40, 50])
Examples:
a[1] → 20
a[1:4] → [20, 30, 40]
a[:3] → [10, 20, 30]
a[3:] → [40, 50]
a[::-1] → reversed array → [50, 40, 30, 20, 10]
---------- 2D SLICING ----------
Let:
b = [Link]([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
Examples:
b[0] → [10, 20, 30] (entire first row)
b[:, 1] → second column → [20, 50, 80]
b[1:, :2] → rows 1 onwards, first 2 columns → [[40,50], [70,80]]
b[0:2, 1:3] → submatrix → [[20,30], [50,60]]
---------- IMPORTANT CONCEPTS ----------
• ":" selects all values.
• Negative indexing works: b[-1] → last row.
• Slicing creates a VIEW, not a copy (in most cases).
===========================================================
6.3 MASKING – Conditional Selection
===========================================================
Masking is one of NumPy’s most powerful tools.
A mask is a condition written on an array that returns True/False values.
Example:
a = [Link]([230, 10, 284, 39, 76])
mask = a > 200
mask → [True, False, True, False, False]
Using mask to filter:
a[a > 200] → [230, 284]
Using mask to change values:
a[a > 200] = 0
Updated array → [0, 10, 0, 39, 76]
---------- Multiple conditions ----------
• a[(a > 40) & (a < 100)]
• a[(a == 50) | (a == 100)]
---------- Useful in ----------
• image processing
• data cleaning
• filtering values
• conditional updates
===========================================================
6.4 BROADCASTING – Automatic Expansion of Dimensions
===========================================================
Broadcasting allows operations between arrays of different shapes.
Example:
a = [Link]([[0,1],
[2,3],
[4,5]])
b = [Link]([10, 100])
Operation:
a*b
NumPy stretches b to match the shape of a:
b becomes:
[[10,100],
[10,100],
[10,100]]
Result:
[[0,100],
[20,300],
[40,500]]
---------- Broadcasting Rules ----------
1. Compare shapes from RIGHT to LEFT.
2. Two dimensions are compatible if:
• They are equal OR
• One of them is 1
3. If a dimension is 1, it can be stretched.
---------- Broadcasting Error Example ----------
c = [Link]([[0,1,2],[3,4,5]]) → shape (2,3)
d = [Link]([10,100]) → shape (2,)
c and d are not compatible → ERROR
Fix:
d[:, None] → shape becomes (2,1)
Now compatible with (2,3)
===========================================================
6.5 DTYPE – Data Types in NumPy
===========================================================
NumPy supports multiple numeric types:
• int8 → -128 to 127
• uint8 → 0 to 255
• int16, int32, int64
• float16, float32, float64
• bool
Example: uint8 overflow
a = [Link]([200], dtype='uint8')
a + a → 144
Reason:
200 + 200 = 400
uint8 max = 255
400 - 256 = 144 (wrap-around)
---------- Checking dtype ----------
[Link]
===========================================================
6.6 Changing dtype
===========================================================
Use astype() to convert data type.
Example:
a = [Link]([200], dtype='uint8')
b = [Link]('uint16')
b + b → 400 (no overflow)
===========================================================
6.7 Advanced NumPy Usage
===========================================================
Topics recommended for deeper study:
• Vectorized operations (faster than loops)
• Fancy indexing
• Boolean indexing
• Transposing matrices
• Reshaping arrays (reshape(), ravel())
• Matrix multiplication using dot(), @ operator
• Statistical functions (mean, std, sum)
• Stacking arrays (vstack, hstack)
• Splitting arrays
Example of vectorized operation:
a = [Link]([1,2,3,4])
a * 10 → [10,20,30,40]
Much faster than:
for i in list: multiply each value
===========================================================
6.8 Exercises (Expanded)
===========================================================
1. Create a uint8 array and repeatedly add values to observe overflow.
2. Write masking code to replace values below 50 with 0.
3. Use slicing to extract the center 2x2 part of a 3x3 matrix.
4. Use broadcasting to multiply a 3x2 matrix with a 1D array of size 2.
5. Create a 3D array and print its shape and slices.
6. Convert arrays from float to int and observe truncation.