NumPy Arrays: Reshaping, Broadcasting & More
After learning the basics of NumPy arrays, today I explored some powerful features that make NumPy essential in data science and even image processing.
πΉ Reshaping Arrays
Reshape changes the dimensions of an array without altering the data.
import numpy as np
arr = np.arange(6) # [0 1 2 3 4 5]
reshaped = arr.reshape(2, 3)
print(reshaped)
[[0 1 2]
[3 4 5]]
πΉ Array Shape Behavior
print(reshaped.shape) # (2, 3)
print(reshaped.ndim) # 2 (2D array)
πΉ Broadcasting
NumPy automatically expands arrays during arithmetic operations.
a = np.array([1, 2, 3])
b = 5
print(a + b) # [6 7 8]
πΉ Array Operations
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y) # [5 7 9]
print(x * y) # [ 4 10 18]
πΉ Image Manipulation (fun fact π¨)
Since images are just arrays of pixel values, NumPy can manipulate them!
from PIL import Image
img = Image.open("sample.jpg")
img_arr = np.array(img)
print(img_arr.shape) # (height, width, channels)
You can modify pixels, apply filters, or reshape images with NumPy.
β¨ Reflection
NumPy isnβt just about numbers β itβs about transforming data at scale and speed. Todayβs deep dive showed me why itβs the foundation for ML, AI, and image processing.
Next, Iβll explore even more operations with NumPy before moving to Pandas π
Top comments (0)