0
0
NumPydata~10 mins

Why structured arrays matter in NumPy - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a structured array with fields 'name' and 'age'.

NumPy
import numpy as np

person = np.array([('Alice', 25), ('Bob', 30)], dtype=[1])
Drag options to blanks, or click blank then click option'
A[('name', 'U10'), ('age', 'i4')]
B[('name', 'S10'), ('age', 'f4')]
C[('name', 'U5'), ('age', 'i2')]
D[('name', 'U20'), ('age', 'f8')]
Attempts:
3 left
💡 Hint
Common Mistakes
Using string types that are not Unicode (like 'S10') for names.
Using float type for age instead of integer.
2fill in blank
medium

Complete the code to access the 'age' field from the structured array.

NumPy
ages = person[1]
Drag options to blanks, or click blank then click option'
A[1]
B['age']
C.age
D['name']
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access fields with dot notation which does not work for numpy structured arrays.
Using the wrong field name.
3fill in blank
hard

Fix the error in the code to create a structured array with fields 'city' and 'population'.

NumPy
cities = np.array([('Paris', 2148327), ('Berlin', 3769495)], dtype=[1])
Drag options to blanks, or click blank then click option'
A[('city', 'U10'), ('population', 'f4')]
B[('city', 'S10'), ('population', 'i8')]
C[('city', 'U10'), ('population', 'i4')]
D[('city', 'i4'), ('population', 'i4')]
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer dtype for city names.
Using float dtype for population when integers are better.
4fill in blank
hard

Fill both blanks to create a structured array and access the 'score' field.

NumPy
data = np.array([(1, 95.5), (2, 88.0)], dtype=[1])
scores = data[2]
Drag options to blanks, or click blank then click option'
A[('id', 'i4'), ('score', 'f4')]
B['score']
C['id']
D[('score', 'i4'), ('id', 'f4')]
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names in dtype or access.
Using wrong data types for fields.
5fill in blank
hard

Fill all three blanks to create a structured array, filter by age, and get names.

NumPy
people = np.array([
    ('Anna', 28),
    ('Tom', 22),
    ('Mike', 35)
], dtype=[1])
adults = people[people[2] 25]
names = adults[3]
Drag options to blanks, or click blank then click option'
A[('name', 'U10'), ('age', 'i4')]
B['age']
C['name']
D[('age', 'i4'), ('name', 'U10')]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names in dtype or filtering.
Trying to filter with dot notation instead of bracket notation.