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:
import numpy as np
zeros = np.zeros((3, 4))
print("Zeros array (3x4):")
print(zeros)
ones = np.ones((2, 3))
print("\nOnes array (2x3):")
print(ones)
identity = np.eye(3)
print("\nIdentity matrix (3x3):")
print(identity)
range_arr = np.arange(0, 10, 2)
print("\nRange array:", range_arr)
linspace = np.linspace(0, 1, 5)
print("Linspace array:", linspace)
Array Indexing and Slicing
NumPy arrays support powerful indexing operations similar to Python lists, but with additional capabilities:
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60, 70, 80])
print("First element:", arr[0])
print("Last element:", arr[-1])
print("First 3:", arr[:3])
print("Last 3:", arr[-3:])
print("Middle:", arr[2:5])
print("Every other:", arr[::2])
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:
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Addition:", arr1 + arr2)
print("Multiplication:", arr1 * arr2)
print("Power:", arr1 ** 2)
print("\nMath functions:")
print("Sum:", np.sum(arr1))
print("Mean:", np.mean(arr1))
print("Max:", np.max(arr1))
print("Min:", np.min(arr1))
print("\nDot product:", np.dot(arr1, arr2))
Reshaping Arrays
You can change the shape of arrays without changing the data:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
print("Original shape:", arr.shape)
print("Array:", arr)
reshaped_2d = arr.reshape(2, 3)
print("\nReshaped to (2, 3):")
print(reshaped_2d)
reshaped_3d = arr.reshape(2, 1, 3)
print("\nReshaped to (2, 1, 3):")
print(reshaped_3d)
flattened = reshaped_2d.flatten()
print("\nFlattened:", flattened)
Boolean Indexing
NumPy allows you to select elements based on conditions:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
greater_than_5 = arr[arr > 5]
print("Elements > 5:", greater_than_5)
evens = arr[arr % 2 == 0]
print("Even numbers:", evens)
between = arr[(arr > 3) & (arr < 8)]
print("Between 3 and 8:", between)
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!