Table of Contents
ToggleStructured arrays in NumPy allow for the representation of data with different types and sizes in a single array. Each element in a structured array can be a record with multiple fields, each field having its own data type.
This is similar to having a table where each row represents a record with various attributes. The key points are as follows −
The first step for creating a structured array is to define the data type for the structured array. This is done using a NumPy dtype object, which specifies the names and types of the fields in the array as shown in the following example −
import numpy as np
# Define the data type for the structured array
dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('height', 'f4')])
Here, dtype is a structured data type with three fields −
Once you have defined the “dtype”, you can create the structured array by passing the dtype to the np.array() function. Following is the syntax −
numpy.array( object, dtype=None, copy=True, order='K', subok=False, ndmin=0 )
Where,
np.array may return a view of the original array if possible.In the following example, we are defining a structured array with a specified dtype that includes fields for “name”, “age”, and “height”. We then create the array with data matching this structure and print the resulting structured array −
# Open Compiler
import numpy as np
# Define the dtype with field names and data types
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
# Create data consistent with the dtype
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]
# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array:\n", structured_array)
Output:
Following is the output obtained −
Structured Array:
[(‘Alice’, 30, 5.6) (‘Bob’, 25, 5.8) (‘Charlie’, 35, 5.9)]
You can create structured arrays with various data types, including strings, integers, and floats, depending on the needs of your application.
In the context of structured arrays −
In the following example, we are creating a structured array with a data type (dtype) that includes mixed data types: integers for IDs, strings for names, and floating-point numbers for scores −
# Open Compiler
import numpy as np
# Define a dtype with mixed data types
dtype = [('id', 'i4'), ('name', 'U15'), ('score', 'f8')]
data = [(1, 'Alice', 88.5), (2, 'Bob', 91.2), (3, 'Charlie', 85.4)]
# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array with Mixed Data Types:\n", structured_array)
Output:
This will produce the following result −
Structured Array with Mixed Data Types:[(1, ‘Alice’, 88.5) (2, ‘Bob’, 91.2) (3, ‘Charlie’, 85.4)]
You can define the dtype and create the structured array using a list of tuples, where each tuple represents a field. Each tuple contains two elements: the first element is the name of the field, and the second element is the data type of that field.
In the following example, we are defining a structured array with fields for “name”, “age”, and “height” using a specified dtype. We then create this array with corresponding data −
# Open Compiler
import numpy as np
# Define the dtype
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
# Define the data
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]
# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array:\n", structured_array)
Output:
Following is the output obtained −
Structured Array:
[(‘Alice’, 30, 5.6) (‘Bob’, 25, 5.8) (‘Charlie’, 35, 5.9)]
Alternatively, you can define the data and dtype using a dictionary to clearly specify the names and types of fields. Each key in the dictionary represents a field name, and the value associated with each key defines the data type of that field.
In this example, we are defining the dtype for a structured array using a dictionary format to specify fields for “name”, “age”, and “height”. We then create and display this structured array with the corresponding data, organizing it into a format that supports multiple data types within each record −
# Open Compiler
import numpy as np
# Define the dtype using a dictionary
dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('height', 'f4')])
# Define the data
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]
# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array from Dictionary:\n", structured_array)
Output:
This will produce the following result −
Structured Array from Dictionary:
[(‘Alice’, 30, 5.6) (‘Bob’, 25, 5.8) (‘Charlie’, 35, 5.9)]
Key Takeaway: Master the basics of structured arrays in NumPy with Vista Academy!
