🚀 NumPy skill that unlocks machine learning


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

Last week we introduced numpy. Created bunch of arrays, sliced them and did some math with them.

To prepare data for machine learning or any analysis, we need to learn to control their shape.

Today - Reshape flat lists into grids. Flatten 2D tables back to 1D arrays. Create arrays filled with zeros, ones, or random numbers.

⏪ Recap: See dimensions first

In:
import numpy as np
sales = np.array([120, 340, 210, 450, 380, 290])
print(sales.shape)
Out: (6,)

🔶 Now reshape above 1D array to 2D

Assume 1st 3 are sales of week 1 and last 3 are of week 2

In:
weekly_sales = np.array([50, 60, 70, 80, 90, 100])
reshaped = weekly_sales.reshape(2, 3)
print(reshaped)
Out:
[[ 50 60 70]
[ 80 90 100]]

🙃 Do reverse - 2D to 1D

Your model might need a linear feed rather than a 2D

In:
grid = np.array([[10, 20, 30], [40, 50, 60]])
flat = grid.flatten()
print(flat)
Out: [10 20 30 40 50 60]

🅾️ Create Zeroes

Use as default values unless anything else is predicted

In:
empty_slots = np.zeros(5)
print(empty_slots)
Out: [0. 0. 0. 0. 0.]

☝️ Create Ones

Can be used as default multipliers unless defined

In:
multipliers = np.ones(4)
print(multipliers)
Out: [1. 1. 1. 1.]

🤖 Generate sample (random) data

💡 We don't need the random module separately! iykyk

In:
random_data = np.random.rand(6)
print(random_data)
Out: [0.42891263 0.78546321 0.23456789 0.91234567 0.56789012 0.34567890]

🟰 How do you and I get the same random data?

Using same seed. Past Pandas Daily readers would know 😉

In:
np.random.seed(42)
data1 = np.random.rand(4)
print(data1)
Out: [0.37454012 0.95071431 0.73199394 0.59865848]

⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. You have a spreadsheet with 500 numbers. Your boss asks: "What do you see?" Most will freeze, scroll, guess. Not you! 3 Python functions to turn raw numbers into answers. No calculator, excel sheets. Just code. Statistics isn't boring math. It's how you make decisions with data. Using just the statistics module we find "center" of any dataset. Any data analyst must know the basics‼️ 🎯 Compute simple...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. TGIF! Everyone is talking about AI. Most people take courses. You're going to build it instead. Let's create a simple sentiment analyzer that reads customer reviews and classifies them as positive or negative. No machine learning libraries—just Python. 🎯 Define list of positive and negative words In: positive_words = ["good", "great", "excellent", "amazing", "love", "best"] negative_words = ["bad",...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We created arrays on Day 1. Sliced them on Day 2. Now comes the real computational power of NumPy. Doing math on entire datasets. No loops. No iteration. Just one line. Add monthly revenues across regions. Calculate profit margins. Find averages. All in seconds!! Let's dive in.. 🍃 Add/Subtract like a breeze In: import numpy as np jan_sales = np.array([120, 340, 210]) feb_sales = np.array([150, 290, 180])...