Python numpy.void 类型 - 如何使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25247190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:56:32  来源:igfitidea点击:

numpy.void type - how to use it?

pythonmatlabnumpyscipy

提问by Felix

I loaded a MATLAB .matfile via scipy.io.loadmatand it gave me a list of numpy.voidobjects.

.mat通过加载了一个 MATLAB文件scipy.io.loadmat,它给了我一个numpy.void对象列表。

Can someone tell me what are they, how they can be used and where can I get some reference documentationon them?

有人可以告诉我它们是什么,如何使用它们以及我可以从哪里获得有关它们的参考文档

采纳答案by rayryeng

According to the numpydocumentation: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html, numpy.voidtypes are defined as flexibledata 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.

根据numpy文档:http: //docs.scipy.org/doc/numpy/reference/arrays.dtypes.htmlnumpy.void类型被定义为灵活的数据类型。基本上,这些数据类型没有与您正在查看的变量相关联的预定义类型。如果你看一下numpy,你有数据类型,例如floatuint8boolstring,等。

voidis 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 structwhere 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.

void是为了适应更通用和更灵活的类型,并且用于那些不必属于这些预定义数据类型中的任何一种的数据类型。当您加载struct每个元素具有与多个字段关联的多种数据类型时,通常会遇到这种情况。每个结构元素可以具有不同数据类型的组合,并且所有这些数据类型的合并以表示该结构元素的实例因此导致我们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 genericdata type methods here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.generic.html#numpy.generic. In fact, all numpydata types are derived from this generic class, including numpy.void.

使用文档,您当然可以像使用任何其他数据类型一样执行相同的操作。看看generic这里的数据类型方法:http: //docs.scipy.org/doc/numpy/reference/generated/numpy.generic.html#numpy.generic。事实上,所有的numpy数据类型都是从这个泛型类派生出来的,包括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.voidand 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.

在我在本文开头提供的第一个链接中,它展示了如何创建自定义记录类型的一个很好的示例,其中记录是数字元组和字符串的组合。创建这些记录的列表时,列表中的每个类型都是类型numpy.void,它表明记录是这种数据类型。然而,牢记这个记录表都有一个数据类型,它是此纪录的,但这个列表中的每个元素将是类型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
  • 具有名为的字段的 16 位字符串 name
  • 浮点数的 2 元素元组,每个元组为 64 位,字段名为 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:

因为我们把这个列表变成了一个numpy.array,我们希望它的数据类型是这样的:

type(x)

We get:

我们得到:

<type 'numpy.ndarray'>

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

请记住,列表本身是一个numpy.array,但不是单个元素。



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:

我们得到:

<type 'numpy.void'> # 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:

我们得到:

<type 'numpy.string_'>

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

要检查第二条记录中的成绩类型,我们执行以下操作:

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

We get:

我们得到:

<type 'numpy.ndarray'>


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 collectionof these elements together is of type numpy.void.

请注意,此列表中的每个元素都是 类型numpy.void。但是,列表中每个元素的各个字段要么是数字元组,要么是字符串。这些元素的集合类型为numpy.void