0
0
NumPydata~10 mins

Creating structured arrays in NumPy - Interactive Practice

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

data = np.array([('Alice', 25), ('Bob', 30)], dtype=[1])
print(data)
Drag options to blanks, or click blank then click option'
A[('name', 'U10'), ('age', 'i4')]
B[('name', 'U10'), ('age', 'f8')]
C[('name', 'U5'), ('age', 'i2')]
D[('name', 'S10'), ('age', 'f4')]
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect data types like float for age.
Not defining dtype as a list of tuples.
2fill in blank
medium

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

NumPy
ages = data[1]
print(ages)
Drag options to blanks, or click blank then click option'
A['name']
B['age']
C.age
D[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation which does not work for numpy structured arrays.
Using incorrect field names.
3fill in blank
hard

Fix the error in the code to create a structured array with fields 'x' and 'y' as floats.

NumPy
points = np.array([(1.0, 2.0), (3.0, 4.0)], dtype=[1])
print(points)
Drag options to blanks, or click blank then click option'
A[('x', 'f8'), ('y', 'f8')]
B[('x', 'i4'), ('y', 'i4')]
C[('x', 'U5'), ('y', 'U5')]
D[('x', 'f4'), ('y', 'f4')]
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer types for float data.
Using string types for numeric data.
4fill in blank
hard

Fill both blanks to create a structured array with fields 'id' as integer and 'score' as float.

NumPy
results = np.array([(1, 95.5), (2, 88.0)], dtype=[[1], [2]])
print(results)
Drag options to blanks, or click blank then click option'
A('id', 'i4')
B('score', 'f4')
C('score', 'f8')
D('id', 'i8')
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of fields.
Using incompatible data types for the values.
5fill in blank
hard

Fill all three blanks to create a structured array with fields 'name' (string), 'age' (int), and 'height' (float).

NumPy
people = np.array([
    ('John', 28, 5.9),
    ('Jane', 32, 5.5)
], dtype=[[1], [2], [3]])
print(people)
Drag options to blanks, or click blank then click option'
A('name', 'U10')
B('age', 'i4')
C('height', 'f4')
D('weight', 'f4')
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names or data types.
Including extra fields not in the data.