Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Working with Record Arrays in NumPy
📖 Scenario: Imagine you work in a small library. You want to keep track of books with their title, author, and year published. You will use a special kind of array called a record array to store this information neatly.
🎯 Goal: Create a NumPy record array to store book information, then select and display books published after a certain year.
📋 What You'll Learn
Create a NumPy record array with fields: 'title' (string), 'author' (string), and 'year' (integer).
Create a variable to hold the year threshold for filtering books.
Use a condition to select books published after the threshold year.
Print the filtered record array showing only those books.
💡 Why This Matters
🌍 Real World
Record arrays help store mixed data types in one array, useful for datasets like books, employees, or products.
💼 Career
Understanding record arrays is important for data scientists and analysts who work with structured data in NumPy.
Progress0 / 4 steps
1
Create a NumPy record array with book data
Create a NumPy record array called books with these exact entries: ('The Hobbit', 'J.R.R. Tolkien', 1937), ('1984', 'George Orwell', 1949), and ('To Kill a Mockingbird', 'Harper Lee', 1960). Use the data types 'U20' for title and author, and int for year.
NumPy
Hint
Use np.array with a list of tuples and specify the dtype as a list of tuples for field names and types.
2
Set a year threshold for filtering books
Create a variable called year_threshold and set it to 1940.
NumPy
Hint
Just create a variable named year_threshold and assign the number 1940 to it.
3
Select books published after the threshold year
Create a variable called recent_books that selects from books only those entries where the year is greater than year_threshold.
NumPy
Hint
Use boolean indexing with books['year'] > year_threshold to filter the record array.
4
Print the filtered record array
Write a print statement to display the recent_books record array.
NumPy
Hint
Use print(recent_books) to show the filtered books.
Practice
(1/5)
1. What is the main advantage of using a record array in numpy?
easy
A. It speeds up numerical calculations on large arrays.
B. It automatically sorts data based on values.
C. It allows storing different data types in one array with named fields.
D. It compresses data to save memory.
Solution
Step 1: Understand record arrays
Record arrays let you store mixed data types in one numpy array by using named fields.
Step 2: Compare options
Only It allows storing different data types in one array with named fields. correctly describes this feature. Others describe unrelated features.
Final Answer:
It allows storing different data types in one array with named fields. -> Option C
Quick Check:
Record arrays = mixed types + named fields [OK]
Hint: Remember: record arrays hold mixed types with names [OK]
Common Mistakes:
Confusing record arrays with regular numeric arrays
Thinking record arrays sort data automatically
Assuming record arrays compress data
2. Which of the following is the correct way to create a numpy record array with fields 'name' (string) and 'age' (integer)?
easy
A. np.rec.array([("Alice", 25), ("Bob", 30)], dtype=[('name', 'U10'), ('age', 'i4')])
B. np.array([("Alice", 25), ("Bob", 30)], dtype=[('name', 'i4'), ('age', 'U10')])
C. np.rec.array(["Alice", 25, "Bob", 30], dtype=[('name', 'U10'), ('age', 'i4')])
D. np.rec.array([(25, "Alice"), (30, "Bob")], dtype=[('name', 'U10'), ('age', 'i4')])
Solution
Step 1: Check data and dtype matching
np.rec.array([("Alice", 25), ("Bob", 30)], dtype=[('name', 'U10'), ('age', 'i4')]) matches tuples of (string, int) with dtype [('name', 'U10'), ('age', 'i4')].
Step 2: Validate other options
np.array([("Alice", 25), ("Bob", 30)], dtype=[('name', 'i4'), ('age', 'U10')]) swaps types incorrectly; C has wrong input format; A swaps field order.
A. You cannot add integer and string fields directly.
B. The dtype specification is incorrect.
C. The data tuples have wrong length.
D. The record array must be created with np.array, not np.rec.array.
Solution
Step 1: Analyze the operation
rec.num is integer array, rec.char is string array.
Step 2: Check addition of int and string
Adding int + string causes a TypeError in numpy.
Final Answer:
You cannot add integer and string fields directly. -> Option A
Quick Check:
int + string = TypeError [OK]
Hint: Cannot add numbers and strings directly in numpy [OK]
Common Mistakes:
Assuming dtype is wrong instead of operation
Thinking np.rec.array is incorrect here
Ignoring type mismatch in addition
5. You have a numpy record array rec with fields 'id' (int), 'score' (float), and 'passed' (bool). How do you create a new record array containing only records where passed is True and score is above 80?
hard
A. rec[rec.passed or rec.score > 80]
B. rec[(rec.passed) & (rec.score > 80)]
C. rec[rec.passed and rec.score > 80]
D. rec[(rec.passed) | (rec.score > 80)]
Solution
Step 1: Understand filtering syntax
Use boolean indexing with & for element-wise AND, parentheses needed.
Step 2: Evaluate options
rec[(rec.passed) & (rec.score > 80)] correctly uses (rec.passed) & (rec.score > 80). Options B and C use Python 'or'/'and' which don't work element-wise. rec[(rec.passed) | (rec.score > 80)] uses | (OR) instead of AND.
Final Answer:
rec[(rec.passed) & (rec.score > 80)] -> Option B
Quick Check:
Use & with parentheses for element-wise AND [OK]
Hint: Use & with parentheses for element-wise conditions [OK]
Common Mistakes:
Using 'and' or 'or' instead of '&' or '|' for arrays