Mastering NumPy

An interactive infographic for the 80/20 of numerical computing in Python.

NumPy Master Reference Table

Your quick lookup guide. Click any function to go directly to the official NumPy documentation.

Direct Links to Documentation

NumPy Reference Manual → The top-level page for all functions and modules.

Mathematical Functions Routines → Detailed section on trigonometry, exponents, logs, rounding, etc.

Important Functions (Supporting Roles)

Frequently used to support the critical functions.

abs()add()multiply()power()sqrt()sin()cos()argmax()argmin()cumsum()flatten()ravel()hstack()vstack()median()percentile()unique()isnan()isfinite()

Utility Functions (Specialized Tools)

Valuable for edge cases and specific applications.

all()any()ceil()floor()copy()corrcoef()eye()diag()full()histogram()interp()polyfit()roll()squeeze()savez()

Recommended Learning Path

Follow this path for maximum return on your learning investment.

1

Array Creation

array, arange, linspace, zeros, ones

2

Basic Operations

sum, mean, max, min, add, multiply

3

Manipulation

reshape, transpose, concatenate

4

Advanced Topics

dot, matmul, where, I/O

Full A-Z Quick Reference

A searchable list of all covered NumPy functions.

Function Description

*(Note: This is a subset; NumPy has ~500+ functions total.)*

The Critical 20 Functions (80% of Use Cases)

Master these functions to handle the vast majority of your numerical tasks.

1. Array Creation & Initialization

np.array()

The fundamental way to create an array from a Python list or tuple.

arr = np.array([[1, 2], [3, 4]])

Visualization:

[[1,2],[3,4]]

Python List

1
2
3
4

np.arange()

Create an array with evenly spaced values within a given interval (by step).

arr = np.arange(0, 10, 2)

Visualization:

0
2
4
6
8

np.linspace()

Create an array with a specific number of evenly spaced points.

arr = np.linspace(0, 10, 5)

Visualization:

0.0
2.5
5.0
7.5
10.0

np.zeros() / ones()

Create efficiently initialized placeholder arrays of a given shape.

zeros_arr = np.zeros((2, 3))

Visualization (zeros):

0
0
0
0
0
0

np.random.rand()

Generate arrays of random numbers (e.g., from a uniform distribution).

rand_arr = np.random.rand(2, 3)

Visualization:

0.12
0.98
0.54
0.33
0.67
0.09

2. Array Manipulation & Shaping

np.reshape()

Change the shape of an array without changing its data.

arr = np.arange(6).reshape((2, 3))

Visualization:

0
1
2
3
4
5
0
1
2
3
4
5

np.concatenate()

Join a sequence of arrays along an existing axis.

a = np.array([[1],[2]])
b = np.array([[3],[4]])
np.concatenate((a, b), axis=0)

Visualization (axis=0):

1
2
3
4
1
2
3
4

np.transpose() or .T

Swap the axes of an array (rows become columns and vice-versa).

arr = np.array([[1,2],[3,4]])
arr.T

Visualization:

1
2
3
4
1
3
2
4

3. Statistics, Math & Logic

sum() / mean() / max()

Calculate aggregate statistics across the entire array or an axis.

arr = np.array([1, 5, 2, 8])
arr.sum() # Result: 16

Visualization (sum):

1
5
2
8
16

np.dot()

Perform dot product of two arrays (essential for linear algebra).

a = np.array([1, 2])
b = np.array([3, 4])
np.dot(a, b) # Result: 11

Visualization: (1*3) + (2*4)

1
2
3
4
11

np.matmul()

Perform matrix product of two arrays.

a = np.ones((2,3))
b = np.ones((3,2))
np.matmul(a,b)

Visualization:

2x3
...
X
3x2
...
2x2
...

np.sort()

Return a sorted copy of an array.

arr = np.array([5, 1, 8, 2])
np.sort(arr)

Visualization:

5
1
8
2
1
2
5
8

np.where()

Return elements chosen from x or y depending on a condition.

arr = np.arange(5)
np.where(arr > 2, 'big', 'small')

Visualization:

0
1
2
3
4
s
s
s
b
b

4. Data Input/Output

np.loadtxt()

Load data from a text file, with each row as a new array entry.

data = np.loadtxt('data.txt')

Visualization:

data.txt

1
2
3
4

np.save()

Save an array to a binary file in NumPy `.npy` format.

arr = np.array([1,2,3])
np.save('my_array.npy', arr)

Visualization:

1
2
3

my_array.npy

Why Are These Critical?

Core Functionality: They handle array creation, math, statistics, and I/O — the absolute bedrock of most NumPy use cases.

Broad Application: These functions are indispensable in data science, machine learning, and scientific computing. For example, np.dot() and np.matmul() are the engines behind neural networks and other advanced algorithms.

The Important 30 Functions (Supporting Roles)

Frequently used to support critical functions and handle specialized tasks.

1. Element-wise & Mathematical Operations

add() / sqrt() / power()

Perform element-wise mathematical operations on arrays.

arr = np.array([1, 4, 9])
np.sqrt(arr)

Visualization (sqrt):

1
4
9
1.
2.
3.

log() / exp()

Compute element-wise natural logarithm or exponential.

arr = np.array([1, 2, 3])
np.exp(arr)

Visualization (exp):

1
2
3
2.71
7.38
20.08

sin() / cos()

Apply trigonometric functions to each element in an array.

arr = np.array([0, np.pi/2])
np.sin(arr)

Visualization (sin):

0
1.57
0
1

2. Shaping, Stacking & Splitting

flatten() / ravel()

Collapse a multi-dimensional array into a single dimension.

arr = np.array([[1,2],[3,4]])
arr.flatten()

Visualization:

1
2
3
4
1
2
3
4

hstack() / vstack()

Stack arrays together either horizontally or vertically.

a = np.array([1,2])
b = np.array([3,4])
np.hstack((a,b))

Visualization (hstack):

1
2
3
4
1
2
3
4

np.split()

Split an array into multiple sub-arrays.

arr = np.arange(6)
np.split(arr, 3)

Visualization:

0
1
2
3
4
5
0
1
2
3
4
5

np.tile()

Construct a new array by repeating an existing one.

arr = np.array([1, 2])
np.tile(arr, 3)

Visualization:

1
2
1
2
1
2
1
2

np.diag()

Extract a diagonal or construct a diagonal array.

arr = np.arange(9).reshape((3,3))
np.diag(arr)

Visualization:

0
1
2
3
4
5
6
7
8
0
4
8

np.meshgrid()

Create coordinate matrices from coordinate vectors for plotting.

x = [0,1]; y = [0,1]
xv, yv = np.meshgrid(x, y)

Visualization:

x=[0,1]
y=[0,1]
xv = [[0,1],[0,1]]
0
1
0
1
yv = [[0,0],[1,1]]
0
0
1
1

3. Statistics, Analysis & Uniqueness

argmax() / argmin()

Find the indices (positions) of the maximum or minimum values.

arr = np.array([5, 1, 9, 2])
arr.argmax() # Result: 2

Visualization (argmax):

5
1
9
2
0123
2

cumsum() / prod()

Calculate the cumulative sum or product of array elements.

arr = np.array([1, 2, 3, 4])
arr.cumsum()

Visualization (cumsum):

1
2
3
4
1
3
6
10

std() / var()

Compute the standard deviation or variance of the data.

arr = np.array([1, 2, 3, 4])
arr.std() # Result: 1.118

Visualization (std):

1
2
3
4
1.118

np.percentile()

Compute the q-th percentile of the data along a specified axis.

arr = np.arange(1, 101)
np.percentile(arr, 50) # Median

Visualization (50th Percentile):

1
...
50
...
100
50.0

np.unique()

Find the sorted unique elements of an array.

arr = np.array([1, 2, 2, 3, 1])
np.unique(arr)

Visualization:

1
2
2
3
1
1
2
3

Why Are These Important?

Enhance Critical Functions: They provide the building blocks for more complex operations. For example, std() (standard deviation) relies on the result of mean().

Enable Specialized Tasks: These functions are crucial for specific domains. For instance, meshgrid() is essential for creating 2D and 3D plots, and unique() is vital for data cleaning and analysis.

The Utility Functions (Specialized Tools)

Valuable for edge cases, convenience, or specific applications.

1. Boolean, Checks & Rounding

all() / any()

Check if all or any elements in an array evaluate to True.

arr = np.array([True, True, False])
arr.any() # Result: True

Visualization (any):

T
T
F
True

isnan() / isinf()

Create a boolean mask to identify NaN (Not a Number) or infinity.

arr = np.array([1, np.nan, 3])
np.isnan(arr)

Visualization (isnan):

1
NaN
3
F
T
F

ceil() / floor() / round()

Perform element-wise rounding to the ceiling, floor, or nearest integer.

arr = np.array([1.1, 2.9])
np.ceil(arr)

Visualization (ceil):

1.1
2.9
2.
3.

2. Data Generation & Manipulation

full()

Create a new array of a given shape, filled with a specific value.

arr = np.full((2, 3), 7)

Visualization:

Shape: (2,3)
Fill: 7
7
7
7
7
7
7

repeat()

Repeat elements of an array a given number of times.

arr = np.array([1, 2])
np.repeat(arr, 3)

Visualization:

1
2
1
1
1
2
2
2

roll()

"Roll" array elements along an axis; elements that roll off one end reappear on the other.

arr = np.arange(4)
np.roll(arr, 1)

Visualization:

0
1
2
3
3
0
1
2

squeeze()

Remove single-dimensional entries from the shape of an array.

arr = np.array([[[1, 2, 3]]])
np.squeeze(arr)

Visualization:

...

Shape: (1, 1, 3)

...

Shape: (3,)

average()

Compute the weighted average along a specified axis.

data = [10, 20]
weights = [1, 3]
np.average(data, weights=weights)

Visualization: (10*1 + 20*3) / 4

10
20

Weights: [1, 3]

17.5

corrcoef()

Return Pearson product-moment correlation coefficients.

x = [1,2,3]; y = [3,5,7]
np.corrcoef(x, y)

Visualization:

x=[1,2,3]
y=[3,5,7]

Correlation Matrix

1.
1.
1.
1.

3. Advanced Analysis & Interop

histogram()

Compute the histogram of a set of data.

data = [1, 2, 1, 3, 1]
np.histogram(data, bins=3)

Visualization:

...

interp()

One-dimensional linear interpolation.

np.interp(2.5, [1,3], [5,9])

Visualization:

x=2.5
xp=[1, 3]
fp=[5, 9]
7.0

polyfit()

Fit a polynomial to a set of data points.

x=[1,2,3]; y=[2,4.1,5.9]
np.polyfit(x, y, 1)

Visualization:

Coefficients
[slope, intercept]

Why Are These "Utility"?

Niche Use Cases: Many of these functions solve specific problems that don't appear in every project. For example, polyfit() is mainly for curve fitting, and interp() is for interpolation tasks.

Convenience & Optimization: Some are helpers that offer a convenient syntax (ceil(), floor()) or are used for performance optimization in rare cases, like interfacing with low-level memory buffers (frombuffer()).

Foundations: Various Ways to Create a NumPy Array

Different methods to initialize arrays for any situation.

1. From a Python List

The most direct way: convert existing Python lists or tuples.

np.array([[1, 2], [3, 4]])

Visualization:

[[1,2],[3,4]]

Python List

1
2
3
4

2. As a Sequence (arange)

Create an array with values from a start to an end point with a defined step.

np.arange(0, 10, 2)

Visualization:

0
2
4
6
8

3. As a Sequence (linspace)

Create an array with a specific number of points spaced evenly between start and end.

np.linspace(0, 1, 5)

Visualization:

0.0
0.25
0.5
0.75
1.0

4. As Placeholders

Create arrays of a specified shape filled entirely with 0s or 1s.

np.ones((2, 4))

Visualization (ones):

1
1
1
1
1
1
1
1

5. With a Constant Value

Create an array of a specific shape filled with any constant value you choose.

np.full((2, 3), 99)

Visualization:

99
99
99
99
99
99

6. An Identity Matrix

Create a 2D square matrix with 1s on the main diagonal and 0s elsewhere.

np.eye(4)

Visualization:

1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1

Power Tools: Mastering Array Indexing

How to select, slice, and filter data with precision.

1. Basic Slicing

Extracting subarrays using `[start:stop:step]` notation for each dimension.

arr = np.arange(16).reshape(4, 4)
# Get rows 1 and 2, and columns 2 and 3
sub_array = arr[1:3, 2:4]

Visualization:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
6
7
10
11

2. Fancy Indexing (Integer Arrays)

Using arrays of integers to select rows, columns, or specific elements in any order.

arr = np.arange(16).reshape(4, 4)
# Get rows 3 and 1 in that specific order
rows = arr[[3, 1]]

Visualization:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
12
13
14
15
4
5
6
7

3. Boolean Indexing (Masking)

Filtering an array by creating a boolean "mask" based on a condition.

arr = np.arange(9).reshape(3, 3)
# Select elements greater than 5
filtered = arr[arr > 5]

Visualization:

0
1
2
3
4
5
6
7
8
6
7
8

4. Combining Indexing Types

Mixing basic slicing with fancy indexing for powerful, complex selections.

arr = np.arange(16).reshape(4, 4)
# From the first 2 rows, get cols 3 and 0
combo = arr[0:2, [3, 0]]

Visualization:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3
0
7
4

The Art of Extraction: Mastering Array Slicing

Using the `start:stop:step` pattern to carve out any piece of data you need.

1. Basic 1D Slicing

The core `start:stop:step` syntax. Omitting `start` defaults to 0, `stop` to the end, and `step` to 1.

arr = np.arange(8)
# Get elements from index 1 up to (not including) 7, in steps of 2
sub_array = arr[1:7:2]

Visualization:

0
1
2
3
4
5
6
7
1
3
5

2. Multi-Dimensional Slicing

Apply slicing independently across each dimension, separated by a comma.

arr = np.arange(16).reshape(4, 4)
# All rows from index 2 onwards, and columns from 0 up to 2
sub_array = arr[2:, :2]

Visualization:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
8
9
12
13

3. Modifying with Slices (Views)

Slices are views, not copies. Assigning to a slice modifies the original array.

arr = np.arange(6)
# Set elements from index 2 to 4 to a new value
arr[2:5] = 99

Visualization:

0
1
2
3
4
5
0
1
99
99
99
5

4. Combining Indexing & Slicing

Mix integer indexing with slicing to select specific rows/columns and ranges simultaneously.

arr = np.arange(16).reshape(4, 4)
# From row 2, get all columns from index 1 onwards
sub_array = arr[2, 1:]

Visualization:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
9
10
11

Unleashing the Calculator: Mathematical Operations

From simple arithmetic to complex linear algebra, the core of NumPy's power.

1. Basic Arithmetic: Operator vs. Universal Function

NumPy lets you use standard math operators for convenience. It also provides "universal functions" (ufuncs) like `np.add` which offer more flexibility (e.g., specifying output arrays) and are recommended for writing clear, robust code.

Addition & Subtraction

`+` vs `np.add` and `-` vs `np.subtract`

a = np.array([5, 8]); b = np.array([2, 3])
add_result = a + b       # or np.add(a, b)
sub_result = a - b       # or np.subtract(a, b)

Visualization (Addition):

5
8
+
2
3
7
11

Multiplication & Division

`*` vs `np.multiply` and `/` vs `np.divide`

a = np.array([6, 10]); b = np.array([2, 5])
mul_result = a * b      # or np.multiply(a, b)
div_result = a / b      # or np.divide(a, b)

Visualization (Multiplication):

6
10
*
2
5
12
50

Power & Remainder

`**` vs `np.power` and `%` vs `np.remainder`

a = np.array([2, 10]); b = np.array([3, 3])
pow_result = a ** b     # or np.power(a, b)
rem_result = a % b      # or np.remainder(a, b)

Visualization (Power):

2
10
**
3
3
8
1000

2. Rounding & Approximation

np.round / np.around

Round to a given number of decimals (to nearest even for .5 cases).

arr = np.array([1.2, 3.7, 5.5])
result = np.round(arr)

Visualization:

1.2
3.7
5.5
1.
4.
6.

np.floor & np.ceil

Round down to the nearest integer (`floor`) or up (`ceil`).

arr = np.array([1.2, 3.7])
floor_res = np.floor(arr) # [1., 3.]
ceil_res = np.ceil(arr)   # [2., 4.]

Visualization (floor):

1.2
3.7
1.
3.

np.trunc / np.fix

Discard the fractional part, rounding towards zero.

arr = np.array([-1.7, 1.2])
result = np.trunc(arr)

Visualization:

-1.7
1.2
-1.
1.

3. Exponential & Logarithmic

np.exp & np.log

Base-e exponential (`e^x`) and the natural logarithm.

arr = np.array([1, 2])
exp_res = np.exp(arr)  # [2.718, 7.389]
log_res = np.log(exp_res) # [1., 2.]

Visualization (exp):

1
2
2.72
7.39

np.log10 & np.log2

Logarithms for base 10 and base 2.

arr = np.array([10, 1000])
log10_res = np.log10(arr) # [1., 3.]

Visualization (log10):

10
1000
1.
3.

np.expm1 & np.log1p

`exp(x)-1` and `log(1+x)`. Give better precision for small `x`.

small_x = 1e-9
# More precise than np.log(1 + small_x)
result = np.log1p(small_x)

Benefit: High Precision

Prevents loss of precision when `x` is close to zero.

4. Trigonometric & Angle Functions

np.sin / cos / tan

Standard trig functions. Input is assumed to be in radians.

rads = np.array([0, np.pi/2])
sin_res = np.sin(rads) # [0., 1.]

Visualization (sin):

0
π/2
0.
1.

np.deg2rad & np.rad2deg

Conveniently convert between degrees and radians.

degrees = 180
radians = np.deg2rad(degrees)

Visualization:

180°
3.14 (π)

np.hypot / arctan2

`hypot(x,y)` is `sqrt(x²+y²)`. `arctan2(y,x)` is `atan(y/x)` but handles quadrants correctly.

base = 3; height = 4
hypotenuse = np.hypot(base, height)

Visualization (hypot):

3
4
5.

5. Aggregations & Statistical Functions

np.sum & np.prod

Compute the sum or product of all elements in an array.

arr = np.array([1, 2, 3, 4])
total = np.sum(arr) # 10

Visualization (sum):

1
2
3
4
10

np.cumsum & np.cumprod

Compute the cumulative sum or product.

arr = np.array([1, 2, 3, 4])
cumulative = np.cumsum(arr)

Visualization (cumsum):

1
2
3
4
1
3
6
10

np.mean / median / std

Calculate mean (average), median (middle value), and standard deviation.

arr = np.array([1, 2, 3, 10])
avg = np.mean(arr) # 4.0
med = np.median(arr) # 2.5

Visualization (median):

1
2
3
10
2.5

np.min / max / ptp

Find the minimum, maximum, or peak-to-peak (`max-min`) range.

arr = np.array([5, 1, 9, 2])
peak_range = np.ptp(arr) # 9-1=8

Visualization (ptp):

5
1
9
2
8

np.argmin / argmax

Find the index (position) of the minimum or maximum value.

arr = np.array([5, 1, 9, 2])
max_idx = np.argmax(arr) # 2

Visualization (argmax):

5
1
9
2
0123
2

np.percentile / quantile

Find values at a specific percentile (0-100) or quantile (0-1).

arr = np.arange(1, 101)
# Find the 75th percentile
p75 = np.percentile(arr, 75)

Visualization:

1..
..100
75.0

6. Linear Algebra

np.dot / matmul / @

Dot product and matrix multiplication. The `@` operator is shorthand for `np.matmul`.

A = np.array([[1,2],[3,4]])
B = np.eye(2)
C = A @ B

Visualization (matmul):

2x2
...
@
2x2
...
2x2
...

np.linalg.inv & det

Calculate the inverse and determinant of a square matrix.

A = np.array([[1,2],[3,4]])
determinant = np.linalg.det(A) # -2.0

Visualization (det):

1
2
3
4
-2.0

np.linalg.solve

Solve a system of linear equations, `Ax = b`, for `x`.

# 2x + y = 1; x + y = 1
A = np.array([[2,1],[1,1]])
b = np.array([1, 1])
x = np.linalg.solve(A, b) # [0., 1.]

Visualization:

A
..
x
=
b
..
x
..