0
0
NumPydata~10 mins

Defining structured dtypes in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining structured dtypes
Start: Define dtype fields
Create structured dtype
Create array with dtype
Access fields by name
Use fields for analysis
End
We first define the fields and their types, then create a structured dtype. Next, we make an array using this dtype, access fields by name, and use them for analysis.
Execution Sample
NumPy
import numpy as np

# Define structured dtype
person_dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])

# Create array
people = np.array([('Alice', 25, 55.0), ('Bob', 30, 85.5)], dtype=person_dtype)

# Access 'age' field
ages = people['age']

# Use 'ages' for analysis
mean_age = np.mean(ages)
This code defines a structured dtype for people with name, age, and weight, creates an array of people, extracts the ages, and computes the mean age.
Execution Table
StepActionVariable/FieldValue/Result
1Define structured dtypeperson_dtype[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]
2Create array with dtypepeople[('Alice', 25, 55.0), ('Bob', 30, 85.5)]
3Access 'age' fieldages[25 30]
4Use 'ages' for analysismean_age27.5
5End--
💡 All steps completed: structured dtype defined, array created, fields accessed, analysis done.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
person_dtypeNone[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')][('name', '<U10'), ('age', '<i4'), ('weight', '<f4')][('name', '<U10'), ('age', '<i4'), ('weight', '<f4')][('name', '<U10'), ('age', '<i4'), ('weight', '<f4')][('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]
peopleNoneNone[('Alice', 25, 55.0), ('Bob', 30, 85.5)][('Alice', 25, 55.0), ('Bob', 30, 85.5)][('Alice', 25, 55.0), ('Bob', 30, 85.5)][('Alice', 25, 55.0), ('Bob', 30, 85.5)]
agesNoneNoneNone[25 30][25 30][25 30]
mean_ageNoneNoneNoneNone27.527.5
Key Moments - 3 Insights
Why do we specify field names and types inside a list of tuples when defining a structured dtype?
Because each tuple defines a field name and its data type, which tells numpy how to store and access each part of the structured array. See execution_table step 1 where person_dtype is defined.
How does accessing a field like 'age' return a separate array?
Accessing a field by name extracts that column from the structured array as a normal numpy array. See execution_table step 3 where ages = people['age'] returns [25 30].
Can we perform normal numpy operations on fields extracted from structured arrays?
Yes, fields behave like normal arrays. For example, we calculate mean_age from ages in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'people' after step 2?
A[25, 30]
B[('Alice', 25, 55.0), ('Bob', 30, 85.5)]
C[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]
DNone
💡 Hint
Check the 'people' variable value in execution_table row for step 2.
At which step do we extract the 'age' field from the structured array?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'ages' variable is assigned in execution_table.
If we add a new field 'height' to person_dtype, which step would change?
ASteps 1 and 2
BStep 2 only
CStep 1 only
DAll steps
💡 Hint
Adding a field changes dtype definition and array creation, see steps 1 and 2.
Concept Snapshot
Defining structured dtypes:
- Use np.dtype with list of (name, type) tuples
- Create arrays with this dtype
- Access fields by name like array['field']
- Fields behave like normal arrays for analysis
- Useful for mixed data types in one array
Full Transcript
We start by defining a structured dtype using numpy's dtype function with a list of tuples. Each tuple has a field name and its data type. Then, we create a numpy array using this dtype, which stores records with multiple fields. We can access any field by its name, which returns a normal numpy array of that field's values. This allows us to perform normal numpy operations on individual fields. For example, we defined a dtype for people with name, age, and weight, created an array of two people, extracted the ages, and calculated their mean age.