10. Structured Data: Numpy's Structured Arrays
2025. 6. 17. 09:24ㆍPython/Numpy
# In[1]
name=['Alice','Bob','Cathy','Doug']
age=[25,45,37,19]
weight=[55.0,85.5,68.0,61.5]
# In[2]
data=np.zeros(4,dtype={'names':('name','age','weight'),'formats':('U10,'i4','f8')})
print(data.dtype)
# Out[2]
[('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]
U10
means Unicode string of maximum length 10,i4
means 4 byte integer, andf8
means 8 byte float.- We can fill the array with our lists of values.
# In[3]
data['name']=name
data['age']=age
data['weight']=weight
print(data)
# Out[3]
[('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. )
('Doug', 19, 61.5)]
# In[4]
data['name']
# Out[4]
array(['Alice' 'Bob' 'Cathy' 'Doug'], dtype='<U10')
- We can also using boolean masking
# In[5]
data[data['age']<30]['name']
# Out[5]
array(['Alice', 'Doug'], dtype='<U10')
Exploring Structured Arrays Creation
# In[6]
np.dtype({'names':('name','age','weight'),'formats':('U10','i4','f8')})
# Out[6]
dtype([('name', '<U10'), ('age', '<i4'), ('weight', '<f8')])
- numerical types can be specified using Python types or Numpy dtypes instead like this
# In[7]
np.dtype({'names':('name','age','weight'),'formats':((np.str_,10),int,np.float32)})
# Out[7]
dtype([('name', '<U10'), ('age', '<i8'), ('weight', '<f4')])
- A compound type can also be specified as a list of tuples.
# In[8]
np.dtype([('name','S10'),('age','i4'),('weight','f8')])
# Out[8]
dtype([('name', 'S10'), ('age', '<i4'), ('weight', '<f8')])
- If the names of the types do not matter to you, you can specify the types alone in a comma-separated string
# In[9]
np.dtype('S10,i4,f8')
# Out[9]
dtype([('f0', 'S10'), ('f1', '<i4'), ('f2', '<f8')])
Numpy data types
Character | Description | Example |
---|---|---|
b |
Byte | np.dtype('b') |
i |
Signed integer | np.dtype('i4') == np.int32 |
u |
Unsigned integer | np.dtype('u1') == np.uint8 |
f |
Floating point | np.dtype('f8') == np.int64 |
c |
Complex floating point | np.dtype('c16') == np.complex128 |
S,a |
String | np.dtype('S5') |
U |
Unicode string | np.dtype('U') == np.str_ |
V |
Raw data(void) | np.dtype('V') == np.void |
More Advanced Compound Types
- We can create a type where each element contains an array or matrix of values.
# In[10]
tp=np.dtype([('id','i8'),('mat','f8',(3,3))])
X=np.zeros(1,dtype=tp)
print(X[0])
print(X['mat'][0])
# Out[10]
(0, [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
- Each element in the
X
array consists of anid
and a 3×3 matrix.
Record Arrays: Structured Arrays with a Twist
- Numpy also provides record arrays (like
np.recarray
), which are almost identical to the structured arrays just described - However, field can be accessed as attributes rather than as dictionary keys.
# In[11]
data['age']
# Out[11]
array([25, 45, 37, 19], dtype=int32)
- If we view our data as a record array instead, we can access like this
# In[12]
data_rec=data.view(np.recarray)
data_rec.age
# Out[12]
array([25, 45, 37, 19], dtype=int32)
- The
In[12]
is that for record arrays, there is some extra overhead involved in accessing the fields.
'Python > Numpy' 카테고리의 다른 글
9. Sorting Arrays (0) | 2025.06.17 |
---|---|
8. Fancy Indexing (1) | 2025.06.17 |
7. Comparisons, Masks, and Boolean Logic (0) | 2025.06.17 |
6. Computation on Arrays: Broadcasting (0) | 2025.06.17 |
5. Aggregations: Min, Max, and Everything in Between (0) | 2025.06.17 |