numpy.void类型-如何使用? [英] numpy.void type - how to use it?
问题描述
我通过scipy.io.loadmat
加载了MATLAB .mat
文件,它为我提供了numpy.void
对象的列表.
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, how they can be used and where can I get some reference documentation on them?
推荐答案
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
用于容纳更通用和更灵活的类型,并且用于那些不必属于这些预定义数据类型中的任何一种的数据类型.在struct
中加载时,通常会遇到这种情况,其中每个元素具有与多个字段关联的多种数据类型.每个结构元素可以具有不同数据类型的组合,所有这些数据类型的合并表示该结构元素的一个实例,因此将我们引向numpy.void
.
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
.
使用该文档,您当然可以像对任何其他数据类型一样执行相同的操作.在此处查看generic
数据类型方法:
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
.
在我在本文开头提供的第一个链接中,它显示了如何创建自定义记录类型的好例子,其中记录是数字元组和字符串的组合.创建这些记录的列表时,列表中的每种类型均为numpy.void
类型,它表明该记录属于此数据类型.但是,请记住,该记录列表具有该记录的数据类型 ,但是此列表的每个元素的类型均为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:
- 一个16位字符串,其字段名为
name
- 由2个元素组成的元组,每个浮点数均为64位,其字段名为
grades
- 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)
由于我们将此列表放入了numpy.array
中,因此我们希望其数据类型是这样的:
Because we made this list into a numpy.array
, we expect its data type to be so:
type(x)
我们得到:
<type 'numpy.ndarray'>
请记住,列表本身是numpy.array
,但不是各个元素.
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]
我们得到:
('John', [6.0, 7.0])
要检查第二条记录的类型,我们要做:
To check the type of the second record, we do:
type(x[1])
我们得到:
<type 'numpy.void'> # As expected
为您带来一些额外的奖励
要访问第二条记录的名称,请执行以下操作:
Some additional bonuses for you
To access the name of the second record, we do:
x[1]['name']
我们得到:
'John'
要访问第二条记录的成绩,我们要做:
To access the grades of the second record, we do:
x[1]['grades']
我们得到:
array([ 6., 7.])
要检查第二条记录中名称的类型,我们要做:
To check the type of the name inside the second record, we do:
type(x[1]['name'])
我们得到:
<type 'numpy.string_'>
要检查第二条记录内的成绩类型,我们要做:
To check the type of the grades inside the second record, we do:
type(x[1]['grades'])
我们得到:
<type 'numpy.ndarray'>
请注意,此列表中的每个元素都是numpy.void
类型.但是,列表中每个元素的单独字段要么是数字的元组,要么是字符串.这些元素的集合一起为numpy.void
类型.
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
.
这篇关于numpy.void类型-如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!