0
0
NumPydata~5 mins

Defining structured dtypes in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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)?
A[('height', 'int'), ('weight', 'int')]
B[('height', 'f8'), ('weight', 'f8')]
C('height', 'weight')
D['height', 'weight']
If you have a structured array 'arr' with a field 'score', how do you get all scores?
Aarr.get('score')
Barr.score
Carr['score']
Darr.score()
What does 'U10' mean in a structured dtype field?
AUnicode string of max length 10
B10-byte unsigned integer
CUnsigned integer of 10 bits
D10-character ASCII string
Which of these is NOT a benefit of structured dtypes?
AStore multiple data types in one array
BAccess fields by name
CRepresent complex records
DAutomatically sort data
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?
A[('x', 'f4'), ('y', 'f4'), ('label', 'U5')]
B[('x', 'i4'), ('y', 'i4'), ('label', 'U10')]
C[('point', 'f8'), ('label', 'U5')]
D[('x', 'f8'), ('y', 'f8'), ('label', 'int')]
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.