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
Recall & Review
beginner
What is a record array in NumPy?
A record array is a special type of NumPy array that allows you to access fields by attribute name, similar to columns in a table. It stores heterogeneous data types in one array.
Click to reveal answer
beginner
How do you create a record array from a structured array in NumPy?
You can create a record array by calling the .view(np.recarray) method on a structured array. This lets you access fields as attributes.
Click to reveal answer
beginner
How do you access a field named 'age' in a NumPy record array?
You can access it using dot notation like array.age or by dictionary-style indexing like array['age'].
Click to reveal answer
intermediate
What is the difference between a structured array and a record array in NumPy?
A structured array stores data with named fields but you access fields using dictionary-style indexing. A record array allows access to fields using attribute (dot) notation.
Click to reveal answer
beginner
Why might you use a record array instead of a regular NumPy array?
Record arrays let you work with mixed data types and access fields by name easily, which is helpful when handling tabular data with different types in each column.
Click to reveal answer
How do you create a record array from a structured array named 'data'?
Adata.view(np.recarray)
Bnp.array(data, dtype='recarray')
Cnp.recarray(data)
Ddata.to_recarray()
✗ Incorrect
You create a record array by calling the .view(np.recarray) method on the structured array.
Which syntax accesses the 'name' field in a record array 'rec'?
Arec['name']
BBoth A and B
Crec.name()
Drec.name
✗ Incorrect
In record arrays, you can access fields either by dictionary-style indexing or dot notation.
What kind of data can a record array hold?
AOnly numbers
BOnly strings
COnly booleans
DMixed data types in different fields
✗ Incorrect
Record arrays can hold mixed data types in different fields, like numbers, strings, and more.
What is the main advantage of using a record array over a structured array?
AFaster computation
BUses less memory
CAccess fields with dot notation
DSupports only numeric data
✗ Incorrect
Record arrays allow accessing fields using dot notation, which is more convenient.
Which NumPy module provides the recarray class?
Anumpy.rec
Bnumpy.recarray
Cnumpy.core.records
Dcer.ypmun
✗ Incorrect
The recarray class is available in the numpy.rec module.
Explain what a record array is and how it differs from a regular NumPy array.
Think about how you access data fields and the types of data stored.
You got /4 concepts.
Describe how to create a record array from a structured array and how to access its fields.
Focus on the method to convert and the two ways to get field data.
You got /3 concepts.
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