Easy NumPy Cheat Code

Here is a quick NumPy cheat code for beginners! These 30 code snippets cover essential operations like array creation, reshaping, basic operations, and more. Copy these examples to quickly get started with NumPy!

1. Import NumPy

import numpy as np

2. Create Arrays

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

3. Array of Zeros, Ones, and Random Values

zeros = np.zeros((2, 3))  # 2x3 array of zeros
ones = np.ones((2, 3))  # 2x3 array of ones
rand = np.random.rand(2, 3)  # 2x3 array of random values

4. Arange and Linspace

arr = np.arange(0, 10, 2)  # [0, 2, 4, 6, 8]
lin = np.linspace(0, 1, 5)  # [0.  0.25  0.5  0.75  1.]

5. Reshape

reshaped = np.arange(6).reshape(2, 3)  # [[0, 1, 2], [3, 4, 5]]

6. Basic Operations

arr = np.array([1, 2, 3])
result = arr + 2  # [3, 4, 5]
dot = np.dot(arr, arr)  # 1*1 + 2*2 + 3*3 = 14

7. Matrix Multiplication

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = np.matmul(A, B)  # [[19, 22], [43, 50]]

8. Indexing and Slicing

arr = np.array([1, 2, 3, 4, 5])
sub = arr[1:4]  # [2, 3, 4]
mat = np.array([[1, 2], [3, 4]])
elem = mat[0, 1]  # 2

9. Boolean Masking

arr = np.array([1, 2, 3, 4])
mask = arr > 2  # [False, False, True, True]
filtered = arr[mask]  # [3, 4]

10. Statistical Operations

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

11. Element-Wise Operations

arr = np.array([1, 2, 3])
squared = arr ** 2  # [1, 4, 9]

12. Concatenation and Stacking

a = np.array([1, 2])
b = np.array([3, 4])
concat = np.concatenate([a, b])  # [1, 2, 3, 4]
stack = np.stack([a, b])  # [[1, 2], [3, 4]]

13. Transpose

mat = np.array([[1, 2], [3, 4]])
transposed = mat.T  # [[1, 3], [2, 4]]

14. Unique Elements

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

15. Save and Load

np.save('array.npy', arr)  # Save array
loaded = np.load('array.npy')  # Load array

16. Create Meshgrid

x = np.arange(0, 3)
y = np.arange(0, 3)
xx, yy = np.meshgrid(x, y)  
# xx = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
# yy = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]

17. Conditional Statements with np.where

arr = np.array([10, 20, 30, 40])
result = np.where(arr > 25, arr, -1)  # [ -1,  -1, 30, 40]

18. Logical Operations

arr = np.array([1, 2, 3, 4])
logical = np.logical_and(arr > 1, arr < 4)  # [False, True, True, False]

19. Broadcasting

arr = np.array([[1, 2, 3]])
broadcasted = arr + np.array([[10], [20]])  
# [[11, 12, 13], [21, 22, 23]]

20. Tile and Repeat

arr = np.array([1, 2, 3])
tiled = np.tile(arr, 2)  # [1, 2, 3, 1, 2, 3]
repeated = np.repeat(arr, 2)  # [1, 1, 2, 2, 3, 3]

21. Stack Arrays Vertically and Horizontally

arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
vstack = np.vstack([arr1, arr2])  # [[1, 2], [3, 4]]
hstack = np.hstack([arr1, arr2])  # [1, 2, 3, 4]

22. Flatten an Array

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

23. Generate Random Integers

random_integers = np.random.randint(0, 10, size=(2, 3))  
# Example: [[3, 7, 2], [9, 4, 5]]

24. Cumulative Sum

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

25. Cumulative Product

arr = np.array([1, 2, 3])
cumprod = np.cumprod(arr)  # [1, 2, 6]

26. Sorting an Array

arr = np.array([3, 1, 2])
sorted_arr = np.sort(arr)  # [1, 2, 3]

27. Find Min, Max, and Argmax

arr = np.array([3, 1, 2])
min_val = np.min(arr)  # 1
max_val = np.max(arr)  # 3
argmax = np.argmax(arr)  # Index 0 (value 3)

28. Advanced Indexing with Boolean Arrays

arr = np.array([10, 20, 30, 40])
bool_idx = arr > 25  # [False, False, True, True]
filtered = arr[bool_idx]  # [30, 40]

29. Matrix Determinant

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

30. Solve a Linear System

A = np.array([[2, 3], [1, 2]])
b = np.array([8, 5])
solution = np.linalg.solve(A, b)  # [1, 2]

Leave a Reply

Your email address will not be published. Required fields are marked *