DEV Community

Aditi Sharma
Aditi Sharma

Posted on

πŸš€ Day 11 of My Python Learning Journey

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 πŸ“Š

Python #NumPy #100DaysOfCode #DataScience #DevCommunity

Top comments (0)