ch8s2_NumPyArraysAndOperations
NumPy arrays (or **ndarrays**) are the **core data structure** in NumPy and the foundation of high-performance numerical computing in Python.
Chapter 8: Introduction to NumPy
Sub-Chapter: NumPy Arrays and Operations — The Backbone of Numerical Computing
NumPy arrays (or ndarrays) are the core data structure in NumPy and the foundation of high-performance numerical computing in Python.
They provide efficient, vectorized operations on homogeneous data — enabling mathematical computations that are fast, expressive, and memory-efficient.
🧩 1. What Makes NumPy Arrays Special?
Unlike Python lists, which store objects of mixed types and have high overhead, NumPy arrays:
- Store homogeneous data in contiguous memory.
- Support vectorized operations — no explicit loops.
- Enable broadcasting between different shapes.
- Are implemented in C for performance.
Comparison Example
import numpy as np
import time
nums = list(range(1_000_000))
arr = np.arange(1_000_000)
# Python list
start = time.time()
nums_result = [x * 2 for x in nums]
print("List:", time.time() - start)
# NumPy array
start = time.time()
arr_result = arr * 2
print("NumPy:", time.time() - start)
🚀 NumPy operations can be 50–100× faster than native Python loops.
⚙️ 2. Creating Arrays
You can create NumPy arrays from Python sequences or using NumPy’s built-in constructors.
import numpy as np
a = np.array([1, 2, 3]) # From list
b = np.array([[1, 2, 3], [4, 5, 6]]) # 2D array
zeros = np.zeros((2, 3)) # All zeros
ones = np.ones((3, 3)) # All ones
range_array = np.arange(0, 10, 2) # Evenly spaced values
linspace = np.linspace(0, 1, 5) # 5 values between 0 and 1
identity = np.eye(3) # 3×3 identity matrix
randoms = np.random.randint(0, 10, (2, 3)) # Random integers 0–9
Quick Reference: Array Creation
| Function | Description | Example |
|---|---|---|
np.array() | Convert list to ndarray | np.array([1,2,3]) |
np.zeros() | Create zeros | np.zeros((3,3)) |
np.ones() | Create ones | np.ones((2,4)) |
np.arange() | Range with step | np.arange(0,10,2) |
np.linspace() | Evenly spaced | np.linspace(0,1,5) |
np.eye() | Identity matrix | np.eye(3) |
np.empty() | Uninitialized memory | np.empty((2,3)) |
np.random.rand() | Uniform random | np.random.rand(2,2) |
🔍 3. Array Properties and Attributes
Each NumPy array exposes useful metadata.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim) # 2 dimensions
print(arr.shape) # (2, 3)
print(arr.size) # 6 elements
print(arr.dtype) # int64
print(arr.itemsize) # Bytes per element
print(arr.nbytes) # Total memory usage
🧠 All NumPy arrays are typed — mixing floats and ints will upcast automatically (e.g.
int→float).
🔢 4. Array Operations (Vectorization)
NumPy automatically applies operations element-wise — known as vectorization.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [4 10 18]
print(a ** 2) # [1 4 9]
print(a / 2) # [0.5 1. 1.5]
Comparison and Logical Operations
arr = np.array([10, 20, 30, 40])
print(arr > 25) # [False False True True]
print(np.logical_and(arr > 10, arr < 40))
🧮 5. Matrix and Linear Algebra Operations
NumPy supports advanced linear algebra directly.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B) # Matrix multiplication
print(np.dot(A, B)) # Same as A @ B
print(np.transpose(A)) # Transpose
print(np.linalg.inv(A))# Inverse
print(np.linalg.det(A))# Determinant
🧠 6. Statistical and Aggregate Functions
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.sum()) # 21
print(arr.mean()) # 3.5
print(arr.std()) # Standard deviation
print(arr.min(axis=0)) # Column-wise min
print(arr.max(axis=1)) # Row-wise max
Use
axis=0for columns andaxis=1for rows.
🔄 7. Reshaping and Transposing
You can change the shape of an array without copying its data.
matrix = np.arange(12).reshape(3, 4)
print(matrix.shape) # (3, 4)
print(matrix.reshape(4, 3))
print(matrix.flatten()) # 1D copy
print(matrix.ravel()) # 1D view (no copy)
print(matrix.T) # Transposed
Shape Diagram
Original (3×4)
[[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]]
Reshaped (4×3)
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9,10,11]]
📈 8. Broadcasting Explained
Broadcasting allows operations between arrays of different shapes by expanding smaller arrays automatically.
matrix = np.array([[1, 2, 3], [4, 5, 6]])
vector = np.array([10, 20, 30])
print(matrix + vector)
Output:
[[11 22 33]
[14 25 36]]
Broadcasting Rules
- Compare dimensions from right to left.
- Dimensions are compatible if they are equal or one of them is 1.
- If incompatible, a
ValueErroroccurs.
Visual Explanation
Matrix (2×3): [[1 2 3]
[4 5 6]]
Vector (1×3): [10 20 30]
→
[[11 22 33]
[14 25 36]]
🧩 9. Combining and Splitting Arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
combined = np.vstack((a, b)) # Vertical stack
hstacked = np.hstack((a, b.T))# Horizontal stack
split = np.split(a, 2) # Split into two arrays
⚡ 10. Real-World Example — Data Normalization
data = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
mean = np.mean(data, axis=0)
std = np.std(data, axis=0)
normalized = (data - mean) / std
print(normalized)
🧾 11. Quick Reference Tables
Array Attributes
| Attribute | Description | Example Output |
|---|---|---|
ndim | Number of dimensions | 2 |
shape | Tuple of array size | (3, 4) |
size | Total number of elements | 12 |
dtype | Data type | float64 |
itemsize | Bytes per element | 8 |
Common Functions
| Category | Function | Purpose |
|---|---|---|
| Creation | np.zeros(), np.ones(), np.arange(), np.random.rand() | Create arrays |
| Math | np.add, np.multiply, np.power | Element-wise math |
| Statistics | np.mean, np.std, np.min, np.max | Aggregation |
| Shape | reshape, flatten, ravel, resize | Manipulation |
| Linear Algebra | np.dot, np.linalg.inv, np.linalg.det | Matrix operations |
🧭 12. Best Practices
✅ Use vectorized operations instead of loops.
✅ Prefer broadcasting to manual repetition.
✅ Always check dtype when mixing types.
✅ Avoid unnecessary copies with .ravel() instead of .flatten().
✅ Use axis wisely for column/row aggregation.
✅ Use np.random for reproducible test data.
🧠 Summary
| Concept | Description | Example |
|---|---|---|
| Array Creation | From lists or constructors | np.array([[1,2],[3,4]]) |
| Element-wise Ops | Fast, vectorized math | a + b, a * 2 |
| Matrix Ops | Dot, transpose, inverse | A @ B, A.T |
| Reshape | Change structure | arr.reshape(3,4) |
| Broadcasting | Align shapes | matrix + vector |
NumPy arrays turn Python into a high-performance, data-parallel computation environment.
Mastering them unlocks everything from basic math to advanced machine learning pipelines.