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 NumPy array that can hold different data types in named fields, like columns in a table.
Click to reveal answer
beginner
How do you access a field named 'age' in a structured NumPy array called 'data'?
You use data['age'] to get all values in the 'age' field.
Click to reveal answer
beginner
Why is accessing fields by name useful in NumPy structured arrays?
It lets you work with specific columns easily without affecting other data, similar to selecting a column in a spreadsheet.
Click to reveal answer
intermediate
Can you assign new values to a field in a NumPy structured array? How?
Yes, by using data['field_name'] = new_values, you can update the values in that field.
Click to reveal answer
intermediate
What happens if you try to access a field name that does not exist in a NumPy structured array?
NumPy raises a ValueError because the field name is not found in the array's dtype.
Click to reveal answer
How do you access the 'height' field in a structured NumPy array named 'people'?
Apeople.get('height')
Bpeople['height']
Cpeople.height()
Dpeople.height
✗ Incorrect
In NumPy structured arrays, fields are accessed using square brackets and the field name as a string.
What type of object is returned when you access a field by name in a structured array?
AA NumPy array of the field's values
BA Python list
CA scalar value
DA dictionary
✗ Incorrect
Accessing a field returns a NumPy array containing all values for that field.
If 'data' is a structured array, what does data['age'] = 30 do?
ARaises an error
BDeletes the 'age' field
CAdds a new field 'age' with value 30
DSets all 'age' values to 30
✗ Incorrect
Assigning a value to a field updates all entries in that field.
What error occurs if you try to access a non-existent field in a structured array?
AValueError
BIndexError
CTypeError
DKeyError
✗ Incorrect
Accessing a missing field raises a ValueError.
Which of these is NOT a valid way to access fields in a NumPy structured array?
Aarray.field
Barray['field']
Carray.get('field')
Darray['field_name']
✗ Incorrect
NumPy structured arrays do not have a .get() method for accessing fields. Use array['field'] or array.field.
Explain how to access and update a field named 'score' in a NumPy structured array.
Think about how you select columns in a table.
You got /3 concepts.
Describe what happens if you try to access a field that does not exist in a structured array.
What error do you get when a key is missing in a dictionary?
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to access the field named 'age' from a NumPy structured array data?
easy
A. data['age']
B. data.age()
C. data[age]
D. data.get('age')
Solution
Step 1: Understand structured array field access
In NumPy, fields in structured arrays are accessed using square brackets with the field name as a string.
Step 2: Identify correct syntax for field access
The syntax data['age'] correctly accesses the 'age' field. Other options use incorrect methods or syntax.
Final Answer:
data['age'] -> Option A
Quick Check:
Field access uses square brackets with field name [OK]
Hint: Use square brackets with field name as string [OK]
Common Mistakes:
Using unquoted field name like data[age]
Calling field as a method like data.age()
Using data.get() which is not valid for structured arrays
2. Which of the following is the correct syntax to create a NumPy structured array with fields 'name' (string) and 'score' (integer)?
easy
A. np.array([('Alice', 90), ('Bob', 85)], dtype=[('name', 'U10'), ('score', 'i4')])
B. np.array([('Alice', 90), ('Bob', 85)], dtype={name: 'U10', score: 'i4'})
C. np.array([('Alice', 90), ('Bob', 85)], dtype=[{name: 'U10'}, {score: 'i4'}])
D. np.array([('Alice', 90), ('Bob', 85)], dtype=('name', 'U10', 'score', 'i4'))
Solution
Step 1: Understand dtype format for structured arrays
The dtype should be a list of tuples, each tuple with field name and data type.
Step 2: Match correct dtype syntax
np.array([('Alice', 90), ('Bob', 85)], dtype=[('name', 'U10'), ('score', 'i4')]) uses the correct list of tuples format: [('name', 'U10'), ('score', 'i4')]. Other options use incorrect dtype formats.
A. It raises a TypeError because dtype is incorrect.
B. It raises a SyntaxError due to missing quotes around field names.
C. It prints the array correctly without errors.
D. It raises an AttributeError because fields are accessed with brackets, not dot notation.
Solution
Step 1: Check field access method
NumPy structured array fields must be accessed using square brackets with the field name as a string, not dot notation.
Step 2: Identify error from dot notation
Using arr.a causes AttributeError because 'a' is not an attribute but a field name.
Final Answer:
It raises an AttributeError because fields are accessed with brackets, not dot notation. -> Option D
Quick Check:
Use arr['a'], not arr.a [OK]
Hint: Use brackets, not dot, to access fields [OK]
Common Mistakes:
Using dot notation to access fields
Assuming dtype syntax error
Expecting code to print without error
5. You have a structured array data with fields 'name' (string), 'age' (int), and 'score' (float). How do you create a new array containing only the 'name' and 'score' fields?
hard
A. data['name']['score']
B. data[['name'], ['score']]
C. data[['name', 'score']]
D. data.get(['name', 'score'])
Solution
Step 1: Understand field selection syntax
To select multiple fields, use a list of field names inside double square brackets: data[['field1', 'field2']].
Step 2: Apply correct syntax to select 'name' and 'score'
Using data[['name', 'score']] returns a new structured array with only those fields.
Final Answer:
data[['name', 'score']] -> Option C
Quick Check:
Use double brackets with list of fields [OK]
Hint: Use double brackets with list of fields to select multiple [OK]
Common Mistakes:
Chaining field accesses like data['name']['score']