NumPy — Complete Lecture Notes 1
NumPy
Complete Lecture Notes
Numerical Python for Scientific Computing
Topics Covered
Introduction & Background • Array Creation • Indexing & Slicing • Math & Linear Algebra •
Statistics • Reshaping • File I/O • Boolean Masking
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 2
Table of Contents
Table of Contents........................................................................................................................2
1. Introduction to NumPy .............................................................................................................4
1.1 What is NumPy? ................................................................................................................4
1.2 Why NumPy Over Python Lists? ........................................................................................4
Reason 1 — Fixed Types (Less Memory Per Element) ........................................................4
Reason 2 — No Type Checking During Iteration ..................................................................4
Reason 3 — Contiguous Memory Layout.............................................................................5
1.3 Applications of NumPy.......................................................................................................5
2. Getting Started ........................................................................................................................6
2.1 Installation .........................................................................................................................6
2.2 Importing NumPy ...............................................................................................................6
3. Creating Arrays .......................................................................................................................6
3.1 From Python Lists..............................................................................................................6
3.2 Array Properties.................................................................................................................6
3.3 Specifying Data Types .......................................................................................................7
4. Array Initialisation Functions ...................................................................................................8
4.1 Code Examples .................................................................................................................8
4.2 rand vs randint — Important Difference .............................................................................9
4.3 Deep Dive — Copying Arrays ............................................................................................9
5. Indexing and Slicing ..............................................................................................................10
5.1 Single Element Access — 2-D Array ...............................................................................10
5.2 Row and Column Access .................................................................................................10
5.3 Slicing Syntax: start : end : step .......................................................................................10
5.4 3-D Array Indexing — Work Outside In ............................................................................10
5.5 Modifying Elements .........................................................................................................11
6. Element-Wise Arithmetic .......................................................................................................12
7. Linear Algebra.......................................................................................................................13
7.1 Matrix Multiplication .........................................................................................................13
7.2 Other Linear Algebra Operations .....................................................................................13
8. Statistics Functions ...............................................................................................................14
9. Reshaping and Stacking Arrays ............................................................................................15
9.1 reshape() .........................................................................................................................15
9.2 Vertical Stack — vstack().................................................................................................15
9.3 Horizontal Stack — hstack() ............................................................................................15
10. Loading Data from Files ......................................................................................................17
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 3
11. Boolean Masking and Advanced Indexing ...........................................................................18
11.1 Boolean Masks ..............................................................................................................18
11.2 [Link]() and [Link]().......................................................................................................18
11.3 Fancy Indexing with Lists ...............................................................................................18
12. Practice Problems and Solutions.........................................................................................20
12.1 Matrix Construction Challenge .......................................................................................20
12.2 Indexing Quiz.................................................................................................................20
12.3 Common Gotchas Reference ........................................................................................20
13. Quick-Reference Cheat Sheet.............................................................................................22
Creation.................................................................................................................................22
Inspection ..............................................................................................................................22
Indexing & Slicing ..................................................................................................................22
Math & Linear Algebra ...........................................................................................................23
Statistics & Transforms ..........................................................................................................23
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 4
1. Introduction to NumPy
1.1 What is NumPy?
NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It
provides:
• A powerful N-dimensional array object
• Sophisticated broadcasting functions
• Tools for integrating C/C++ and Fortran code
• Useful linear algebra, Fourier transform, and random number capabilities
• The foundation for virtually every major Python data-science library (Pandas, SciPy,
TensorFlow, etc.)
1.2 Why NumPy Over Python Lists?
The single most important reason: Speed. NumPy arrays can be 10x–100x faster than Python
lists for numerical operations. Three key reasons explain this:
Reason 1 — Fixed Types (Less Memory Per Element)
Python lists use a built-in int type that stores extra metadata per value. NumPy uses compact
fixed types:
Property Python List int NumPy int32 NumPy int16
Bytes per integer 28 bytes (object 4 bytes 2 bytes
overhead)
Stores object Yes (+8 bytes) No No
type?
Stores reference Yes (+8 bytes) No No
count?
Stores object Yes (long, 8 bytes) Yes (4 bytes) Yes (2 bytes)
value?
Type checking on Every iteration Never (fixed) Never (fixed)
iterate?
💡 TIP: Always specify the smallest dtype that fits your data (e.g. np.int16 for small integers)
to save memory and gain speed.
Reason 2 — No Type Checking During Iteration
A Python list can hold mixed types (int, float, str, bool). NumPy must iterate and type-check
each element. NumPy arrays are homogeneous — every element is the same dtype — so
iteration never pauses to check types.
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 5
Reason 3 — Contiguous Memory Layout
Python list elements are pointers scattered in memory. NumPy stores all elements in a single
contiguous block:
PYTHON LIST vs NUMPY ARRAY — Memory Layout
Python List Memory NumPy Array Memory
Pointer → [ *p1, *p2, *p3, *p4 ] [ val1 | val2 | val3 | val4 ]
Values scattered at p1, p2, p3, p4 All values side-by-side in one block
Cache misses — must jump around RAM Cache-friendly — loaded in one chunk
No SIMD vectorisation possible CPU SIMD (Single Instruction Multiple Data)
used
💡 NOTE: SIMD (Single Instruction Multiple Data): Modern CPUs can add/multiply multiple
values in a single clock cycle when data is contiguous in memory. NumPy exploits this;
Python lists cannot.
1.3 Applications of NumPy
• MATLAB replacement — matrix maths, signal processing
• Data visualisation backend (Matplotlib)
• Core of Pandas DataFrames
• Storing and processing images as pixel arrays (PNG/JPEG)
• Game boards and grids (e.g. Connect Four)
• Machine learning / deep learning — directly and as the conceptual basis of tensors in
PyTorch/TensorFlow
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 6
2. Getting Started
2.1 Installation
# Install via pip
pip install numpy
# If pip does not work, try:
pip3 install numpy
2.2 Importing NumPy
import numpy as np # 'np' is the universal convention
💡 NOTE: Always use 'np' as the alias. All NumPy documentation and community code uses
this convention — sticking to it makes your code instantly readable.
3. Creating Arrays
3.1 From Python Lists
# 1-D array (vector)
a = [Link]([1, 2, 3])
# Output: array([1, 2, 3])
# 2-D array (matrix)
b = [Link]([[9.0, 8.0, 7.0],
[6.0, 5.0, 4.0]])
# Output: array([[9., 8., 7.],
# [6., 5., 4.]])
# 3-D array (tensor)
c = [Link]([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
Visualising Array Dimensions
1-D (Vector) 2-D (Matrix) 3-D (Tensor)
[ 1 2 3 ] [ 9 8 7 ] Layer 0: [[1,2],[3,4]]
[ 6 5 4 ] Layer 1: [[5,6],[7,8]]
shape: (3,) shape: (2, 3) shape: (2, 2, 2)
3.2 Array Properties
Every NumPy array carries metadata attributes you can query at any time:
a = [Link]([1, 2, 3], dtype=np.int16)
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 7
[Link] # Number of dimensions → 1
[Link] # Tuple of sizes → (3,)
[Link] # Data type → int16
[Link] # Bytes per element → 2
[Link] # Total elements → 3
[Link] # Total bytes → 6 (= size × itemsize)
Attribute Returns Example (2×3 float64
array)
ndim int — number of axes 2
shape tuple of ints (2, 3)
dtype data-type object float64
itemsize int — bytes per element 8
size int — total elements 6
nbytes int — total bytes used 48
3.3 Specifying Data Types
[Link]([1, 2, 3]) # default int32 (or int64 on 64-bit)
[Link]([1, 2, 3], dtype=np.int16) # saves memory for small values
[Link]([1.5, 2.5], dtype=np.float32)
# Common dtypes:
# int8 int16 int32 int64
# float16 float32 float64
# bool complex64 complex128
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 8
4. Array Initialisation Functions
NumPy provides many convenience functions to create arrays without typing every value:
Function What it creates Example
[Link](shape) All zeros (float64) [Link]((3, 4))
[Link](shape) All ones (float64) [Link]((2, 3),
dtype=np.int32)
[Link](shape, val) Filled with val [Link]((2, 2), 99)
np.full_like(a, val) Same shape as a, np.full_like(a, 4)
filled with val
[Link](n) / [Link](n) n×n identity matrix [Link](3)
[Link](d0,d1,...) Uniform random [0, 1) [Link](3, 3)
[Link](low,high,size) Random integers [Link](0, 10,
[low, high) (3,3))
[Link](shape) Random [0, 1) from a [Link]([Link])
shape tuple
[Link](start,stop,step) Evenly spaced values [Link](0, 20, 2)
[Link](start,stop,n) n evenly-spaced [Link](0, 1, 5)
points
[Link](a, n, axis) Repeat array along [Link]([[1,2,3]], 3, axis=0)
axis
4.1 Code Examples
[Link]((2, 3))
# array([[0., 0., 0.],
# [0., 0., 0.]])
[Link]((4, 2, 2))
# shape (4, 2, 2) — four 2×2 matrices of 1s
[Link]((2, 2), 99)
# array([[99, 99],
# [99, 99]])
[Link](3)
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
[Link](4, 8, size=(3, 3))
# array of random ints from 4 to 7 (8 is exclusive)
[Link]([[1, 2, 3]], 3, axis=0)
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 9
# array([[1, 2, 3],
# [1, 2, 3],
# [1, 2, 3]])
4.2 rand vs randint — Important Difference
# rand — pass SEPARATE integer arguments (NOT a tuple)
[Link](4, 2) # ✅ correct
[Link]((4, 2)) # ❌ wrong — tuple inside parentheses
# randint — pass shape as keyword 'size'
[Link](0, 7, size=(3, 3)) # ✅ correct
💡 CAUTION: [Link]() takes dimensions as *args (separate integers), NOT a tuple.
[Link]() uses the keyword 'size=' for the shape. Mixing these up is a very
common beginner error!
4.3 Deep Dive — Copying Arrays
a = [Link]([1, 2, 3])
# WRONG — b is just another name for the same array
b = a
b[0] = 100
print(a) # → [100, 2, 3] ← a is also changed!
# CORRECT — make an independent copy
b = [Link]()
b[0] = 100
print(a) # → [1, 2, 3] ← a is unchanged
💡 CAUTION: Assignment ( b = a ) does NOT copy the data — both variables point to the
same memory block. Always call .copy() when you need an independent duplicate.
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 10
5. Indexing and Slicing
5.1 Single Element Access — 2-D Array
Syntax: array[ row , column ] (negative indices count from the end)
a = [Link]([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
a[1, 5] # row 1, col 5 → 13
a[1, -2] # row 1, second-to-last col → 13 (same element)
Index Map for 2×7 Array
col 0 col 1 col 2 col 3 col 4 col 5 col 6
row 0 1 2 3 4 5 6 7
row 1 8 9 10 11 12 13 14
neg idx -7 -6 -5 -4 -3 -2 -1
5.2 Row and Column Access
# Get entire row 0
a[0, :] # or a[0] → [1, 2, 3, 4, 5, 6, 7]
# Get entire column 2
a[:, 2] → [3, 10]
5.3 Slicing Syntax: start : end : step
The general slice syntax mirrors Python lists but works in every dimension independently:
# a [ row_slice , col_slice ]
# a [ start:end:step , start:end:step ]
# Get elements 2, 4, 6 from row 0 (indices 1, 3, 5)
a[0, 1:6:2] → [2, 4, 6]
# Get last 3 columns of both rows
a[:, -3:] → [[ 5, 6, 7],
[12,13,14]]
Slice a[0, 1:6:2] → elements at indices 1, 3, 5
idx 0 idx 1 idx 2 idx 3 idx 4 idx 5 idx 6
1 2 3 4 5 6 7
— ✓ selected — ✓ selected — ✓ selected —
5.4 3-D Array Indexing — Work Outside In
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 11
For higher-dimensional arrays, think of each index position as 'zooming in' one level at a time:
b = [Link]([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
# shape: (2, 2, 2)
# b[layer, row, col]
b[0, 1, 0] # layer 0 → row 1 → col 0 → 3
b[1, :, :] # entire layer 1 → [[5,6],[7,8]]
b[0, 1, :] # layer 0, row 1, all cols → [3, 4]
3-D Array — Layer × Row × Column
Layer Row Values Access Example
0 0 [1, 2] b[0,0,:] → [1,2]
0 1 [3, 4] b[0,1,0] → 3
1 0 [5, 6] b[1,0,:] → [5,6]
1 1 [7, 8] b[1,1,1] → 8
5.5 Modifying Elements
a = [Link]([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
# Change single element
a[1, 5] = 20
# Change entire column (must match shape)
a[:, 2] = [5, 5] # set col 2 to [5, 5]
a[:, 2] = 99 # broadcast: set all of col 2 to 99
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 12
6. Element-Wise Arithmetic
NumPy arrays support all standard operators element-by-element. The operation is applied to
every element simultaneously — no loops needed:
a = [Link]([1, 2, 3, 4])
a + 2 # → [3, 4, 5, 6] add scalar to every element
a - 2 # → [-1, 0, 1, 2]
a * 2 # → [2, 4, 6, 8]
a / 2 # → [0.5, 1.0, 1.5, 2.0]
a ** 2 # → [1, 4, 9, 16] element-wise power
# In-place (modifies array directly)
a += 2 # a is now [3, 4, 5, 6]
# Array + Array (must have same shape or be broadcastable)
b = [Link]([1, 0, 1, 0])
a + b # → [4, 4, 6, 6]
# Universal functions (ufuncs)
[Link](a) # sine of each element
[Link](a) # cosine
[Link](a) # square root
[Link](a) # natural log
[Link](a) # e^element
💡 NOTE: NumPy ufuncs (universal functions) apply the operation to every element without a
Python for-loop. They are implemented in C and are extremely fast. See
[Link]/doc/stable/reference/[Link] for the full list.
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 13
7. Linear Algebra
7.1 Matrix Multiplication
Regular * is element-wise (requires same shape). For true matrix multiplication (dot product),
use [Link]() or the @ operator:
A = [Link]((2, 3)) # 2 rows × 3 cols, all 1s
B = [Link]((3, 2), 2) # 3 rows × 2 cols, all 2s
# Rule: A is (m×k), B must be (k×n) → result is (m×n)
# A is (2×3), B is (3×2) → result is (2×2)
[Link](A, B) # → [[6, 6], [6, 6]]
A @ B # same thing — cleaner syntax
Matrix Multiplication Rule (m×k) @ (k×n) = (m×n)
A (2 × 3) @ B (3 × 2) = C (2 × 2) ✓
[[1,1,1],[1,1,1]] [[2,2],[2,2],[2,2]] [[6,6],[6,6]]
7.2 Other Linear Algebra Operations
import [Link] as la # or use [Link] directly
C = [Link](3)
[Link](C) # Determinant → 1.0
[Link](C) # Inverse matrix
[Link](C) # Eigenvalues and eigenvectors
[Link](C) # Matrix norm
[Link](C) # Matrix rank
[Link](A, b) # Solve linear system Ax = b
💡 NOTE: For even more mathematical functions (integration, optimisation, signal
processing), explore the SciPy library which builds directly on top of NumPy.
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 14
8. Statistics Functions
NumPy has efficient built-in statistical functions that can operate across the whole array or along
a specific axis:
stats = [Link]([[1, 2, 3],
[4, 5, 6]])
[Link](stats) # Overall minimum → 1
[Link](stats) # Overall maximum → 6
[Link](stats) # Sum of all → 21
[Link](stats) # Mean → 3.5
[Link](stats) # Median → 3.5
[Link](stats) # Std deviation
[Link](stats) # Variance
# Using axis parameter
[Link](stats, axis=0) # Min of each COLUMN → [1, 2, 3]
[Link](stats, axis=1) # Min of each ROW → [1, 4]
[Link](stats, axis=0) # Col sums → [5, 7, 9]
[Link](stats, axis=1) # Row sums → [6, 15]
Understanding Axis Direction
Axis Direction Think of it as [Link] example
(2×3 array)
axis=0 Down each column ↓ Collapse rows → result [5, 7, 9]
has shape (3,)
axis=1 Across each row → Collapse cols → result [6, 15]
has shape (2,)
None (default) All elements Single scalar 21
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 15
9. Reshaping and Stacking Arrays
9.1 reshape()
Change the shape of an array without changing its data. The total number of elements must
stay the same:
before = [Link]([[1, 2, 3, 4],
[5, 6, 7, 8]])
# shape: (2, 4) → 8 elements total
[Link]((8, 1)) # → 8 rows, 1 col
[Link]((4, 2)) # → 4 rows, 2 cols
[Link]((2, 2, 2))# → 3-D 2×2×2
[Link]((1, 8)) # → 1 row, 8 cols
# WRONG — 8 elements cannot fit in a (2×3) = 6 element shape
[Link]((2, 3)) # ❌ ValueError
9.2 Vertical Stack — vstack()
Stack arrays on top of each other. Column counts must match:
v1 = [Link]([1, 2, 3, 4])
v2 = [Link]([5, 6, 7, 8])
[Link]([v1, v2])
# array([[1, 2, 3, 4],
# [5, 6, 7, 8]])
# Stack more than two
[Link]([v1, v2, v2, v1])
# 4-row matrix
9.3 Horizontal Stack — hstack()
Stack arrays side by side. Row counts must match:
h1 = [Link]((2, 4))
h2 = [Link]((2, 2))
[Link]([h1, h2])
# array([[1., 1., 1., 1., 0., 0.],
# [1., 1., 1., 1., 0., 0.]])
# shape: (2, 6)
vstack vs hstack — Visual Comparison
vstack (adds rows) hstack (adds columns)
[A] → [A] [A | B] → wider array
[B] [B] Row count stays the same
Column count must match Row count must match
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 16
Result: more rows Result: more columns
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 17
10. Loading Data from Files
NumPy can read tabular text files (CSV, TSV, etc.) directly into an array:
# [Link] (comma-separated):
# 1, 13, 21, 11, 196, 75, 4
# 4, 13, 6, 2, 360, 88, 7
file_data = [Link]('[Link]', delimiter=',')
# → float64 array by default
# Convert dtype to integers
file_data = file_data.astype('int32')
# Save array to disk
[Link]('my_array.npy', file_data) # binary format
[Link]('[Link]', file_data, delimiter=',')
# Load back
arr = [Link]('my_array.npy')
💡 NOTE: genfromtxt automatically infers float64. Use .astype() to convert to a more memory-
efficient type like int32 after loading.
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 18
11. Boolean Masking and Advanced Indexing
11.1 Boolean Masks
Applying a comparison operator to an array returns a boolean array of the same shape:
a = [Link]([[1, 13, 21],
[4, 6, 196]])
a > 50
# array([[False, False, True],
# [False, False, True]])
# Use mask to filter: returns 1-D array of matching values
a[a > 50] # → [21, 196]
# Multiple conditions — use & (and), | (or), ~ (not)
a[(a > 10) & (a < 100)] # → [13, 21]
a[~(a > 50)] # → [1, 13, 4, 6] (values NOT > 50)
💡 CAUTION: Use & and | (bitwise), NOT 'and'/'or' (Python keywords). Wrap each condition
in parentheses: (a > 10) & (a < 100)
11.2 [Link]() and [Link]()
a = [Link]([[1, 13, 21],
[4, 6, 196]])
# any: True if AT LEAST ONE element satisfies condition
[Link](a > 50, axis=0) # column-wise → [False, False, True]
[Link](a > 50, axis=1) # row-wise → [True, True]
# all: True if EVERY element satisfies condition
[Link](a > 0, axis=0) # all positive? → [True, True, True]
[Link](a > 50, axis=0) # all > 50? → [False, False, False]
11.3 Fancy Indexing with Lists
Pass a list of indices to select specific elements in any order:
a = [Link]([10, 20, 30, 40, 50, 60, 70, 80, 90])
a[[1, 2, 8]] # → [20, 30, 90] (indices 1, 2, 8)
# 2-D fancy indexing
m = [Link]([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11,12, 13, 14, 15],
[16,17, 18, 19, 20]])
# Select elements at (row0,col1), (row1,col2), (row2,col3)
m[[0, 1, 2], [1, 2, 3]] # → [2, 8, 14]
# Select rows 0,2 and columns 1,3
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 19
m[np.ix_([0, 2], [1, 3])]
# → [[ 2, 4],
# [12, 14]]
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 20
12. Practice Problems and Solutions
12.1 Matrix Construction Challenge
Build the following 5×5 matrix using NumPy functions (do NOT type all values manually):
# Target output:
# [[1, 1, 1, 1, 1],
# [1, 0, 0, 0, 1],
# [1, 0, 9, 0, 1],
# [1, 0, 0, 0, 1],
# [1, 1, 1, 1, 1]]
Solution:
# Step 1: All-ones shell
output = [Link]((5, 5), dtype=int)
# Step 2: Inner 3×3 zeros block
z = [Link]((3, 3), dtype=int)
z[1, 1] = 9 # Place 9 in the centre
# Step 3: Embed inner block
output[1:-1, 1:-1] = z
print(output)
12.2 Indexing Quiz
Given this 4×5 matrix m, answer each question:
m = [Link]([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
Question Answer Explanation
Rows 1–2, Cols 0–1 m[1:3, 0:2] Slice rows 1 and 2; slice cols 0
and 1
Elements at m[[0,1,2,3],[1,2,3,4]] Fancy indexing: list of rows, list
(0,1),(1,2),(2,3),(3,4) of cols
Rows 0,2,3 and Cols 1 m[np.ix_([0,2,3], range(1,5))] np.ix_ creates an open mesh
onwards for 2-D fancy index
12.3 Common Gotchas Reference
Mistake Symptom Fix
b = a (no copy) Changing b also changes a b = [Link]()
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 21
[Link]((3,3)) TypeError: argument must be [Link](3, 3)
int
a * b for matrix mult. Element-wise, not matmul [Link](a,b) or a @ b
(a>5) and (a<10) ValueError: ambiguous truth (a>5) & (a<10)
value
wrong reshape size Cannot reshape (m,n) into (p,q) Ensure m*n == p*q
a[1][2] for 2-D array Works but slow (creates copy) a[1, 2] (preferred)
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 22
13. Quick-Reference Cheat Sheet
Creation
Code Result
[Link]([1,2,3]) 1-D array from list
[Link]((m,n)) m×n matrix of 0s
[Link]((m,n)) m×n matrix of 1s
[Link]((m,n), v) m×n matrix filled with v
[Link](n) n×n identity matrix
[Link](s,e,step) Range array
[Link](s,e,n) n evenly spaced points
[Link](m,n) Uniform random [0,1)
[Link](a,b,size=(m,n)) Random ints [a,b)
Inspection
Code Returns
[Link] Number of dimensions
[Link] Tuple of dimension sizes
[Link] Data type
[Link] Bytes per element
[Link] Total bytes
[Link] Total number of elements
Indexing & Slicing
Code Meaning
a[i, j] Element at row i, col j
a[i, :] or a[i] Entire row i
a[:, j] Entire column j
a[r1:r2, c1:c2] Sub-matrix slice
a[::2, ::2] Every other row & col
a[-1, -1] Last row, last col
a[a > v] All elements > v
[Link] | Complete Study Notes
NumPy — Complete Lecture Notes 23
a[[0,2], [1,3]] Fancy index: (0,1) and (2,3)
Math & Linear Algebra
Code Meaning
a + b Element-wise add (or scalar)
a * b Element-wise multiply
a @ b / [Link](a,b) Matrix multiplication
[Link](a) Determinant
[Link](a) Inverse matrix
[Link](a) Eigenvalues & vectors
[Link]/cos/exp/log(a) Element-wise ufuncs
Statistics & Transforms
Code Meaning
[Link]/max(a, axis=k) Min/max along axis k
[Link](a, axis=k) Sum along axis k
[Link]/median/std/var(a) Statistical summaries
[Link](new_shape) Reshape without data change
[Link]([a, b]) Stack vertically (more rows)
[Link]([a, b]) Stack horizontally (more cols)
[Link](file, delimiter=',') Load CSV into array
[Link]() True independent copy
NumPy Official Docs: [Link]/doc/stable | Lecture notes compiled from video transcript
[Link] | Complete Study Notes