๐ข NumPy Arrays
NumPy arrays are faster and more compact than Python lists. They are the foundation for scientific computing.
Mastering this concept will significantly boost your Python data science skills!
๐ป Code Example:
import numpy as np # 1. Create arrays arr = np.array([10, 20, 30, 40, 50]) matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 2. Broadcasting โ apply operations without loops print(arr * 2) # [20 40 60 80 100] print(arr + 100) # [110 120 130 140 150] # 3. Slicing and indexing print(matrix[1, :]) # Second row โ [4 5 6] print(matrix[:, 2]) # Third column โ [3 6 9] print(matrix[0:2, 1:]) # Sub-matrix # 4. Statistical operations data = np.random.randint(1, 100, size=(5, 4)) print("Mean:", data.mean()) print("Std :", data.std()) print("Max :", data.max(axis=0)) # Per column max # 5. Reshape and stack flat = np.arange(12) reshaped = flat.reshape(3, 4) print(reshaped) # 6. Linear algebra A = np.array([[2, 1], [1, 3]]) b = np.array([8, 13]) x = np.linalg.solve(A, b) # Solve Ax = b print("Solution:", x) # [3. 2.] # 7. Boolean mask filtering scores = np.array([45, 82, 91, 33, 77, 60]) passing = scores[scores >= 60] print("Passing scores:", passing) # [82 91 77 60]
Keep exploring and happy coding! ๐ป