📖 Scenario: You are working with temperature data collected from three cities over four days. The data is organized in a table format, where each row represents a city and each column represents a day.
🎯 Goal: You will create a NumPy ndarray to hold this temperature data, set up a variable for the number of days, extract temperatures for a specific city, and finally print the extracted data.
📋 What You'll Learn
Use NumPy to create an ndarray with exact temperature values
Create a variable to store the number of days
Extract temperatures for the city 'CityB' using ndarray indexing
Print the extracted temperatures
💡 Why This Matters
🌍 Real World
Scientists and analysts often use ndarrays to store and analyze data collected over time or across different locations.
💼 Career
Understanding how to use ndarrays is essential for data scientists and analysts working with numerical data in Python.
Progress0 / 4 steps
1
Create the temperature data ndarray
Import NumPy as np and create a 2D ndarray called temperatures with these exact values: first row [22, 21, 23, 22], second row [19, 20, 21, 20], third row [25, 26, 27, 26].
NumPy
Hint
Use np.array() to create the ndarray with the exact nested list of values.
2
Set the number of days variable
Create a variable called num_days and set it to the number of columns in the temperatures ndarray.
NumPy
Hint
Use the shape attribute of the ndarray to get the number of columns.
3
Extract temperatures for CityB
Create a variable called cityb_temps that extracts the second row (index 1) from the temperatures ndarray.
NumPy
Hint
Use indexing with [1] to get the second row from the ndarray.
4
Print the extracted temperatures
Print the variable cityb_temps to display the temperatures for CityB.
NumPy
Hint
Use print(cityb_temps) to display the array.
Practice
(1/5)
1.
What is the main purpose of a numpy ndarray?
easy
A. To display images on screen
B. To create text documents
C. To store numbers in one or more dimensions for fast math operations
D. To connect to databases
Solution
Step 1: Understand what ndarray stores
ndarray is designed to hold numbers arranged in rows and columns or more dimensions.
Step 2: Recognize its use for math
It allows fast math operations on many numbers at once, unlike regular Python lists.
Final Answer:
To store numbers in one or more dimensions for fast math operations -> Option C
Quick Check:
ndarray = fast number storage [OK]
Hint: ndarray = fast multi-dimensional number storage [OK]
Common Mistakes:
Thinking ndarray stores text or images
Confusing ndarray with database tools
Assuming ndarray is for display only
2.
Which of the following is the correct way to create a 1D ndarray from a Python list [1, 2, 3]?
import numpy as np
# Which line is correct?
easy
A. arr = np.array([1, 2, 3])
B. arr = np.ndarray([1, 2, 3])
C. arr = np.array(1, 2, 3)
D. arr = np.array{1, 2, 3}
Solution
Step 1: Recall correct syntax for np.array()
np.array() takes a list or nested lists inside parentheses.
Step 2: Check each option
arr = np.array([1, 2, 3]) uses correct syntax with square brackets inside parentheses. Others have wrong function calls or braces.
Final Answer:
arr = np.array([1, 2, 3]) -> Option A
Quick Check:
np.array(list) syntax = correct [OK]
Hint: Use np.array() with list inside parentheses [OK]