๐ข NumPy Concepts with Demo Programs
This blog covers essential NumPy concepts for data science and numerical computing. Each demo has syntax highlighting and a copy button for easy learning and sharing.
1. Creating NumPy Arrays
import numpy as np
# 1D array
arr1 = np.array([1, 2, 3, 4])
print("1D array:", arr1)
# 2D array
arr2 = np.array([[1, 2], [3, 4]])
print("2D array:\n", arr2)
# Using arange and linspace
arr_arange = np.arange(0, 10, 2) # start, stop, step
arr_linspace = np.linspace(0, 1, 5) # start, stop, number of points
print("arange:", arr_arange)
print("linspace:", arr_linspace)
# Array of zeros, ones, identity
zeros = np.zeros((2,3))
ones = np.ones((2,2))
identity = np.eye(3)
print("Zeros:\n", zeros)
print("Ones:\n", ones)
print("Identity:\n", identity)
2. Array Indexing and Slicing
# 1D array slicing
arr = np.array([10, 20, 30, 40, 50])
print("Original array:", arr)
print("Slice [1:4]:", arr[1:4])
print("Every 2nd element:", arr[::2])
# 2D array indexing
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("Element at (0,2):", arr2d[0,2])
print("First row:", arr2d[0])
print("Second column:", arr2d[:,1])
3. Array Operations
a = np.array([1,2,3])
b = np.array([4,5,6])
# Element-wise operations
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
print("a / b =", a / b)
# Universal functions
print("sqrt(a):", np.sqrt(a))
print("exp(a):", np.exp(a))
print("sin(a):", np.sin(a))
4. Array Aggregations
arr = np.array([[1,2,3],[4,5,6]])
print("Sum:", np.sum(arr))
print("Sum axis=0 (columns):", np.sum(arr, axis=0))
print("Sum axis=1 (rows):", np.sum(arr, axis=1))
print("Mean:", np.mean(arr))
print("Max:", np.max(arr))
print("Min:", np.min(arr))
print("Std dev:", np.std(arr))
5. Reshaping and Transpose
arr = np.arange(12)
print("Original array:", arr)
reshaped = arr.reshape(3,4)
print("Reshaped (3x4):\n", reshaped)
flattened = reshaped.flatten()
print("Flattened:", flattened)
print("Transpose:\n", reshaped.T)
6. Boolean Indexing and Filtering
arr = np.array([10, 15, 20, 25, 30])
# Boolean mask
mask = arr > 20
print("Mask:", mask)
print("Filtered array:", arr[mask])
# Or directly
print("Values > 20:", arr[arr > 20])
7. Random Numbers with NumPy
# Random numbers
rand_float = np.random.rand(5) # uniform [0,1)
rand_int = np.random.randint(0, 10, size=5)
rand_norm = np.random.randn(5) # standard normal
print("Random floats:", rand_float)
print("Random integers:", rand_int)
print("Random normal:", rand_norm)
8. Linear Algebra & Dot Products
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
# Matrix multiplication
dot = np.dot(A,B)
print("Dot product:\n", dot)
# Determinant and inverse
det = np.linalg.det(A)
inv = np.linalg.inv(A)
print("Determinant:", det)
print("Inverse:\n", inv)
9. Stacking Arrays
a = np.array([1,2,3])
b = np.array([4,5,6])
# Vertical and horizontal stacking
vstacked = np.vstack([a,b])
hstacked = np.hstack([a,b])
print("VStacked:\n", vstacked)
print("HStacked:", hstacked)
10. Saving and Loading Arrays
arr = np.arange(10)
# Save to file
np.save("my_array.npy", arr)
# Load back
loaded_arr = np.load("my_array.npy")
print("Loaded array:", loaded_arr)
✅ With these 10 demos, you now have a full **NumPy cheat sheet with practical code examples**, ready for learning !
No comments:
Post a Comment