📖 Scenario: Imagine you work in a bakery that tracks daily sales of different types of bread. The sales data is collected as a single list of numbers, but you want to organize it by days and bread types to understand patterns better.
🎯 Goal: You will reshape a one-dimensional array of sales data into a two-dimensional array to see daily sales per bread type clearly.
📋 What You'll Learn
Create a NumPy array called sales with exactly 12 values representing sales.
Create a variable called days and set it to 4.
Use the reshape method on sales to create a new array called daily_sales with shape (days, 3).
Print the daily_sales array to see the reshaped data.
💡 Why This Matters
🌍 Real World
Reshaping arrays helps organize raw data into meaningful tables, like daily sales per product, making it easier to analyze and understand.
💼 Career
Data scientists often reshape data to prepare it for analysis, visualization, or machine learning models.
Progress0 / 4 steps
1
Create the sales data array
Create a NumPy array called sales with these exact values: [10, 15, 7, 12, 18, 9, 14, 20, 11, 13, 17, 8].
NumPy
Hint
Use np.array([...]) with the exact numbers inside the brackets.
2
Set the number of days
Create a variable called days and set it to 4.
NumPy
Hint
Just assign the number 4 to the variable days.
3
Reshape the sales array
Use the reshape method on sales to create a new array called daily_sales with shape (days, 3).
NumPy
Hint
Use sales.reshape(days, 3) to reshape the array.
4
Print the reshaped sales data
Print the daily_sales array to see the sales organized by day and bread type.
NumPy
Hint
Use print(daily_sales) to show the reshaped array.
Practice
(1/5)
1. Why is reshaping an array important in numpy?
easy
A. It sorts the array elements.
B. It deletes elements to reduce size.
C. It converts arrays into lists.
D. It changes the shape without altering the data content.
Solution
Step 1: Understand reshaping purpose
Reshaping changes how data is organized in rows and columns but keeps all elements intact.
Step 2: Compare options
Only It changes the shape without altering the data content. correctly states reshaping changes shape without changing data content.
Final Answer:
It changes the shape without altering the data content. -> Option D
Quick Check:
Reshape = Change shape, keep data [OK]
Hint: Reshape changes shape, not data values [OK]
Common Mistakes:
Thinking reshaping deletes or adds elements
Confusing reshaping with sorting
Believing reshaping converts data types
2. Which of the following is the correct syntax to reshape a numpy array arr into 2 rows and 3 columns?
easy
A. arr.reshape(2;3)
B. arr.reshape(3, 2)
C. arr.reshape(2, 3)
D. arr.reshape([2, 3])
Solution
Step 1: Recall numpy reshape syntax
The reshape method takes dimensions as separate arguments, e.g., reshape(2, 3).
Step 2: Check options
arr.reshape(2, 3) uses correct syntax with comma-separated integers. arr.reshape(3, 2) would create 3 rows and 2 columns instead. The option using a semicolon (;) is invalid syntax. The option passing a list [2, 3] is incorrect as reshape expects separate arguments.
Final Answer:
arr.reshape(2, 3) -> Option C
Quick Check:
reshape(rows, cols) = reshape(2, 3) [OK]
Hint: Use commas, not semicolons or lists in reshape() [OK]
Common Mistakes:
Using semicolons instead of commas
Passing a list instead of separate arguments
Swapping row and column order
3. What is the shape of the array after executing this code?
A. ValueError: cannot reshape array of size 10 into shape (3,4)
B. TypeError: reshape arguments must be integers
C. SyntaxError: invalid syntax
D. No error, reshaping works fine
Solution
Step 1: Check array size and reshape target
arr has 10 elements. Reshape tries to make shape (3,4) which needs 12 elements.
Step 2: Identify error type
Since total elements mismatch, numpy raises ValueError about impossible reshape.
Final Answer:
ValueError: cannot reshape array of size 10 into shape (3,4) -> Option A
Quick Check:
Mismatch size causes ValueError [OK]
Hint: Total elements must match for reshape [OK]
Common Mistakes:
Ignoring total element count mismatch
Expecting reshape to truncate or pad data
Confusing ValueError with SyntaxError
5. Given a numpy array arr = np.arange(24), which reshape command correctly reshapes it into a 3D array with shape (2, 3, 4) using automatic dimension calculation?
hard
A. arr.reshape(2, 3, 4)
B. arr.reshape(-1, 3, 4)
C. arr.reshape(3, -1, 4)
D. arr.reshape(2, 4, -1)
Solution
Step 1: Understand array size and target shape
arr has 24 elements. Target shape is (2, 3, 4) which multiplies to 24.
Step 2: Use -1 to let numpy calculate dimension
Using -1 lets numpy infer that dimension. arr.reshape(-1, 3, 4) uses -1 in first dimension, so shape becomes (2,3,4) automatically.
Step 3: Check other options
arr.reshape(2, 3, 4) works but does not use automatic calculation. arr.reshape(3, -1, 4) results in (3, 2, 4). arr.reshape(2, 4, -1) results in (2, 4, 3).
Final Answer:
arr.reshape(-1, 3, 4) -> Option B
Quick Check:
Use -1 for auto dimension: reshape(-1, 3, 4) [OK]
Hint: Use -1 where you want numpy to infer dimension [OK]
Common Mistakes:
Placing -1 in wrong dimension causing errors
Not matching total elements after reshape
Assuming multiple -1s allowed in reshape
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.