简体   繁体   English

如何将二维 numpy 数组直接拆分为 numpy 对象数组

[英]How to split a 2D numpy array directly into numpy array of objects

For a special application dealing with numpy arrays of different lengths, I need my preferably numpy array, not just a list , to have the form np.ndarray[np.ndarray[ ], np.ndarray[ ], ..., dtype=object] . For a special application dealing with numpy arrays of different lengths, I need my preferably numpy array, not just a list , to have the form np.ndarray[np.ndarray[ ], np.ndarray[ ], ..., dtype=object] If I have given sequence, list, etc. of numpy arrays, I want them always to have this form.如果我给出了numpy arrays 的序列、列表等,我希望它们始终具有这种形式。 However, for a list of numpy arrays of the same length, eg,但是,对于相同长度的 numpy arrays 的列表,例如,

np.array(*np.array([np.arange(4), np.arange(4)], dtype=object)

gives me np.ndarray[np.ndarray[[]], dtype=object] so I came up with the workaround below.给了我np.ndarray[np.ndarray[[]], dtype=object]所以我想出了下面的解决方法。

Is there any other magic option, which could be passed to np.array() or another method which gives the desired result more directly?是否有任何其他魔术选项可以传递给np.array()或其他更直接地给出所需结果的方法?

Workaround:解决方法:

inp_arr_a = np.asarray([np.arange(4), np.arange(3)], dtype=object)
inp_arr_b = np.array([np.arange(4), np.arange(4)])

def split_to_obj_arr(arr):
    return np.delete(np.array([*arr, 'dummy'], dtype=object), -1, 0)

gives for split_to_obj_arr(inp_arr_a)给出split_to_obj_arr(inp_arr_a)

array([array([0, 1, 2, 3]), array([0, 1, 2])], dtype=object)

and for split_to_obj_arr(inp_arr_b)对于split_to_obj_arr(inp_arr_b)

array([array([0, 1, 2, 3]), array([0, 1, 2, 3])], dtype=object)

np.array(...) by design tries to return as high a dimensional numeric array as possible. np.array(...)按照设计尝试返回尽可能高的维数数组。 If the inputs are ragged it will raise a future-warning (unless you specify object dtype) and return the object array containing arrays.如果输入参差不齐,它将引发未来警告(除非您指定object )并返回包含 arrays 的 object 数组。 Or with some combinations of shapes it will raise an error.或者使用某些形状组合会引发错误。

Forcing an object dtype with the None element and then deleting that is one way around this.使用None元素强制 object dtype 然后删除这是解决此问题的一种方法。 I prefer creating the None filled array first, and assigning the elements:我更喜欢先创建None填充数组,然后分配元素:

In [80]: def foo(alist):
    ...:     res = np.empty(len(alist), object)
    ...:     res[:] = alist
    ...:     return res
    ...: 
In [81]: foo([[],[]])
Out[81]: array([list([]), list([])], dtype=object)
In [82]: foo([np.array([]),np.array([])])
Out[82]: array([array([], dtype=float64), array([], dtype=float64)], dtype=object)
In [83]: foo([np.ones((2,3)),np.zeros((2,3))])
Out[83]: 
array([array([[1., 1., 1.],
       [1., 1., 1.]]),
       array([[0., 0., 0.],
       [0., 0., 0.]])], dtype=object)
In [84]: foo([np.array([2,3]),np.array([1,2])])
Out[84]: array([array([2, 3]), array([1, 2])], dtype=object)

Creating a 2d object array like this is also possible, but trickier.像这样创建 2d object 数组也是可能的,但比较棘手。 It may be simpler to reshape a 1d as needed after.之后根据需要重塑 1d 可能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM