0

The following code snippet is from numpy's webpage:

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])

>>> x['a']
array([1, 3])

I have no ideal of why the dtype keyword argument takes the above form and why the array can be indexed like a dict.

2
  • 1
    It's giving a different name and type for each column of your array, similarly to a panda dataframe Commented Apr 26, 2022 at 9:05
  • 2
    read about structured arrays, numpy.org/doc/stable/user/basics.rec.html Commented Apr 26, 2022 at 11:55

1 Answer 1

1

I'm no numpy expert, so I might be a bit off here, but here's my understanding.

We define a structured data type with two named fields, 'a' and 'b'. Both of these fields contain a little-endian (<) integer (i) of 32 bits (4 bytes).

This is the data type associated with the array we're storing. So every element in our custom datatype takes its first field ('a') from the first element in our array (x[0]), and the second element ('b') from the second element in our array (x[1]).

If we look at all the values of the first field, x['a'] we get an array - [1,3].

If we look at all the values of the second field, x['b'] we get another array, that of the values in the second element - [2,4].

We can have our array store 3 elements but would have to maintain the 2-field structure.

x = np.array([(1,2), (3,4), (5,6)], dtype=[('a','<i4'), ('b','<i4')])

>>> x['a']
array([1, 3, 5], dtype=int32)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.