Reputation: 45
I have come across the following statement in numpy:
x=numpy.zeros((2,2),dtype=[('x','i4'),('y','i4')])
and the output is like this:
[[(0,0)(0,0)]
[(0,0)(0,0)]]
What is the meaning of [('x','i4'),('y','i4')]
? Please explain.
Upvotes: 1
Views: 1168
Reputation: 1565
This is how the elements of the array are given a name and datatype.
In this case, the names of the first elements of each entry in the array can be accessed using 'x'
and the second elements can be accessed using 'y'
:
>>> x['x']
array([[0, 0],
[0, 0]])
>>> x['y']
array([[0, 0],
[0, 0]])
This is clearer if we change one of the entries:
>>> x['x'] = numpy.array([[1,1],[1,1]])
>>> x
array([[(1, 0), (1, 0)],
[(1, 0), (1, 0)]], dtype=[('x', 'i4'), ('y', 'i4')])
As you can see, the first element in each entry has been changed.
The 'i4'
parts specify the datatype of the elements. Specifically:
i
means signed integer
4
means a 4-byte size
See the documentation here
Upvotes: 5
Reputation: 538
If you look at the docs for Structured arrays, dtype denotes the data type of the values in the numpy array.
[('x','i4'),('y','i4')]
means x
is a 32-bit integer and y
is also a 32-bit integer.
Upvotes: 0
Reputation: 195
Here i4 is a 4-byte (32-bit) integer.
You will find more details in https://docs.scipy.org/doc/numpy-1.14.0/reference/arrays.dtypes.html (i4 is about halfway down the page).
Upvotes: 0