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

preface

Python Numpy is a basic library for scientific computation that provides methods for fast manipulation of multi-dimensional array objects, arrays, and packages named ordinary and structured arrays. We have already learned about structured arrays as follows:

  • Numpy 4 ways to create structured data types
  • Structuring arrays with 3 assignment operations
  • Structured array 3 kinds of index access

As we all know, numpy ordinary arrays require that their data types be homogeneous, but their structured arrays are similar to Python’s built-in array type dictionary, defined using the numpy.dtype method to allow arrays to contain elements of different data types.

A structured array data type is a collection of fields. Each field consists of name, type, and offset. The field offset is optional.

As we learned in the last section, structured array indexes support index, and can access single field, multiple field data, and also support scalar access. Since the index of a structured array is mutable, the result returned is a view of the original array.

In addition to creating structured arrays using the numpy.array() method, Numpy also provides the numpy.record() method to create an array of records, which can be accessed directly through properties.

Let’s go~ Let’s go~

1. Record array

  • What is a record array?

    Numpy provides a standard array subclass, numpy.recarray, to create ndarray objects.

    Arrays created with numpy.recarray can be accessed not only through indexes but also through properties of structured arrays.

    And record arrays use a special data type, numpy.record, which allows field access to the data type scalar that is looked up as an attribute

  • Record the characteristics of an array

    • Record arrays allow fields to be accessed as array members
    • Structured arrays created by numpy.recarray can be converted to normal arrays by.tolist()
    • A structured array can be converted to an array of records using the numpy.rec.array() method

2. Record array creation

The numpy.rec module provides the ability to create arrays of records from various objects.

  • Create an array of records using the numpy.rec.array() method

    The record data refers directly to the field name to access the data in the array

    >>> recarr = np.rec.array([("Tom".12."Beijing"), ("Anne".10."Guangzhou"), ("Kenty".15."Shengzheng")],dtype=[("name"."U5"), ("age"."i8"), ("address"."U5")])
    >>> recarr.name
    array(['Tom'.'Anne'.'Kenty'], dtype='<U5')
    >>> recarr.age
    array([12.10.15], dtype=int64)
    >>> recarr.address
    array(['Beiji'.'Guang'.'Sheng'], dtype='<U5')
    >>> recarr.address[1:2]
    array(['Guang'], dtype='<U5')
    >>> recarr[1:2]
    rec.array([('Anne'.10.'Guang')],
              dtype=[('name'.'<U5'), ('age'.'<i8'), ('address'.'<U5')])
    >>> recarr[1:2].address
    array(['Guang'], dtype='<U5')
    >>> recarr[1].address
    'Guang'
    >>>
    
    Copy the code

    If the array is not a record, the system will throw an exception

    >>> arr = np.array([("Tom".12."Beijing"), ("Anne".10."Guangzhou"), ("Kenty".15."Shengzheng")],dtype=[("name"."U5"), ("age"."i8"), ("add
    ress"."U5")])
    >>> arr.name
    Traceback (most recent call last):
      File "<stdin>", line 1.in <module>
    AttributeError: 'numpy.ndarray' object has no attribute 'name'
    >>>
    Copy the code
  • Convert the parameters to an array of records using the numpy.rec.array() method

    A structured array created by numpy.array is converted to an array of records by rec.array methods

    >>> arr = np.array([("Tom".12."Beijing"), ("Anne".10."Guangzhou"), ("Kenty".15."Shengzheng")],dtype=[("name"."U5"), ("age"."i8"), ("add
    ress"."U5")])
    >>> recarr = np.rec.array(arr)
    >>>
    
    Copy the code
  • Get an array of records from a structured array using a view

    In addition to using rec.array to create arrays, we can also use the numpy.view view method to create arrays

    • When using the view method, the data type of the structured array is converted to the numpy.record data type by defining the type field as Np. recarray
    >>> arr = np.array([("Tom".12."Beijing"), ("Anne".10."Guangzhou"), ("Kenty".15."Shengzheng")],dtype=[("name"."U5"), ("age"."i8"), ("add
    ress"."U5")])
    >>> recarr = arr.view(dtype=np.dtype((np.record,arr.dtype)),type=np.recarray)
    >>>
    
    Copy the code
    • Numpy.view () calls numpy.recarray, which is automatically converted to numpy.record. Dtype can be omitted.

    >>> recarr = arr.view(np.recarray)
    >>>
    
    Copy the code

    Note that dTYPE and type need to be reset if converted to a non-record array.

    >>> recarr = np.rec.array([("Tom".12."Beijing"), ("Anne".10."Guangzhou"), ("Kenty".15."Shengzheng")],dtype=[("name"."U5"), ("age"."i8"), ("address"."U5")])
    >>> arr = recarr.view(recarr.dtype.fields or recarr.dtype,np.ndarray)
    >>> type(arr)
    <class 'numpy.ndarray'> > > >type(recarr)
    <class 'numpy.recarray'> > > >Copy the code

3. Record array auxiliary functions

When structured arrays are converted to record arrays, numpy specifically provides a number of methods for manipulating structured arrays in the numpy.lib.recfunctions module.

The original approach to structuring was implemented by John Hunter for the Matplotlib module. These methods have been rewritten and extended.

Examples of common methods are as follows:

methods role
numpy.lib.recfunctions.append_fields(base,name,data) Add new fields to an existing array. Name, data, and dtypes are values, not lists
numpy.lib.recfunctions.apply_along_fields(func,arr) Apply the function func as a structured array field
numpy.lib.recfunctions.assign_fields_by_name(dst,src) Assigns values from one structured array to another by field name
numpy.lib.recfunctions.drop_fields(base,drop_name) Drop drop_name and return a new structured array
numpy.lib.recfunctions.find_duplicates(a) Query duplicate items in a structured array
numpy.lib.recfunctions.get_fieldstructure(adtype) Convert a structured array to a dictionary

conclusion

In this installment, we’ll focus on converting a Numpy structured array into a record array, which allows access to structured array field data through reference properties.

When converting from a record array to a structured array, we need to reset dType and type.

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