0
0
NumPydata~10 mins

Creating structured arrays in NumPy - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating structured arrays
Define data types for fields
Create data tuples matching types
Use numpy.array() with dtype
Structured array created
Access fields by name or index
First, define the data types for each field. Then create data tuples matching those types. Use numpy.array() with the dtype to create the structured array. Finally, access data by field names.
Execution Sample
NumPy
import numpy as np

dtype = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]
data = [('Alice', 25, 55.0), ('Bob', 30, 85.5)]
arr = np.array(data, dtype=dtype)
print(arr)
This code creates a structured array with fields name, age, and weight, then prints it.
Execution Table
StepActionData/VariableResult/State
1Define dtype[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]dtype ready with 3 fields
2Create data tuples[('Alice', 25, 55.0), ('Bob', 30, 85.5)]Data matches dtype structure
3Call np.array(data, dtype=dtype)data and dtypeStructured array created with 2 records
4Print arrarr[('Alice', 25, 55.) ('Bob', 30, 85.5)]
💡 All data tuples processed, structured array created successfully
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
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')]
dataNoneNone[('Alice', 25, 55.0), ('Bob', 30, 85.5)][('Alice', 25, 55.0), ('Bob', 30, 85.5)][('Alice', 25, 55.0), ('Bob', 30, 85.5)]
arrNoneNoneNoneStructured array with 2 recordsStructured array with 2 records
Key Moments - 3 Insights
Why do we need to specify the dtype when creating a structured array?
The dtype tells numpy the names and types of each field so it can store data correctly. Without dtype, numpy treats data as simple arrays, not structured records. See execution_table step 1 and 3.
Can we access fields by name after creating the structured array?
Yes, after creation, you can access fields by their names like arr['name'] or arr['age']. This is because dtype defines named fields. This is implied after step 3.
What happens if data tuples don't match the dtype structure?
Numpy will raise an error or misinterpret data because it expects each tuple to match the dtype fields exactly. See step 2 where data matches dtype.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'arr' after step 3?
AStructured array with 2 records
BNone
CList of tuples
Ddtype definition
💡 Hint
Check the 'Result/State' column for step 3 in the execution table.
At which step is the data variable assigned the list of tuples?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Data/Variable' column and see when data gets its value.
If we change the dtype to include a new field 'height', what must we also change?
ANothing, data tuples stay the same
BChange the print statement only
CAdd 'height' values to each data tuple
DRemove 'weight' field from dtype
💡 Hint
Recall that data tuples must match dtype fields exactly as shown in key moments.
Concept Snapshot
Creating structured arrays:
- Define dtype as list of (name, type) pairs
- Prepare data tuples matching dtype
- Use np.array(data, dtype=dtype) to create array
- Access fields by arr['fieldname']
- Ensures mixed data types in one array
Full Transcript
To create a structured array in numpy, first define the data types for each field using a list of tuples with field names and types. Then prepare your data as tuples matching these types. Use numpy.array() with the data and dtype to create the structured array. This array holds records with named fields, allowing you to access data by field names. The dtype is essential to tell numpy how to store and interpret each field. If data tuples don't match the dtype, numpy will raise errors or misinterpret data. After creation, you can print the array or access fields like arr['name'].