0
0
NumPydata~10 mins

Defining structured dtypes in NumPy - Interactive Code Practice

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

Complete the code to define a structured dtype with fields 'name' as string and 'age' as integer.

NumPy
dtype = np.dtype([('name', [1]), ('age', 'i4')])
Drag options to blanks, or click blank then click option'
A'i8'
B'f4'
C'U10'
D'S10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'S10' which is byte string instead of Unicode string.
Using integer types like 'i8' or 'f4' for string fields.
2fill in blank
medium

Complete the code to create a structured dtype with fields 'height' as float and 'weight' as float.

NumPy
dtype = np.dtype([('height', [1]), ('weight', 'f8')])
Drag options to blanks, or click blank then click option'
A'f8'
B'i4'
C'U5'
D'S5'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer types like 'i4' for float fields.
Using string types for numeric fields.
3fill in blank
hard

Fix the error in the dtype definition to correctly define 'id' as integer and 'score' as float.

NumPy
dtype = np.dtype([('id', 'i4'), ('score', [1])])
Drag options to blanks, or click blank then click option'
A'i4'
B'S4'
C'U4'
D'f4'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer type 'i4' for 'score' field.
Using string types for numeric fields.
4fill in blank
hard

Fill both blanks to define a structured dtype with 'date' as 10-character string and 'temperature' as 64-bit float.

NumPy
dtype = np.dtype([('date', [1]), ('temperature', [2])])
Drag options to blanks, or click blank then click option'
A'U10'
B'f8'
C'i4'
D'S10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using byte string 'S10' instead of Unicode string for 'date'.
Using integer type for 'temperature'.
5fill in blank
hard

Fill all three blanks to create a structured dtype with 'city' as 15-char string, 'population' as 32-bit int, and 'area' as 32-bit float.

NumPy
dtype = np.dtype([('city', [1]), ('population', [2]), ('area', [3])])
Drag options to blanks, or click blank then click option'
A'U15'
B'i4'
C'f4'
D'S15'
Attempts:
3 left
💡 Hint
Common Mistakes
Using byte string 'S15' instead of Unicode string for 'city'.
Mixing up integer and float types for 'population' and 'area'.