防止numpy创建多维数组 [英] Prevent numpy from creating a multidimensional array
问题描述
在创建数组时,NumPy非常有用.如果numpy.array
的第一个参数具有__getitem__
和__len__
方法,则根据它们可能是有效的序列来使用它们.
NumPy is really helpful when creating arrays. If the first argument for numpy.array
has a __getitem__
and __len__
method these are used on the basis that it might be a valid sequence.
不幸的是,我想创建一个包含dtype=object
的数组,而NumPy却不是有帮助的".
Unfortunatly I want to create an array containing dtype=object
without NumPy being "helpful".
分解为一个最小的示例,该类将如下所示:
Broken down to a minimal example the class would like this:
import numpy as np
class Test(object):
def __init__(self, iterable):
self.data = iterable
def __getitem__(self, idx):
return self.data[idx]
def __len__(self):
return len(self.data)
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.data)
如果"iterables"的长度不同,那么一切都很好,我得到的正是我想要的结果:
and if the "iterables" have different lengths everything is fine and I get exactly the result I want to have:
>>> np.array([Test([1,2,3]), Test([3,2])], dtype=object)
array([Test([1, 2, 3]), Test([3, 2])], dtype=object)
但是NumPy会创建一个多维数组,如果它们恰好具有相同的长度:
but NumPy creates a multidimensional array if these happen to have the same length:
>>> np.array([Test([1,2,3]), Test([3,2,1])], dtype=object)
array([[1, 2, 3],
[3, 2, 1]], dtype=object)
不幸的是,只有一个ndmin
参数,所以我想知道是否有一种方法可以强制执行ndmax
或以某种方式阻止NumPy将自定义类解释为另一个维度(而无需删除__len__
或__getitem__
) ?
Unfortunatly there is only a ndmin
argument so I was wondering if there is a way to enforce a ndmax
or somehow prevent NumPy from interpreting the custom classes as another dimension (without deleting __len__
or __getitem__
)?
推荐答案
一种解决方法当然是创建所需形状的数组,然后复制数据:
A workaround is of course to create an array of the desired shape and then copy the data:
In [19]: lst = [Test([1, 2, 3]), Test([3, 2, 1])]
In [20]: arr = np.empty(len(lst), dtype=object)
In [21]: arr[:] = lst[:]
In [22]: arr
Out[22]: array([Test([1, 2, 3]), Test([3, 2, 1])], dtype=object)
请注意,无论如何我都会感到麻木的行为.解释可迭代对象(这是您要使用的对象,对吗?)依赖于numpy版本.甚至可能是越野车.也许其中一些错误实际上是功能.无论如何,当更改numpy版本时,我会小心避免损坏.
Notice that in any case I would not be surprised if numpy behavior w.r.t. interpreting iterable objects (which is what you want to use, right?) is numpy version dependent. And possibly buggy. Or maybe some of these bugs are actually features. Anyway, I'd be wary of breakage when a numpy version changes.
相反,复制到预先创建的数组中应该更可靠.
On the contrary, copying into a pre-created array should be way more robust.
这篇关于防止numpy创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!