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 dtype in NumPy?
A structured dtype in NumPy is a way to define a data type that contains multiple named fields, each with its own type. It allows you to store complex records like a table with columns of different types.
Click to reveal answer
beginner
How do you define a structured dtype with fields 'name' (string) and 'age' (integer)?
You define it using a list of tuples: [('name', 'U10'), ('age', 'i4')]. 'U10' means a Unicode string of max length 10, and 'i4' means a 4-byte integer.
Click to reveal answer
beginner
Why use structured dtypes instead of regular arrays?
Structured dtypes let you store different types of data together in one array, like a spreadsheet row. Regular arrays hold only one data type, so structured dtypes are useful for mixed data.
Click to reveal answer
beginner
What does this dtype definition mean? dtype = [('x', 'f4'), ('y', 'f4')]
It defines a structured dtype with two fields: 'x' and 'y', both 4-byte floating point numbers. This can represent 2D points with float coordinates.
Click to reveal answer
beginner
How do you access the 'age' field from a structured NumPy array named 'data'?
You access it by using data['age']. This returns an array of all the 'age' values from each record.
Click to reveal answer
What is the correct way to define a structured dtype with fields 'height' (float) and 'weight' (float)?
A[('height', 'int'), ('weight', 'int')]
B[('height', 'f8'), ('weight', 'f8')]
C('height', 'weight')
D['height', 'weight']
✗ Incorrect
Option B correctly defines a structured dtype with two float fields using a list of tuples.
If you have a structured array 'arr' with a field 'score', how do you get all scores?
Aarr.get('score')
Barr.score
Carr['score']
Darr.score()
✗ Incorrect
You access fields in structured arrays using square brackets and the field name as a string.
What does 'U10' mean in a structured dtype field?
AUnicode string of max length 10
B10-byte unsigned integer
CUnsigned integer of 10 bits
D10-character ASCII string
✗ Incorrect
'U10' means a Unicode string with a maximum length of 10 characters.
Which of these is NOT a benefit of structured dtypes?
AStore multiple data types in one array
BAccess fields by name
CRepresent complex records
DAutomatically sort data
✗ Incorrect
Structured dtypes do not automatically sort data; sorting is a separate operation.
How would you define a structured dtype for a record with a 2D point (x, y) as floats and a label as a string of max length 5?
A[('x', 'f4'), ('y', 'f4'), ('label', 'U5')]
B[('x', 'i4'), ('y', 'i4'), ('label', 'U10')]
C[('point', 'f8'), ('label', 'U5')]
D[('x', 'f8'), ('y', 'f8'), ('label', 'int')]
✗ Incorrect
Option A correctly defines two float fields for x and y and a Unicode string label of length 5.
Explain how to create and use a structured dtype in NumPy to store records with different data types.
Think of a table with columns of different types stored in one array.
You got /4 concepts.
Describe the advantages of using structured dtypes over regular NumPy arrays.
Consider why you might want to keep related data together with different types.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of defining a structured dtype in numpy?
easy
A. To convert arrays into lists automatically
B. To create arrays with only one data type
C. To speed up mathematical operations on arrays
D. To combine multiple data types in one array with named fields
Solution
Step 1: Understand structured dtype concept
Structured dtypes allow combining different data types in one array with named fields.
Step 2: Compare options with concept
Only To combine multiple data types in one array with named fields correctly describes this purpose; others describe unrelated features.
Final Answer:
To combine multiple data types in one array with named fields -> Option D
Quick Check:
Structured dtype = combine types [OK]
Hint: Structured dtype means named fields with different types [OK]
Common Mistakes:
Thinking structured dtype is for single data type arrays
Confusing structured dtype with speed optimization
Believing it converts arrays to lists
2. Which of the following is the correct syntax to define a structured dtype with fields 'name' as string and 'age' as integer?
easy
A. dtype = [('name', 'U10'), ('age', 'i4')]
B. dtype = ['name': 'U10', 'age': 'i4']
C. dtype = {'name': 'U10', 'age': 'i4'}
D. dtype = [('name', 10), ('age', int)]
Solution
Step 1: Recall structured dtype syntax
Structured dtype is defined as a list of tuples with (field_name, data_type).
Step 2: Check each option
dtype = [('name', 'U10'), ('age', 'i4')] matches the correct syntax; others use invalid formats or types.
Final Answer:
dtype = [('name', 'U10'), ('age', 'i4')] -> Option A
Quick Check:
List of tuples = correct dtype syntax [OK]
Hint: Use list of (field, type) tuples for structured dtype [OK]
Common Mistakes:
Using dictionary instead of list of tuples
Using colon instead of comma inside tuples
Using integer 10 instead of string 'U10' for string length
3. What will be the output of this code?
import numpy as np
dtype = [('id', 'i4'), ('score', 'f4')]
data = np.array([(1, 9.5), (2, 8.0)], dtype=dtype)
print(data['score'])
medium
A. [9.5 8. ]
B. [(1, 9.5) (2, 8.0)]
C. [1 2]
D. Error: invalid field name
Solution
Step 1: Understand structured array creation
Array has fields 'id' (int) and 'score' (float). Data has two records.
Step 2: Access 'score' field
Printing data['score'] returns array of scores: [9.5, 8.0].
Final Answer:
[9.5 8. ] -> Option A
Quick Check:
Access field returns values [9.5 8.0] [OK]
Hint: Access field by name to get its values array [OK]
Common Mistakes:
Expecting full tuples instead of single field values
Confusing field names causing errors
Printing whole array instead of one field
4. Identify the error in this code snippet:
import numpy as np
dtype = [('name', 'U5'), ('age', 'i4')]
data = np.array([('Alice', 25), ('Bob', 30)], dtype=dtype)
print(data['age'])
medium
A. Missing parentheses in np.array call
B. No error, code runs fine
C. Incorrect dtype format, should be dictionary
D. Field 'name' length too short for 'Alice'
Solution
Step 1: Check string length for 'name' field
'U5' means max 5 characters, but 'Alice' has 5 characters, which fits exactly.
Step 2: Verify if any error occurs
Actually, 'Alice' fits in 'U5', so no error from length. Check other options.
Step 3: Re-examine options
Options B, C, and D incorrectly identify non-existent errors; the code runs fine.
Final Answer:
No error, code runs fine -> Option B
Quick Check:
String length fits exactly, no error [OK]
Hint: Check string length carefully; exact fit is allowed [OK]
Common Mistakes:
Assuming string length must be larger than string length
Confusing dtype syntax with dictionary
Thinking missing parentheses cause error here
5. You want to create a structured array to store employee data with fields: 'emp_id' (integer), 'name' (string max 8 chars), and 'salary' (float). Which dtype definition is correct and why?