Recall & Review
beginner
What is a structured dtype in NumPy?
A structured dtype in NumPy is a way to define a data type that contains multiple named fields, each with its own type. It allows you to store complex records like a table with columns of different types.
Click to reveal answer
beginner
How do you define a structured dtype with fields 'name' (string) and 'age' (integer)?
You define it using a list of tuples: [('name', 'U10'), ('age', 'i4')]. 'U10' means a Unicode string of max length 10, and 'i4' means a 4-byte integer.
Click to reveal answer
beginner
Why use structured dtypes instead of regular arrays?
Structured dtypes let you store different types of data together in one array, like a spreadsheet row. Regular arrays hold only one data type, so structured dtypes are useful for mixed data.Click to reveal answer
beginner
What does this dtype definition mean? dtype = [('x', 'f4'), ('y', 'f4')]
It defines a structured dtype with two fields: 'x' and 'y', both 4-byte floating point numbers. This can represent 2D points with float coordinates.
Click to reveal answer
beginner
How do you access the 'age' field from a structured NumPy array named 'data'?
You access it by using data['age']. This returns an array of all the 'age' values from each record.
Click to reveal answer
What is the correct way to define a structured dtype with fields 'height' (float) and 'weight' (float)?
✗ Incorrect
Option B correctly defines a structured dtype with two float fields using a list of tuples.
If you have a structured array 'arr' with a field 'score', how do you get all scores?
✗ Incorrect
You access fields in structured arrays using square brackets and the field name as a string.
What does 'U10' mean in a structured dtype field?
✗ Incorrect
'U10' means a Unicode string with a maximum length of 10 characters.
Which of these is NOT a benefit of structured dtypes?
✗ Incorrect
Structured dtypes do not automatically sort data; sorting is a separate operation.
How would you define a structured dtype for a record with a 2D point (x, y) as floats and a label as a string of max length 5?
✗ Incorrect
Option A correctly defines two float fields for x and y and a Unicode string label of length 5.
Explain how to create and use a structured dtype in NumPy to store records with different data types.
Think of a table with columns of different types stored in one array.
You got /4 concepts.
Describe the advantages of using structured dtypes over regular NumPy arrays.
Consider why you might want to keep related data together with different types.
You got /3 concepts.