📖 Scenario: Imagine you work at a small bakery that tracks daily sales of different types of bread. You want to organize this sales data in arrays to better understand the shape and size of your data.
🎯 Goal: You will create arrays representing sales data, check their shapes and dimensions, and print these details to understand how data is structured in arrays.
📋 What You'll Learn
Create a 1D array with daily sales numbers
Create a 2D array with sales of different bread types over several days
Use variables to store array shapes and dimensions
Print the shape and dimension of each array
💡 Why This Matters
🌍 Real World
Arrays are used to store and organize data like sales, temperatures, or images in rows and columns.
💼 Career
Knowing array shapes and dimensions is essential for data scientists and analysts to prepare data for analysis and machine learning.
Progress0 / 4 steps
1
Create a 1D array for daily sales
Import numpy as np and create a 1D array called daily_sales with these exact values: 10, 15, 7, 12, 9.
Data Analysis Python
Hint
Use np.array() to create the array with the given list of numbers.
2
Create a 2D array for sales of bread types
Create a 2D array called bread_sales using np.array() with these exact values: [[5, 3, 2], [7, 8, 4], [6, 5, 3]]. Each inner list represents sales of three bread types for one day.
Data Analysis Python
Hint
Use np.array() with a list of lists to create a 2D array.
3
Get shapes and dimensions of the arrays
Create variables daily_shape and daily_dim to store the shape and number of dimensions of daily_sales. Similarly, create bread_shape and bread_dim for bread_sales. Use the .shape and .ndim attributes.
Data Analysis Python
Hint
Use array.shape to get the shape and array.ndim to get the number of dimensions.
4
Print the shapes and dimensions
Print the variables daily_shape, daily_dim, bread_shape, and bread_dim each on its own line in this exact order.
Data Analysis Python
Hint
Use print() to show each variable on its own line.
Practice
(1/5)
1. What does the shape of a NumPy array represent?
easy
A. The number of elements along each dimension
B. The total number of elements in the array
C. The data type of the elements
D. The memory size of the array
Solution
Step 1: Understand the meaning of shape
The shape of an array is a tuple that shows how many elements are in each dimension.
Step 2: Differentiate shape from other properties
Shape is not the total count, data type, or memory size, but the size per dimension.
Final Answer:
The number of elements along each dimension -> Option A
Quick Check:
Shape = elements per dimension [OK]
Hint: Shape = count of elements per axis [OK]
Common Mistakes:
Confusing shape with total number of elements
Mixing shape with data type
Thinking shape is memory size
2. Which of the following is the correct way to get the shape of a NumPy array arr?
easy
A. arr.shape
B. arr.shape()
C. shape(arr)
D. arr.get_shape()
Solution
Step 1: Recall NumPy shape syntax
In NumPy, shape is an attribute, not a method, so no parentheses are used.
Step 2: Check each option
arr.shape uses arr.shape correctly; others use incorrect syntax or functions.
The array has 2 rows and 3 columns, so shape is (2, 3).
Step 2: Confirm shape output
Printing arr.shape returns the tuple (2, 3).
Final Answer:
(2, 3) -> Option C
Quick Check:
Rows=2, Columns=3 -> (2, 3) [OK]
Hint: Count rows then columns for shape [OK]
Common Mistakes:
Swapping rows and columns in shape
Thinking shape is total elements
Confusing shape with array size
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3])
print(arr.shape())
medium
A. print statement syntax is incorrect
B. arr is not a NumPy array
C. The array is empty
D. shape is an attribute, not a method; parentheses cause error
Solution
Step 1: Identify the error source
The code calls arr.shape() with parentheses, but shape is an attribute.
Step 2: Understand attribute vs method
Attributes do not use parentheses; calling with () causes a TypeError.
Final Answer:
shape is an attribute, not a method; parentheses cause error -> Option D
Quick Check:
Attributes no parentheses [OK]
Hint: Remember: shape is attribute, no () [OK]
Common Mistakes:
Using parentheses with shape
Assuming array is empty causes error
Misreading print syntax
5. You have a 3D NumPy array with shape (4, 3, 2). Which of the following operations will correctly reshape it into a 2D array with 4 rows and 6 columns?
hard
A. arr.reshape(12, 2)
B. arr.reshape(4, 6)
C. arr.reshape(6, 4)
D. arr.reshape(2, 12)
Solution
Step 1: Calculate total elements
Original shape (4, 3, 2) means 4*3*2 = 24 elements total.
Step 2: Determine new shape dimensions
New shape (4, 6) means 4*6 = 24 elements, matching total elements.
Step 3: Verify other options
arr.reshape(12, 2), arr.reshape(6, 4), and arr.reshape(2, 12) do not produce 4 rows and 6 columns.
Final Answer:
arr.reshape(4, 6) -> Option B
Quick Check:
4*6=24 matches total elements [OK]
Hint: New shape must keep total elements same [OK]
Common Mistakes:
Mixing up rows and columns in reshape
Choosing shapes with wrong total elements
Forgetting reshape must keep total size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.