0
0
NumPydata~20 mins

Why structured arrays matter in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structured Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'])
A[2.5 4.5]
B[1 3]
C[2 4]
DTypeError
Attempts:
2 left
💡 Hint
Look at the dtype and which field is accessed.
data_output
intermediate
1: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))
A6
B3
C2
DTypeError
Attempts:
2 left
💡 Hint
Count the number of tuples in the array.
🔧 Debug
advanced
2: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')])
ATypeError
BIndexError
CValueError
DNo error
Attempts:
2 left
💡 Hint
Check if all tuples match the dtype specification.
🚀 Application
advanced
2: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')])
Apeople[people['age'] > 30]
Bpeople[people['name'] > 30]
Cpeople[people['age'] < 30]
Dpeople[people['age'] == '30']
Attempts:
2 left
💡 Hint
Compare the 'age' field with 30 using a greater than operator.
🧠 Conceptual
expert
3:00remaining
Why use structured arrays instead of regular arrays?
Which reason best explains why structured arrays are important in data science?
AThey only store strings efficiently.
BThey are faster than all other numpy arrays for numerical computations.
CThey automatically visualize data without extra code.
DThey allow storing multiple named fields with different data types in one array, enabling easy access and manipulation of heterogeneous data.
Attempts:
2 left
💡 Hint
Think about handling data with different types in one structure.