0
0
NumPydata~10 mins

Record arrays 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 create a record array from a list of tuples.

NumPy
import numpy as np

data = [(1, 'Alice', 3.5), (2, 'Bob', 4.0)]
dtype = [('id', 'i4'), ('name', 'U10'), ('score', 'f4')]
record_array = np.array(data, dtype=[1])
Drag options to blanks, or click blank then click option'
Arecord
Bdata
Clist
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the data list as dtype instead of the dtype variable.
Forgetting to specify dtype when creating the array.
2fill in blank
medium

Complete the code to access the 'name' field from the record array.

NumPy
names = record_array[1]
Drag options to blanks, or click blank then click option'
A[0]
B.name
C['name']
D[['name']]
Attempts:
3 left
💡 Hint
Common Mistakes
Using double brackets which returns a subarray instead of a field.
Using string indexing which is invalid for record arrays.
3fill in blank
hard

Fix the error in the code to create a record array with named fields.

NumPy
import numpy as np

values = [(10, 'John', 2.5), (20, 'Jane', 3.0)]
dtype = [('age', 'i4'), ('name', 'U5'), ('height', 'f4')]
rec_arr = np.array(values, [1]=dtype)
Drag options to blanks, or click blank then click option'
Adtypes
Btype
Cdtype
Dformat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'type' instead of 'dtype'.
Using 'dtypes' which is not recognized.
4fill in blank
hard

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

NumPy
import numpy as np

values = [(1, 'Ann', 4.2), (2, 'Ben', 3.8)]
dtype = [('id', 'i4'), ('name', 'U10'), ('score', 'f4')]
rec_arr = np.array([1], dtype=[2])
scores = rec_arr.score
Drag options to blanks, or click blank then click option'
Avalues
Bdtype
Cdata
Drecords
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the data and dtype variables.
Using undefined variables for data or dtype.
5fill in blank
hard

Fill all three blanks to create a record array, filter by score > 3.5, and get names.

NumPy
import numpy as np

values = [(1, 'Tom', 3.2), (2, 'Sue', 4.5), (3, 'Max', 3.8)]
dtype = [('id', 'i4'), ('name', 'U10'), ('score', 'f4')]
rec_arr = np.array([1], dtype=[2])
high_scores = rec_arr[rec_arr.score [3] 3.5]
names = high_scores.name
Drag options to blanks, or click blank then click option'
Avalues
Bdtype
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Using wrong variable names for data or dtype.