Python 将numpy数组转换为numpy数组数组

Python 将numpy数组转换为numpy数组数组,python,arrays,numpy,Python,Arrays,Numpy,如何以(num)pythonic方式将numpy数组a转换为numpy数组b。理想情况下,该解决方案适用于任意尺寸和数组长度 将numpy导入为np a=np.arange(12)。重塑(2,3,2) b=np.empty((2,3),dtype=object) b[0,0]=np.数组([0,1]) b[0,1]=np.数组([2,3]) b[0,2]=np.数组([4,5]) b[1,0]=np.数组([6,7]) b[1,1]=np.数组([8,9]) b[1,2]=np.数组([10,1

如何以(num)pythonic方式将numpy数组
a
转换为numpy数组
b
。理想情况下,该解决方案适用于任意尺寸和数组长度

将numpy导入为np
a=np.arange(12)。重塑(2,3,2)
b=np.empty((2,3),dtype=object)
b[0,0]=np.数组([0,1])
b[0,1]=np.数组([2,3])
b[0,2]=np.数组([4,5])
b[1,0]=np.数组([6,7])
b[1,1]=np.数组([8,9])
b[1,2]=np.数组([10,11])
首先:

In [638]: a=np.arange(12).reshape(2,3,2)
In [639]: b=np.empty((2,3),dtype=object)
In [640]: for index in np.ndindex(b.shape):
    b[index]=a[index]
   .....:     
In [641]: b
Out[641]: 
array([[array([0, 1]), array([2, 3]), array([4, 5])],
       [array([6, 7]), array([8, 9]), array([10, 11])]], dtype=object)
这并不理想,因为它使用迭代。但我想知道是否有可能以任何其他方式访问
b
的元素。通过使用
dtype=object
可以打破
numpy
已知的基本矢量化
b
本质上是一个带有
numpy
多数组形状覆盖的列表
dtype=object
在大小为2的阵列周围设置了一堵无法穿透的墙

例如,
a[:,:,0]
以(2,3)数组给出所有偶数。仅仅通过索引,我无法从
b
获取这些数字。我必须使用迭代:

[b[index][0] for index in np.ndindex(b.shape)]
# [0, 2, 4, 6, 8, 10]

np.array
根据数据的规律性,尝试生成尽可能高维的数组。为了骗它生成一个对象数组,我们必须给出一个不规则的列表或对象列表。例如,我们可以:

mylist = list(a.reshape(-1,2)) # list of arrays
mylist.append([]) # make the list irregular
b = np.array(mylist)  # array of objects
b = b[:-1].reshape(2,3)  # cleanup

最后一个解决方案建议我的第一个可以稍微清理一下:

b = np.empty((6,),dtype=object)
b[:] = list(a.reshape(-1,2))
b = b.reshape(2,3)
我怀疑在封面下,
list()
调用会进行如下迭代

[x for x in a.reshape(-1,2)]
因此,就时间而言,它可能与
ndindex
时间没有太大区别


关于
b
我没有想到的一件事是,我可以用它来做数学运算,它的概括性几乎与
a
相同:

b-10
b += 10
b *= 2

对象数据类型的另一种选择是结构化数据类型,例如

In [785]: b1=np.zeros((2,3),dtype=[('f0',int,(2,))])

In [786]: b1['f0'][:]=a

In [787]: b1
Out[787]: 
array([[([0, 1],), ([2, 3],), ([4, 5],)],
       [([6, 7],), ([8, 9],), ([10, 11],)]], 
      dtype=[('f0', '<i4', (2,))])

In [788]: b1['f0']
Out[788]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])

In [789]: b1[1,1]['f0']
Out[789]: array([8, 9])
[785]中的[code]:b1=np.zero((2,3),dtype=[('f0',int,(2,)])) 在[786]中:b1['f0'][:]=a In[787]:b1 出[787]: 数组([[([0,1],),([2,3],),([4,5],)], [([6, 7],), ([8, 9],), ([10, 11],)]], dtype=[('f0','作为开始:

In [638]: a=np.arange(12).reshape(2,3,2)
In [639]: b=np.empty((2,3),dtype=object)
In [640]: for index in np.ndindex(b.shape):
    b[index]=a[index]
   .....:     
In [641]: b
Out[641]: 
array([[array([0, 1]), array([2, 3]), array([4, 5])],
       [array([6, 7]), array([8, 9]), array([10, 11])]], dtype=object)
由于使用迭代,它并不理想。但我想知道是否有可能以任何其他方式访问
b
的元素。通过使用
dtype=object
您打破了
numpy
已知的基本向量化。
b
本质上是一个具有
numpy
多数组形状覆盖的列表。
dtype=object
在大小为2的阵列周围设置了一堵无法穿透的墙

例如,
a[:,:,0]
给出了一个(2,3)数组中的所有偶数。我无法仅通过索引从
b
中获取这些数字。我必须使用迭代:

[b[index][0] for index in np.ndindex(b.shape)]
# [0, 2, 4, 6, 8, 10]

np.array
根据数据的规律性,尝试制作尽可能高维的数组。要愚弄它制作对象数组,我们必须给出一个不规则的列表或对象列表。例如,我们可以:

mylist = list(a.reshape(-1,2)) # list of arrays
mylist.append([]) # make the list irregular
b = np.array(mylist)  # array of objects
b = b[:-1].reshape(2,3)  # cleanup

最后一个解决方案建议我的第一个可以稍微清理一下:

b = np.empty((6,),dtype=object)
b[:] = list(a.reshape(-1,2))
b = b.reshape(2,3)
我怀疑在封面下,
list()
调用会进行如下迭代

[x for x in a.reshape(-1,2)]
因此,就时间而言,它可能与
ndindex
时间没有太大区别


关于
b
我没有想到的一件事是,我可以用它来做数学运算,它的概括性几乎与
a
相同:

b-10
b += 10
b *= 2

对象数据类型的另一种选择是结构化数据类型,例如

In [785]: b1=np.zeros((2,3),dtype=[('f0',int,(2,))])

In [786]: b1['f0'][:]=a

In [787]: b1
Out[787]: 
array([[([0, 1],), ([2, 3],), ([4, 5],)],
       [([6, 7],), ([8, 9],), ([10, 11],)]], 
      dtype=[('f0', '<i4', (2,))])

In [788]: b1['f0']
Out[788]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])

In [789]: b1[1,1]['f0']
Out[789]: array([8, 9])
[785]中的[code]:b1=np.zero((2,3),dtype=[('f0',int,(2,)])) 在[786]中:b1['f0'][:]=a In[787]:b1 出[787]: 数组([[([0,1],),([2,3],),([4,5],)], [([6, 7],), ([8, 9],), ([10, 11],)]], dtype=[('f0','作为开始:

In [638]: a=np.arange(12).reshape(2,3,2)
In [639]: b=np.empty((2,3),dtype=object)
In [640]: for index in np.ndindex(b.shape):
    b[index]=a[index]
   .....:     
In [641]: b
Out[641]: 
array([[array([0, 1]), array([2, 3]), array([4, 5])],
       [array([6, 7]), array([8, 9]), array([10, 11])]], dtype=object)
由于使用迭代,它并不理想。但我想知道是否有可能以任何其他方式访问
b
的元素。通过使用
dtype=object
您打破了
numpy
已知的基本向量化。
b
本质上是一个具有
numpy
多数组形状覆盖的列表。
dtype=object
在大小为2的阵列周围设置了一堵无法穿透的墙

例如,
a[:,:,0]
给出了一个(2,3)数组中的所有偶数。我无法仅通过索引从
b
中获取这些数字。我必须使用迭代:

[b[index][0] for index in np.ndindex(b.shape)]
# [0, 2, 4, 6, 8, 10]

np.array
根据数据的规律性,尝试制作尽可能高维的数组。要愚弄它制作对象数组,我们必须给出一个不规则的列表或对象列表。例如,我们可以:

mylist = list(a.reshape(-1,2)) # list of arrays
mylist.append([]) # make the list irregular
b = np.array(mylist)  # array of objects
b = b[:-1].reshape(2,3)  # cleanup

最后一个解决方案建议我的第一个可以稍微清理一下:

b = np.empty((6,),dtype=object)
b[:] = list(a.reshape(-1,2))
b = b.reshape(2,3)
我怀疑在封面下,
list()
调用会进行如下迭代

[x for x in a.reshape(-1,2)]
因此,就时间而言,它可能与
ndindex
时间没有太大区别


关于
b
我没有想到的一件事是,我可以用它来做数学运算,它的概括性几乎与
a
相同:

b-10
b += 10
b *= 2

对象数据类型的另一种选择是结构化数据类型,例如

In [785]: b1=np.zeros((2,3),dtype=[('f0',int,(2,))])

In [786]: b1['f0'][:]=a

In [787]: b1
Out[787]: 
array([[([0, 1],), ([2, 3],), ([4, 5],)],
       [([6, 7],), ([8, 9],), ([10, 11],)]], 
      dtype=[('f0', '<i4', (2,))])

In [788]: b1['f0']
Out[788]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])

In [789]: b1[1,1]['f0']
Out[789]: array([8, 9])
[785]中的[code]:b1=np.zero((2,3),dtype=[('f0',int,(2,)])) 在[786]中:b1['f0'][:]=a In[787]:b1 出[787]: 数组([[([0,1],),([2,3],),([4,5],)], [([6, 7],), ([8, 9],), ([10, 11],)]], dtype=[('f0','作为开始:

In [638]: a=np.arange(12).reshape(2,3,2)
In [639]: b=np.empty((2,3),dtype=object)
In [640]: for index in np.ndindex(b.shape):
    b[index]=a[index]
   .....:     
In [641]: b
Out[641]: 
array([[array([0, 1]), array([2, 3]), array([4, 5])],
       [array([6, 7]), array([8, 9]), array([10, 11])]], dtype=object)
由于使用迭代,它并不理想。但我想知道是否有可能以任何其他方式访问
b
的元素。通过使用
dtype=object
您打破了
numpy
已知的基本向量化。
b
本质上是一个具有
numpy
多数组形状覆盖的列表。
dtype=object
在大小为2的阵列周围设置了一堵无法穿透的墙

例如,
a[:,:,0]
给出了一个(2,3)数组中的所有偶数。我无法仅通过索引从
b
中获取这些数字。我必须使用迭代:

[b[index][0] for index in np.ndindex(b.shape)]
# [0, 2, 4, 6, 8, 10]

np.array
根据数据的规律性,尝试生成尽可能高维的数组