如何连接Numpy的ndarray来存储对象? [英] How to interface Numpy's ndarrays to store objects in?
问题描述
将numpy导入为npa1 = np.array([[1,2],[3,4],[5,6]])a2 = np.array([7,3])a3 = np.array([1])# 我想做的事a_parent = np.ndarray(a1,a2,a3)#期望的输出打印(a_parent [0])>>>[[1 2][3 4][5 6]打印(a_parent [1])>>>[7 3]打印(a_parent [2])>>>[1]
我知道这是可能的,因为当我从 scipy.io
库中使用 loadmat
导入Matlab单元数据时,数据将转换为numpy的 ndarray 代码>,并且其行为与上述代码完全相同.我已经浏览了 numpy文档,我可以找不到一个可行的例子来说明我自己该怎么做.
在[5]中:a1 = np.array([[1,2,3,[3,4],[5,6]])...:a2 = np.array([7,3])...:a3 = np.array([1])
最好的方法是制作具有所需dtype和形状的'空白'数组:
在[6]中:a_parent = np.empty(3,object)在[7]中:a_parentOut [7]:array([None,None,None],dtype = object)
并从所需数组(或其他对象)的列表中填充"它:
在[13]中:a_parent [:] = [a1,a2,a3]在[14]中:a_parent出[14]:array([array([[1,2],[3,4],[5,6]]),数组([7,3]),array([1])],dtype = object)
我确定 loadmat
使用此方法.
将列表直接传递到 np.array
也许可行,但是v1.19希望我们包括 object
dtype:
在[10]中:np.array([a1,a2,a3])/usr/local/bin/ipython3:1:VisibleDeprecationWarning:不推荐使用粗糙的嵌套序列(它是具有不同长度或形状的list-or-tuples或ndarray的列表或元组)创建ndarray.如果您打算这样做,则在创建ndarray时必须指定"dtype = object"#!/usr/bin/python3出[10]:array([array([[1,2],[3,4],[5,6]]),数组([7,3]),array([1])],dtype = object)
如果数组的形状都相同,则此方法不起作用:
在[11]中:np.array([a1,a1])出[11]:数组([[[[1,2],[3,4],[5,6],[[1,2],[3,4],[5,6]]]
对于某些形状组合,我们会出错.
在[15]中:a_parent [:] = [a3,a3,a3]在[16]中:a_parentOut [16]:array([array([1]),array([1]),array([1])],dtype = object)
I want to store a series of differently sized arrays into one "parent" array. Like this:
import numpy as np
a1 = np.array([[1,2], [3,4], [5,6]])
a2 = np.array([7,3])
a3 = np.array([1])
# What I want to do
a_parent = np.ndarray(a1, a2, a3)
# Desired output
print(a_parent[0])
>>> [[1 2]
[3 4]
[5 6]]
print(a_parent[1])
>>> [7 3]
print(a_parent[2])
>>> [1]
I know this is possible because when I import Matlab cell data using loadmat
from the scipy.io
library the data gets converted to a numpy ndarray
and it behaves exactly like above. I've looked through the numpy docs and I can't find a working example to show how I could do this myself.
In [5]: a1 = np.array([[1,2], [3,4], [5,6]])
...: a2 = np.array([7,3])
...: a3 = np.array([1])
The best way is to make a 'blank' array of the desired dtype and shape:
In [6]: a_parent = np.empty(3, object)
In [7]: a_parent
Out[7]: array([None, None, None], dtype=object)
and 'fill' it from a list of the desired arrays (or other objects):
In [13]: a_parent[:] = [a1,a2,a3]
In [14]: a_parent
Out[14]:
array([array([[1, 2],
[3, 4],
[5, 6]]), array([7, 3]),
array([1])], dtype=object)
I'm sure loadmat
uses this method.
Passing the list directly to np.array
may work, but v1.19 wants us to include the object
dtype:
In [10]: np.array([a1,a2,a3])
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
#!/usr/bin/python3
Out[10]:
array([array([[1, 2],
[3, 4],
[5, 6]]), array([7, 3]),
array([1])], dtype=object)
This does not work if the arrays are all the same shape:
In [11]: np.array([a1,a1])
Out[11]:
array([[[1, 2],
[3, 4],
[5, 6]],
[[1, 2],
[3, 4],
[5, 6]]])
And for some shape combinations we get an error.
In [15]: a_parent[:] = [a3,a3,a3]
In [16]: a_parent
Out[16]: array([array([1]), array([1]), array([1])], dtype=object)
这篇关于如何连接Numpy的ndarray来存储对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!