“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Numpy structured array can support multiple types of data type fields. The reason is that numpy structured arrays use the numpy.dtype method to create multiple types of data fields.

Numpy structured data types are knots of fields. Each field consists of name, type, and offset.

The data type of the field can be any NUMPY data type, other structured data types, and subarray data types.

Numpy also provides four methods for creating structured array types:

  • It is a list of tuples, one tuple for each field
  • Name is the default system name, such as f0 or F1
  • Represents field parameters in dictionary form
  • Representing field names in dictionary form (not recommended)

For numpy structured array operations, we have learned in numpy structured array assignment three kinds of structured array assignment methods respectively

  • From tuple assignment
  • Scalar assignment
  • Assignments from other structured arrays

The most common operation in Numpy for ordinary arrays is indexing, and the same is true for structured arrays.

Let’s go~, Let’s go~, Let’s go~

1. Structured array indexes

In numpy index slices and numpy advanced indexes, you can see that numpy indexes return copies.

Similarly, for a Numpy structured array index, is the value returned a view or a copy?

Let’s take a look at an example where we first create a structured array

>>> x = np.array([("Thu".3), ("Wen".4), ("Fri".5)],dtype=[("name"."U5"), ("num"."i8")])
>>> x
array([('Thu'.3), ('Wen'.4), ('Fri'.5)],
      dtype=[('name'.'<U5'), ('num'.'<i8')]) > > >Copy the code

We use python tuples such as x[1] index and x[1].base to query data from X

>>> print(x[1].base)
[('Thu'.3) ('Wen'.4) ('Fri'.5)]
Copy the code

Therefore, when scalar indexing, the result returned is the view

If we index x[1][“name”] to the specific data, we can also query x[1][“name”].base, we can see that the result is None, so the result is a copy.

>>> x[1] ["name"]
'Wen'
>>> print(x[1] ["name"].base)
None
Copy the code

X [“name”][1] indexes the same data as x[1][“name”], but returns a duplicate.

>>> x["name"] [1]
'Wen'
>>> print(x["name"] [1].base)
None
Copy the code

Similarly, x[“name”][[1,0]] indexes multiple data, and if you look at its base attribute, you can see that it is None, and the result is a copy.

>>> x["name"] [[0.1]]
array(['Thu'.'Wen'], dtype='<U5')
>>> print(x["name"] [[0.1]].base)
None
Copy the code

So, x[[1,0]][“name”] index multiple data, check its base attribute, from x, return the view

>>> x[[0.1]] ["name"]
array(['Thu'.'Wen'], dtype='<U5')
>>> print(x[[0.1]] ["name"].base)
[('Thu'.3) ('Wen'.4)] > > >Copy the code

2. Access a single field

We can use field names to access and modify the values of individual fields in a structured array

We can see that when we access a single field, we return a view of the original array.

>>> x["name"]
array(['Thu'.'Wen'.'Fri'], dtype='<U5')
>>> print(x["name"].base)
[('Thu'.3) ('Wen'.4) ('Fri'.5)] > > >Copy the code

We can modify the value of a structured array through index access

>>> x = np.array([("Thu".3), ("Wen".4), ("Fri".5)],dtype=[("name"."U5"), ("num"."i8")])
>>> y = x["num"]
>>> y[:] = 1
>>> x
array([('Thu'.1), ('Wen'.1), ('Fri'.1)],
      dtype=[('name'.'<U5'), ('num'.'<i8')]) > > >Copy the code

Y, as assigned, has the same dtype,shape,strides as x[“num”]

>>> print(y.dtype,y.shape,y.strides)
int64 (3(,)28.)>>> print(x["num"].dtype,x["num"].shape,x["num"].strides)
int64 (3(,)28,) > > >Copy the code

3. Access multiple fields

We can access the values of the related fields of a structured array as a list through an index.

  • An index is a list of field names

  • The behavior of multi-field indexes was changed from NUMpy 1.15 to NUMpy 1.16

>>> a = np.ones(3,dtype=[("one"."i4"), ("two"."i4"), ("three"."i4")])
>>> a[["one"."three"]]
array([(1.1), (1.1), (1.1)],
      dtype={'names': ['one'.'three'].'formats': ['<i4'.'<i4'].'offsets': [0.8].'itemsize':12})

Copy the code
  • When multiple indexes are accessed, the result returned is a view of the original array

    >>> print(a[["one"."three"]].base)
    [(1.1.1) (1.1.1) (1.1.1)]
    >>> print(a[["one"."three"]].flags.owndata)
    False
    
    Copy the code

When numpy results in an array multi-field index, it returns a view. We all know that the view modifies the original array, and the view fields are sorted in the order in which they are indexed.

>>> a[["one"."three"]] = (1.3)
>>> a
array([(1.1.3), (1.1.3), (1.1.3)],
      dtype=[('one'.'<i4'), ('two'.'<i4'), ('three'.'<i4')]) > > >Copy the code

4. Get structured scalars

We can access structured array data through integer indexes

  • Indexing a single element of a structured array by a single integer returns a structured scalar

    >>> a = np.ones(3,dtype=[("one"."i4"), ("two"."i4"), ("three"."i4")])
    >>> a
    array([(1.1.1), (1.1.1), (1.1.1)],
          dtype=[('one'.'<i4'), ('two'.'<i4'), ('three'.'<i4')])
    >>> b = a[0]
    >>> b
    (1.1.1) > > >Copy the code

    Unlike the single-element index of a normal array, structured scalars are mutable and therefore return a view of the original array.

    >>> print(b.base)
    [(1.1.1) (1.1.1) (1.1.1)]
    >>> print(b.flags.owndata)
    False
    >>>
    
    Copy the code
  • You can access multiple structured array data using integer list scalars

    >>> x = np.array([("Thu".3), ("Wen".4), ("Fri".5)],dtype=[("name"."U5"), ("num"."i8")])
    >>> x[[0.1]] ["num"]
    array([3.4], dtype=int64)
    >>>
    
    Copy the code

conclusion

This issue, we mainly numpy structured array index single field index, multi field index, integer scalar index three methods for learning.

The indexes of structured arrays are different from ordinary indexes. They all return views and can modify the original array data.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time