Chapter 3: NumPy & Pandas / Lesson 12

NumPy Arrays

Working with NumPy Arrays

NumPy arrays are the foundation of numerical computing in Python. Understanding how to create, manipulate, and operate on arrays is essential for machine learning. Arrays are more efficient than Python lists for mathematical operations.

In this lesson, we'll dive deeper into array operations, indexing, slicing, and mathematical functions that make NumPy powerful for ML.

Array Creation Methods

NumPy provides many ways to create arrays beyond just converting lists:

array_creation.py
import numpy as np # Zeros array zeros = np.zeros((3, 4)) print("Zeros array (3x4):") print(zeros) # Ones array ones = np.ones((2, 3)) print("\nOnes array (2x3):") print(ones) # Identity matrix identity = np.eye(3) print("\nIdentity matrix (3x3):") print(identity) # Array with range range_arr = np.arange(0, 10, 2) # Start, stop, step print("\nRange array:", range_arr) # Evenly spaced values linspace = np.linspace(0, 1, 5) # Start, stop, num_points print("Linspace array:", linspace)

Array Indexing and Slicing

NumPy arrays support powerful indexing operations similar to Python lists, but with additional capabilities:

indexing_slicing.py
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80]) # Basic indexing print("First element:", arr[0]) print("Last element:", arr[-1]) # Slicing print("First 3:", arr[:3]) print("Last 3:", arr[-3:]) print("Middle:", arr[2:5]) print("Every other:", arr[::2]) # 2D array indexing matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("\nMatrix:") print(matrix) print("Element [1,2]:", matrix[1, 2]) print("First row:", matrix[0, :]) print("Second column:", matrix[:, 1])

Array Operations

NumPy enables vectorized operations that work on entire arrays efficiently:

array_math.py
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) # Element-wise operations print("Array 1:", arr1) print("Array 2:", arr2) print("Addition:", arr1 + arr2) print("Multiplication:", arr1 * arr2) print("Power:", arr1 ** 2) # Mathematical functions print("\nMath functions:") print("Sum:", np.sum(arr1)) print("Mean:", np.mean(arr1)) print("Max:", np.max(arr1)) print("Min:", np.min(arr1)) # Dot product print("\nDot product:", np.dot(arr1, arr2))

Reshaping Arrays

You can change the shape of arrays without changing the data:

reshaping.py
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) print("Original shape:", arr.shape) print("Array:", arr) # Reshape to 2D reshaped_2d = arr.reshape(2, 3) print("\nReshaped to (2, 3):") print(reshaped_2d) # Reshape to 3D reshaped_3d = arr.reshape(2, 1, 3) print("\nReshaped to (2, 1, 3):") print(reshaped_3d) # Flatten back to 1D flattened = reshaped_2d.flatten() print("\nFlattened:", flattened)

Boolean Indexing

NumPy allows you to select elements based on conditions:

boolean_indexing.py
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Select elements greater than 5 greater_than_5 = arr[arr > 5] print("Elements > 5:", greater_than_5) # Select even numbers evens = arr[arr % 2 == 0] print("Even numbers:", evens) # Multiple conditions between = arr[(arr > 3) & (arr < 8)] print("Between 3 and 8:", between) # Modify selected elements arr[arr > 7] = 99 print("After modification:", arr)

💡 Key Concept

NumPy arrays enable vectorized operations that are much faster than Python loops. Instead of iterating through elements, operations happen on entire arrays at once, making ML computations efficient!

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...