转换numpy的数组numpy的数组的数组 [英] Convert a numpy array to an array of numpy arrays

查看:213
本文介绍了转换numpy的数组numpy的数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以转换numpy的阵列 A 来numpy的阵列 B 在(NUM)Python的方式。解决方案最好应为任意尺寸和长度数组工作。

导入numpy的是NP一个= np.arange(12).reshape(2,3,2)B = np.empty((2,3),DTYPE =对象)
B〔0,0〕= np.array([0,1])
B〔0,1] = np.array([2,3])
B〔0,2〕= np.array([4,5])
B〔1,0〕= np.array([6,7])
B〔1,1] = np.array([8,9])
B〔1,2] = np.array([10,11])


解决方案

对于启动:

 在[638]:A = np.arange(12).reshape(2,3,2)
在[639]:B = np.empty((2,3),DTYPE =对象)
在[640]:在np.ndindex指数(b.shape):
    B〔指数] = A [指数]
   .....:
在[641]:乙
出[641]:
阵列([[阵列([0,1]),阵列([2,3]),阵列([4,5])],
       [阵列([6,7]),阵列([8,9]),阵列([10,11])]] DTYPE =对象)

这不是理想的,因为它使用的迭代。但我不知道它是否是可以访问元素以任何其他方式。通过使用 DTYPE =对象你打破 numpy的而闻名的基本矢量。 B 实质上是 numpy的多阵列状覆盖列表。 DTYPE =对象提出坚不可摧的墙的面积约为2数组。

例如, A [:,:,0] 给我所有的偶数,在(2,3)阵列。我无法从这些数字b 只索引。我必须使用迭代:

  [B [指数] [0]指数np.ndindex(b.shape)
#[0,2,4,6,8,10]


np.array 试图使最高维数组,它可以,考虑到数据的规律性。愚弄它变成做对象的数组,我们必须给列表或物体的不规则列表。例如,我们可以:

  MYLIST =列表(a.reshape(-1,2))数组列表#
mylist.append([])#榜上无名不规则
B = np.array(MYLIST)#对象数组
B = [: - 1] .reshape(2,3)#清理


最后的解决方案表明,我的第一个可以被清理了一下:

  B = np.empty((6日),DTYPE =对象)
B〔:] =列表(a.reshape(-1,2))
B = b.reshape(2,3)

我怀疑在幕后,在的list()呼叫确实迭代像

  [X在a.reshape X(-1,2)]

所以时间上来说可能不是从 ndindex 的时间差不多。


一件事,我没想到约 B 是我可以做数学题就可以了,用几乎相同的共性作为 A

  B-10
B + = 10
的b * = 2


到对象DTYPE另一种方法是一个结构DTYPE,例如

 在[785]:B1 = np.zeros((2,3),DTYPE = [('F0',INT,(2))])在[786]:B1 ['F0'] [:] =一在[787]:B1
出[787]:
阵列([[([0,1],),([2,3],),([4,5],)]
       [(6,7],),([8,9],),([10,11],)]]
      DTYPE = [('F0','&下; 6-14',(2,))]​​)在[788]:B1 ['F0']
出[788]:
阵列([[[0,1],
        [2,3]
        [4,5],       [[6,7]
        [8,9]
        [10,11]]])在[789]:B1 [1,1] ['F0']
出[789]:阵列([8,9])

B B1 可添加: B + B1 (产生一个对象 DTYPE)。奇妙而又奇妙!

How can I convert numpy array a to numpy array b in a (num)pythonic way. Solution should ideally work for arbitrary dimensions and array lengths.

import numpy as np

a=np.arange(12).reshape(2,3,2)

b=np.empty((2,3),dtype=object)
b[0,0]=np.array([0,1])
b[0,1]=np.array([2,3])
b[0,2]=np.array([4,5])
b[1,0]=np.array([6,7])
b[1,1]=np.array([8,9])
b[1,2]=np.array([10,11])

解决方案

For a start:

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)

It's not ideal since it uses iteration. But I wonder whether it is even possible to access the elements of b in any other way. By using dtype=object you break the basic vectorization that numpy is known for. b is essentially a list with numpy multiarray shape overlay. dtype=object puts an impenetrable wall around those size 2 arrays.

For example, a[:,:,0] gives me all the even numbers, in a (2,3) array. I can't get those numbers from b with just indexing. I have to use iteration:

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


np.array tries to make the highest dimension array that it can, given the regularity of the data. To fool it into making an array of objects, we have to give an irregular list of lists or objects. For example we could:

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


The last solution suggests that my first one can be cleaned up a bit:

b = np.empty((6,),dtype=object)
b[:] = list(a.reshape(-1,2))
b = b.reshape(2,3)

I suspect that under the covers, the list() call does an iteration like

[x for x in a.reshape(-1,2)]

So time wise it might not be much different from the ndindex time.


One thing that I wasn't expecting about b is that I can do math on it, with nearly the same generality as on a:

b-10
b += 10
b *= 2


An alternative to an object dtype would be a structured dtype, e.g.

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])

And b and b1 can be added: b+b1 (producing an object dtype). Curiouser and curiouser!

这篇关于转换numpy的数组numpy的数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆