1

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.

dhilmathy
2,8882 gold badges23 silver badges33 bronze badges
asked Jul 16, 2018 at 15:10
1
  • 4
    Please see Structured arrays. Commented Jul 16, 2018 at 15:15

3 Answers 3

5

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

answered Jul 16, 2018 at 15:25
Sign up to request clarification or add additional context in comments.

Comments

0

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).

answered Jul 16, 2018 at 15:27

Comments

0

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.

rahlf23
9,0474 gold badges31 silver badges57 bronze badges
answered Jul 16, 2018 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.