Challenge - 5 Problems
Structured Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing fields in a structured array
What is the output of this code that creates a structured array and accesses one field?
NumPy
import numpy as np arr = np.array([(1, 2.5), (3, 4.5)], dtype=[('x', 'i4'), ('y', 'f4')]) print(arr['y'])
Attempts:
2 left
💡 Hint
Look at the dtype and which field is accessed.
✗ Incorrect
The structured array has fields 'x' and 'y'. Accessing arr['y'] returns the array of floats [2.5, 4.5].
❓ data_output
intermediate1:30remaining
Number of elements in a structured array
How many elements does this structured array contain?
NumPy
import numpy as np arr = np.array([(10, 20), (30, 40), (50, 60)], dtype=[('a', 'i4'), ('b', 'i4')]) print(len(arr))
Attempts:
2 left
💡 Hint
Count the number of tuples in the array.
✗ Incorrect
The array has 3 tuples, so len(arr) returns 3.
🔧 Debug
advanced2:00remaining
Error when mixing data types in structured array
What error does this code raise when trying to create a structured array with inconsistent field data types?
NumPy
import numpy as np arr = np.array([(1, 'a'), (2, 3)], dtype=[('num', 'i4'), ('char', 'U1')])
Attempts:
2 left
💡 Hint
Check if all tuples match the dtype specification.
✗ Incorrect
The second tuple has an integer where a string is expected, causing a ValueError.
🚀 Application
advanced2:30remaining
Selecting records based on a field condition
Given a structured array of people with fields 'name' and 'age', which option correctly selects all records where age is greater than 30?
NumPy
import numpy as np people = np.array([('Alice', 25), ('Bob', 35), ('Carol', 40)], dtype=[('name', 'U10'), ('age', 'i4')])
Attempts:
2 left
💡 Hint
Compare the 'age' field with 30 using a greater than operator.
✗ Incorrect
Option A correctly filters records where 'age' is greater than 30.
🧠 Conceptual
expert3:00remaining
Why use structured arrays instead of regular arrays?
Which reason best explains why structured arrays are important in data science?
Attempts:
2 left
💡 Hint
Think about handling data with different types in one structure.
✗ Incorrect
Structured arrays let you keep different types of data together with names, which is useful for real-world datasets.