📖 Scenario: You are working with data collected from a small weather station. The data is stored in arrays representing temperature readings over several days.
🎯 Goal: Learn how to create a NumPy array and explore its basic attributes: shape, dtype, ndim, and size.
📋 What You'll Learn
Create a NumPy array with specific temperature data
Create a variable to hold the array
Access and print the array's shape, dtype, ndim, and size attributes
💡 Why This Matters
🌍 Real World
Data scientists often check array attributes to understand the data's structure before processing it.
💼 Career
Knowing array attributes is essential for data cleaning, transformation, and ensuring compatibility with machine learning models.
Progress0 / 4 steps
1
Create a NumPy array with temperature data
Import the NumPy library as np and create a NumPy array called temps with these exact values: [[22, 21, 23], [20, 19, 18], [25, 24, 26]].
NumPy
Hint
Use np.array() to create the array with the nested list of temperatures.
2
Create a variable to hold the array's shape
Create a variable called shape and set it to the shape attribute of the temps array.
NumPy
Hint
Use temps.shape to get the shape of the array.
3
Create variables for dtype, ndim, and size
Create three variables: dtype set to temps.dtype, ndim set to temps.ndim, and size set to temps.size.
NumPy
Hint
Access each attribute directly from the temps array.
4
Print the array attributes
Print the variables shape, dtype, ndim, and size each on a separate line in this exact order.
NumPy
Hint
Use four print() statements, one for each variable.
Practice
(1/5)
1. Which numpy attribute tells you the total number of elements in an array?
easy
A. size
B. shape
C. ndim
D. dtype
Solution
Step 1: Understand the meaning of each attribute
shape shows dimensions, dtype shows data type, ndim shows number of dimensions, and size shows total elements.
Step 2: Identify the attribute for total elements
The attribute size gives the total number of elements in the array.
Final Answer:
size -> Option A
Quick Check:
Total elements = size [OK]
Hint: Total elements? Use size attribute [OK]
Common Mistakes:
Confusing shape with total elements
Thinking ndim gives total elements
Mixing dtype with size
2. Which of the following is the correct way to get the number of dimensions of a numpy array arr?
easy
A. arr.size()
B. arr.ndim
C. arr.shape()
D. arr.dtype
Solution
Step 1: Recall attribute vs method syntax in numpy
Attributes like ndim and dtype are accessed without parentheses, methods like size() do not exist.
Step 2: Identify correct attribute for number of dimensions
arr.ndim gives the number of dimensions directly.
Final Answer:
arr.ndim -> Option B
Quick Check:
Number of dimensions = arr.ndim [OK]
Hint: Use attribute without () for ndim, size, dtype [OK]