Monday, March 4, 2019

NumPy library - 8 (Structured Arrays)

NumPy's Structured Arrays are much more complex not only in size, but in the structure, when compared to mono-dimensional and two-dimensional arrays. They contain structs or records instead of individual items.

Let us create a structured array consisting of an integer, a character string of length 6 and a Boolean value. This will be a simple array of structs as items in which using the dtype option, we'll specify a list of comma-separated specifiers to indicate the elements that will constitute the struct, along with data type and order. Thus in our case we will specify the three types of data in the dtype option with the right order using the corresponding specifiers. See the following program:

import numpy as np

strarr1 = np.array([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3,2-2j), (3, 'Third', 0.8, 1+3j)],dtype=('i2, a6, f4, c8'))
print('The created Structured array : \n')
print(strarr1)


The output shows the created Structured array based on struct of items:

The created Structured array :

[(1, b'First', 0.5, 1.+2.j) (2, b'Second', 1.3, 2.-2.j)
 (3, b'Third', 0.8, 1.+3.j)]
------------------
(program exited with code: 0)

Press any key to continue . . .


We can also use the data type explicitly specifying int8, uint8, float16, complex64, and so forth to create a structured array. Let's create the above structured array using explicit data type specification. See the following program:

import numpy as np

strarr1 = np.array([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3,2-2j), (3, 'Third', 0.8, 1+3j)],dtype=('int16, a6, float32, complex64'))
print('The created Structured array : \n')
print(strarr1) 


The output shows the created Structured array based on struct of items which is the same as in the previous program:

The created Structured array :

[(1, b'First', 0.5, 1.+2.j) (2, b'Second', 1.3, 2.-2.j)
 (3, b'Third', 0.8, 1.+3.j)]
------------------
(program exited with code: 0)

Press any key to continue . . .


To access the rows from a Structured array we have to specify the appropriate reference index as we do for other arrays. The following program shows to to retrieve rows from a Structured array:

import numpy as np

strarr1 = np.array([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3,2-2j), (3, 'Third', 0.8, 1+3j)],dtype=('int16, a6, float32, complex64'))

print('The created Structured array : \n')
print(strarr1)
print('\nThe first row of Structured array : \n')
print(strarr1[0])
print('\nThe second row Structured array : \n')
print(strarr1[1])
print('\nThe third row Structured array : \n')
print(strarr1[2]) 


The output shows the created Structured array and then prints the rows individually:

The created Structured array :

[(1, b'First', 0.5, 1.+2.j) (2, b'Second', 1.3, 2.-2.j)
 (3, b'Third', 0.8, 1.+3.j)]

The first row of Structured array :

(1, b'First', 0.5, 1.+2.j)

The second row Structured array :

(2, b'Second', 1.3, 2.-2.j)

The third row Structured array :

(3, b'Third', 0.8, 1.+3.j)

------------------
(program exited with code: 0)

Press any key to continue . . .

To Structured array, names are assigned automatically to each item of struct which can be considered as the names of the columns of the array. The names are assigned automatically with an f (which stands for field) and a progressive integer that indicates the position in the sequence.Using them as a structured index, we can refer to all the elements of the same type, or of the same column. See the next program which prints the columns using these names:

import numpy as np

strarr1 = np.array([(1, 'First', 0.5, 1+2j),(2, 'Second', 1.3,2-2j), (3, 'Third', 0.8, 1+3j)],dtype=('int16, a6, float32, complex64'))

print('The created Structured array : \n')
print(strarr1)

print('\nThe columns of Structured array : \n')
print(strarr1['f1'])
print(strarr1['f2'])
print(strarr1['f3'])


The output is as follows:

The created Structured array :

[(1, b'First', 0.5, 1.+2.j) (2, b'Second', 1.3, 2.-2.j)
 (3, b'Third', 0.8, 1.+3.j)]

The columns of Structured array :

[b'First' b'Second' b'Third']
[0.5 1.3 0.8]
[1.+2.j 2.-2.j 1.+3.j]
------------------
(program exited with code: 0)

Press any key to continue . . .

A better approach would be to specify the names with something more meaningful which can be done at the time of array declaration as shown in the following program:

import numpy as np

strarr1 = np.array([(1,'First',0.5,1+2j),(2,'Second',1.3,2-2j),
(3,'Third',0.8,1+3j)],dtype=[(
'id','i2'),('position','a6'),('value','f4'),('complex','c8')])

print('The created Structured array : \n')
print(strarr1)
print(strarr1.dtype)
strarr1.dtype.names = ('id','order','value','complex')
print('\nThe columns of Structured array : \n')
print(strarr1['order'])
print(strarr1['value'])
print(strarr1['complex'])

We defined a tuple of names assigned to the dtype attribute of the structured array and then used the elements to print the columns as shown in the following output:

The created Structured array :

[(1, b'First', 0.5, 1.+2.j) (2, b'Second', 1.3, 2.-2.j)
 (3, b'Third', 0.8, 1.+3.j)]
[('id', '<i2'), ('position', 'S6'), ('value', '<f4'), ('complex', '<c8')]

The columns of Structured array :

[b'First' b'Second' b'Third']
[0.5 1.3 0.8]
[1.+2.j 2.-2.j 1.+3.j]
------------------
(program exited with code: 0)

Press any key to continue . . .

Here I am ending today's post. In the next post we shall further explore NumPy library and discuss about Reading and Writing Array Data on Files. Till we meet next keep practicing and learning Python as Python is easy to learn!
Share:

0 comments:

Post a Comment