python numpy 数组中的自定义数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2350072/
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-11-04 00:24:31  来源:igfitidea点击:

Custom data types in numpy arrays

pythonnumpy

提问by Mike Dewar

I'm creating a numpy array which is to be filled with objects of a particular class I've made. I'd like to initialize the array such that it will only ever contain objects of that class. For example, here's what I'd like to do, and what happens if I do it.

我正在创建一个 numpy 数组,该数组将填充我创建的特定类的对象。我想初始化数组,使其只包含该类的对象。例如,这是我想做的事情,如果我这样做会发生什么。

class Kernel:
    pass

>>> L = np.empty(4,dtype=Kernel)

TypeError: data type not understood

I can do this:

我可以做这个:

>>> L = np.empty(4,dtype=object)

and then assign each element of Las a Kernelobject (or any other type of object). It would be so neat were I able to have an array of Kernels, though, from both a programming point of view (type checking) and a mathematical one (operations on sets of functions).

然后将每个元素分配L为一个Kernel对象(或任何其他类型的对象)。Kernel但是,从编程的角度(类型检查)和数学的角度(对函数集的运算)来看,如果我能够拥有一个s数组,那就太好了。

Is there any way for me to specify the data type of a numpy array using an arbitrary class?

有什么方法可以让我使用任意类指定 numpy 数组的数据类型吗?

采纳答案by dwf

If your Kernel class has a predictable amount of member data, then you could define a dtype for it instead of a class. e.g. if it's parameterized by 9 floats and an int, you could do

如果您的内核类具有可预测的成员数据量,那么您可以为其定义 dtype 而不是类。例如,如果它由 9 个浮点数和一个整数参数化,你可以这样做

kerneldt = np.dtype([('myintname', np.int32), ('myfloats', np.float64, 9)])
arr = np.empty(dims, dtype=kerneldt)

You'll have to do some coercion to turn them into objects of class Kernel every time you want to manipulate methods of a single kernel but that's one way to store the actual data in a NumPy array. If you want to only store a reference, then the object dtype is the best you can do without subclassing ndarray.

每次您想要操作单个内核的方法时,您都必须进行一些强制转换以将它们转换为 Kernel 类的对象,但这是将实际数据存储在 NumPy 数组中的一种方法。如果你只想存储一个引用,那么对象 dtype 是最好的,你可以在不继承 ndarray 的情况下做。

回答by Eric O Lebigot

As far as I know, enforcing a single type for elements in a numpy.ndarray has to be done manually (unless the array contains Numpy scalars): there is no built-in checking mechanism (your array has dtype=object). If you really want to enforce a single type, you have to subclass ndarray and implement the checks in the appropriate methods (__setitem__, etc.).

据我所知,必须手动执行 numpy.ndarray 中元素的单一类型(除非数组包含 Numpy 标量):没有内置检查机制(您的数组具有 dtype=object)。如果您真的想强制执行单一类型,则必须继承 ndarray 并在适当的方法(__setitem__等)中实现检查。

If you want to implement operations on a set of functions (Kernel objects), you might be able to do so by defining the proper operations directly in your Kernel class. This is what I did for my uncertainties.pymodule, which handles numpy.ndarrays of numbers with uncertainties.

如果您想在一组函数(内核对象)上实现操作,您可以通过直接在内核类中定义正确的操作来实现。这就是我为我的不确定性.py 模块所做的,该模块处理具有不确定性的数字的 numpy.ndarrays。