numpy.void type - how to use it?

后端 未结 1 1380
挽巷
挽巷 2021-01-31 15:44

I loaded a MATLAB .mat file via scipy.io.loadmat and it gave me a list of numpy.void objects.

Can someone tell me what are they, h

1条回答
  •  梦毁少年i
    2021-01-31 16:04

    According to the numpy documentation: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html, numpy.void types are defined as flexible data types. Basically, these are data types where there is no pre-defined type associated to the variable(s) you're looking at. If you look at numpy, you have data types such as float, uint8, bool, string, etc.

    void is to accommodate for more generic and flexible types and are for those data types that don't necessary fall into any one of these pre-defined data types. This situation is mostly encountered when you're loading in a struct where each element has multiple data types associated with multiple fields. Each structure element could have a combination of different data types, and the amalgamation of all of these data types to represent an instance of this structure element thus leads us to numpy.void.

    With the documentation, you can certainly do the same operations like you would with any other data type. Take a look at the generic data type methods here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.generic.html#numpy.generic . In fact, all numpy data types are derived from this generic class, including numpy.void.

    In the first link I provided at the beginning of this post, it shows a good example of how to create a custom record type, where a record is a combination of a tuple of numbers and a string. When creating a list of these records, each type in the list is of type numpy.void and it demonstrates that a record is of this data type. However, bear in mind that this record list has a data type that is of this record, but each element of this list will be of type numpy.void.


    However, as a matter of self-containment, let's re-create the example here: Let's create a custom record type where it has two fields associated for each variable you create:

    • A 16-bit string with a field named name
    • A 2-element tuple of floating point numbers that are 64-bits each, with a field named grades

    As such, you'd do something like:

    import numpy as np
    dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))])
    

    As such, let's create an example list of two elements and instantiate their fields:

    x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
    

    Because we made this list into a numpy.array, we expect its data type to be so:

    type(x)
    

    We get:

    
    

    Remember, the list itself is a numpy.array, but not the individual elements.


    To access the second element of this list, which is the second record, we do:

    x[1]
    

    We get:

    ('John', [6.0, 7.0])
    

    To check the type of the second record, we do:

    type(x[1])
    

    We get:

     # As expected
    

    Some additional bonuses for you

    To access the name of the second record, we do:

    x[1]['name']
    

    We get:

    'John'
    

    To access the grades of the second record, we do:

    x[1]['grades']
    

    We get:

    array([ 6.,  7.])
    

    To check the type of the name inside the second record, we do:

    type(x[1]['name'])
    

    We get:

    
    

    To check the type of the grades inside the second record, we do:

    type(x[1]['grades'])
    

    We get:

    
    

    Take note that each element in this list is of type numpy.void. However, the individual fields for each element in our list is either a tuple of numbers, or a string. The collection of these elements together is of type numpy.void.

    0 讨论(0)
提交回复
热议问题