![]() |
|
NumPy Cheat Sheet — Science Coding Edition - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Publications & Research (https://theluminarchive.co.uk/forumdisplay.php?fid=12) +---- Forum: Educational Resources (https://theluminarchive.co.uk/forumdisplay.php?fid=45) +---- Thread: NumPy Cheat Sheet — Science Coding Edition (/showthread.php?tid=82) |
NumPy Cheat Sheet — Science Coding Edition - Leejohnston - 11-13-2025 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([Zeros & ones: Code: np.zeros(5)Range of values: Code: np.arange(0, 10, 2) # 0,2,4,6,8Even 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])Scalar operations: Code: a + 10 # [11, 12, 13]----------------------------------------------------------------------- 4. Useful Functions Code: np.sum(a)----------------------------------------------------------------------- 5. Indexing & Slicing Select elements like Python lists: Code: a = np.array([10, 20, 30, 40])2D indexing: Code: b = np.array([----------------------------------------------------------------------- 6. Shape & Reshaping Code: a.shapeExample: Code: x = np.arange(6) # [0 1 2 3 4 5]----------------------------------------------------------------------- 7. Matrix Operations (Important for Physics & AI) Matrix multiplication: Code: np.dot(A, B)or Code: A @ BTranspose: Code: A.TInverse (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–1Random 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. |