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 structured array in NumPy?
A structured array is a special type of NumPy array that allows you to store different types of data in each element, like a table with named columns.
Click to reveal answer
beginner
Why are structured arrays useful compared to regular NumPy arrays?
Structured arrays let you keep different data types together in one array, making it easier to work with complex data like records or tables.
Click to reveal answer
intermediate
How do structured arrays help with data analysis?
They let you access data by column names, making your code clearer and easier to understand, just like using a spreadsheet.
Click to reveal answer
beginner
Can you store strings and numbers together in a structured array?
Yes, structured arrays can hold different types like strings, integers, and floats all in one array element.
Click to reveal answer
beginner
What is a real-life example where structured arrays matter?
Storing information about people, like name (string), age (integer), and height (float), all in one array for easy access and analysis.
Click to reveal answer
What is the main advantage of using structured arrays in NumPy?
ASpeed up numerical calculations only
BIncrease array size automatically
CMake arrays one-dimensional
DStore multiple data types in one array
✗ Incorrect
Structured arrays allow storing different data types together, unlike regular arrays which hold one type.
How do you access a column in a structured array?
ABy using the row number only
BBy using the column's name
CBy converting to a list first
DBy reshaping the array
✗ Incorrect
You can access columns directly by their names, making data handling easier.
Which of these is NOT a benefit of structured arrays?
AAutomatically sorting data
BNamed access to data fields
CHolding mixed data types
DBetter organization of complex data
✗ Incorrect
Structured arrays do not automatically sort data; sorting must be done separately.
What kind of data is best suited for structured arrays?
ASingle-type numeric data
BOnly large arrays of floats
CData with multiple fields of different types
DImages and videos
✗ Incorrect
Structured arrays are designed for data with multiple fields like names, ages, and scores.
Which NumPy feature allows you to define the data types and names for each field in a structured array?
Adtype
Bshape
Cndim
Dsize
✗ Incorrect
The dtype parameter defines the data types and field names in a structured array.
Explain why structured arrays matter when working with mixed data types in NumPy.
Think about how you would store a list of people with names and ages.
You got /3 concepts.
Describe a simple example where using a structured array is better than a regular NumPy array.
Consider storing a small database of items with different attributes.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using numpy structured arrays?
easy
A. They automatically visualize data.
B. They only store integers efficiently.
C. They replace Python lists completely.
D. They allow storing different data types in one array with named fields.
Solution
Step 1: Understand structured arrays
Structured arrays let you store multiple data types together, like numbers and text, in one array with named fields.
Step 2: Compare options
Only They allow storing different data types in one array with named fields. correctly describes this main advantage. Others are incorrect or unrelated.
Final Answer:
They allow storing different data types in one array with named fields. -> Option D
Quick Check:
Structured arrays = multiple types + named fields [OK]
Hint: Remember: structured arrays hold mixed data types by field names [OK]
Common Mistakes:
Thinking structured arrays only hold one data type
Confusing structured arrays with visualization tools
Assuming structured arrays replace all Python lists
2. Which of the following is the correct way to define a structured array with fields 'name' (string) and 'age' (integer)?
easy
A. np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'int'), ('age', 'float')])
B. np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')])
C. np.array([('Alice', 25), ('Bob', 30)], dtype=[('age', 'i4'), ('name', 'S10')])
D. np.array(['Alice', 25, 'Bob', 30], dtype=[('name', 'S10'), ('age', 'i4')])
Solution
Step 1: Check data and dtype match
np.array([(b'Alice', 25), (b'Bob', 30)], dtype=[('name', 'S10'), ('age', 'i4')]) correctly uses byte strings for names and integer type for age, matching the dtype fields.
Step 2: Identify errors in other options
B uses wrong types ('int' for name, 'float' for age); C passes string first ('Alice') to 'age' ('i4'), causing type mismatch; D passes a flat list instead of tuples.
C. Field 'age' does not exist in the structured array.
D. Array creation syntax is invalid.
Solution
Step 1: Check dtype fields
The structured array has fields 'id' and 'name', but no 'age' field.
Step 2: Analyze the print statement
Trying to print arr['age'] causes an error because 'age' is not defined in dtype.
Final Answer:
Field 'age' does not exist in the structured array. -> Option C
Quick Check:
Accessing undefined field = error [OK]
Hint: Check field names carefully before accessing [OK]
Common Mistakes:
Assuming all fields exist by default
Ignoring dtype field names
Confusing data values with field names
5. You have a structured array with fields 'name' (string), 'age' (int), and 'score' (float). How can you sort this array first by 'age' ascending, then by 'score' descending?
hard
A. Use arr['score'] = -arr['score'] arr.sort(order=['age', 'score'])
B. Use np.sort(arr, order=['age', 'score']) with a custom comparator for descending score.
C. Use arr.sort(order=['age']) then arr['score'] = -arr['score'] before sorting again.
D. Use arr.sort(order=['age']) then arr[arr['age'] == age_value].sort(order='score') for each age.
Solution
Step 1: Understand sorting by multiple fields
NumPy structured arrays can be sorted by multiple fields using sort(order=[...]), but only ascending.
Step 2: Handle descending order
To sort 'score' descending, negate it first (arr['score'] = -arr['score']), then arr.sort(order=['age', 'score']). This sorts age ascending, then negated score ascending (original score descending).
Final Answer:
Use arr['score'] = -arr['score'] arr.sort(order=['age', 'score']) -> Option A