Thread Rating:
NumPy Cheat Sheet — Science Coding Edition
#1
NumPy Cheat Sheet — Science Coding Edition

NumPy is the foundation of scientific computing in Python. 
It powers simulations, data analysis, physics calculations, AI, and large-scale maths.

This sheet gives you the essential commands and concepts every science coder needs.

-----------------------------------------------------------------------

1. Importing NumPy

The standard import:

Code:
import numpy as np

-----------------------------------------------------------------------

2. Creating Arrays

1D array: 
Code:
a = np.array([1, 2, 3, 4])

2D array (matrix): 
Code:
b = np.array([
    [1, 2],
    [3, 4]
])

Zeros & ones: 
Code:
np.zeros(5)
np.ones((3,3))

Range of values: 
Code:
np.arange(0, 10, 2)  # 0,2,4,6,8

Even spacing: 
Code:
np.linspace(0, 1, 5)

-----------------------------------------------------------------------

3. Array Operations (Fast Maths)

NumPy does maths element-by-element:

Code:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

a + b  # [5, 7, 9]
a * b  # [4, 10, 18]
a ** 2  # [1, 4, 9]

Scalar operations:

Code:
a + 10      # [11, 12, 13]
a * 3      # [3, 6, 9]

-----------------------------------------------------------------------

4. Useful Functions

Code:
np.sum(a)
np.mean(a)
np.max(a)
np.min(a)
np.std(a)      # standard deviation
np.sqrt(a)
np.exp(a)
np.log(a)

-----------------------------------------------------------------------

5. Indexing & Slicing

Select elements like Python lists:

Code:
a = np.array([10, 20, 30, 40])

a[0]        # 10
a[1:3]      # [20, 30]
a[-1]      # 40

2D indexing:

Code:
b = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

b[0, 1]    # 2
b[:, 0]    # first column → [1, 4]
b[1, :]    # second row → [4, 5, 6]

-----------------------------------------------------------------------

6. Shape & Reshaping

Code:
a.shape
a.reshape(2, 3)

Example:

Code:
x = np.arange(6)        # [0 1 2 3 4 5]
x.reshape(2, 3)        # 2 rows, 3 columns

-----------------------------------------------------------------------

7. Matrix Operations (Important for Physics & AI)

Matrix multiplication:

Code:
np.dot(A, B)
 
or 
Code:
A @ B

Transpose: 
Code:
A.T

Inverse (if square matrix): 
Code:
np.linalg.inv(A)

Determinant: 
Code:
np.linalg.det(A)

Solve linear equations: 
Code:
np.linalg.solve(A, b)

-----------------------------------------------------------------------

8. Random Numbers (Useful in Simulations)

Code:
np.random.rand(3)        # random numbers 0–1
np.random.randn(3)        # normal distribution
np.random.randint(0, 10)  # random integer

Random arrays:

Code:
np.random.rand(2, 3)

-----------------------------------------------------------------------

9. Loading & Saving Data

Load CSV: 
Code:
data = np.loadtxt("data.csv", delimiter=",")

Save array: 
Code:
np.savetxt("output.csv", data, delimiter=",")

-----------------------------------------------------------------------

10. Common Mistakes

❌ Using lists instead of arrays for scientific maths 
✔ NumPy arrays are MUCH faster and support vectorised maths 

❌ Forgetting that * multiplies element-by-element 
✔ Use dot() or @ for matrix multiplication 

❌ Mixing shapes (e.g., 3×3 with 2×2) 
✔ Check array dimensions with .shape 

❌ Using Python math functions 
✔ Use np.sqrt(), np.log(), np.exp() for arrays 

❌ Forgetting to import NumPy 
✔ import numpy as np 

-----------------------------------------------------------------------

Summary

NumPy gives you:
• fast arrays 
• vectorised maths 
• matrix operations 
• scientific functions 
• random numbers 
• reshaping & slicing 
• data loading 

It’s the foundation of all serious scientific computing in Python.
Reply
« Next Oldest | Next Newest »


Forum Jump:


Users browsing this thread: 1 Guest(s)