Python 将列表的ndarray转换为ndarray

Python 将列表的ndarray转换为ndarray,python,numpy,numpy-ndarray,Python,Numpy,Numpy Ndarray,形状为(2,2),将其转换为(2,2,2) 解决方案应给出:nda['b'][0,0,1]==2,nda['b'][1,1,0]==9等。请尝试以下方法 nda = np.resize(nda, (2,2,2)) nda.shape 结果 (2,2,2) 你创造了一个奇怪的结构;你不能简单地重塑它: In [1]: dtypes = [('a', np.float64), ('b', object)] ...: nda = np.zeros((2,2), dtype = dtypes

形状为(2,2),将其转换为(2,2,2)

解决方案应给出:
nda['b'][0,0,1]==2
nda['b'][1,1,0]==9
等。请尝试以下方法

nda = np.resize(nda, (2,2,2))
nda.shape
结果

(2,2,2)

你创造了一个奇怪的结构;你不能简单地重塑它:

In [1]: dtypes = [('a', np.float64), ('b', object)] 
   ...: nda = np.zeros((2,2), dtype = dtypes) 
   ...:  
   ...: nda['b'][0,0] = [1,2] 
   ...: nda['b'][1,0] = [2,3] 
   ...: nda['b'][0,1] = [3,4] 
   ...: nda['b'][1,1] = [9,5]   
它有两个字段,一个带数字,另一个带列表:

In [2]: nda                                                                     
Out[2]: 
array([[(0., list([1, 2])), (0., list([3, 4]))],
       [(0., list([2, 3])), (0., list([9, 5]))]],
      dtype=[('a', '<f8'), ('b', 'O')])
如果转换为1d,我们可以
堆叠
(或者与
串联
)结合使用:

通常,如果您有一个由列表或数组组成的对象数据类型数组,则可以使用排序版本的
串联
将其合并为一个(数字)数组,但它必须是1d,一个数组/列表的“iterable”

是的,将字段解包到嵌套列表中会产生一些可以转换回(2,2,2)数组的内容:

(您不能简单地将这些数组(或列表)放回
nda
数组。数据类型错误。)

使用不同的
dtype
(`b字段是2个整数,而不是更通用的对象):

[10]中的
:数据类型=[('a',np.float64),('b',int,(2,)]
…:nda=np.zeros((2,2),dtype=dtypes)
...:  
…:nda['b'][0,0]=[1,2]
…:nda['b'][1,0]=[2,3]
…:nda['b'][0,1]=[3,4]
…:nda['b'][1,1]=[9,5]
在[11]:nda中
出[11]:
数组([[(0.,[1,2]),(0.,[3,4]),
[(0., [2, 3]), (0., [9, 5])]],

dtype=[('a','
np.array(nda['b'].tolist()).shape
?解决方案应提供写入的可能性:
nda['b'][0,0,0]
In [2]: nda                                                                     
Out[2]: 
array([[(0., list([1, 2])), (0., list([3, 4]))],
       [(0., list([2, 3])), (0., list([9, 5]))]],
      dtype=[('a', '<f8'), ('b', 'O')])
In [3]: nda['b']                                                                
Out[3]: 
array([[list([1, 2]), list([3, 4])],
       [list([2, 3]), list([9, 5])]], dtype=object)
In [4]: _.shape                                                                 
Out[4]: (2, 2)
In [5]: nda['b'].ravel()                                                        
Out[5]: 
array([list([1, 2]), list([3, 4]), list([2, 3]), list([9, 5])],
      dtype=object)
In [6]: np.stack(nda['b'].ravel())                                              
Out[6]: 
array([[1, 2],
       [3, 4],
       [2, 3],
       [9, 5]])
In [7]: np.stack(nda['b'].ravel()).reshape(2,2,2)                               
Out[7]: 
array([[[1, 2],
        [3, 4]],

       [[2, 3],
        [9, 5]]])
In [14]: _2['b'].tolist()                                                       
Out[14]: [[[1, 2], [3, 4]], [[2, 3], [9, 5]]]
In [10]: dtypes = [('a', np.float64), ('b', int, (2,))] 
    ...: nda = np.zeros((2,2), dtype = dtypes) 
    ...:  
    ...: nda['b'][0,0] = [1,2] 
    ...: nda['b'][1,0] = [2,3] 
    ...: nda['b'][0,1] = [3,4] 
    ...: nda['b'][1,1] = [9,5]                                                  
In [11]: nda                                                                    
Out[11]: 
array([[(0., [1, 2]), (0., [3, 4])],
       [(0., [2, 3]), (0., [9, 5])]],
      dtype=[('a', '<f8'), ('b', '<i8', (2,))])
In [12]: nda['b']                                                               
Out[12]: 
array([[[1, 2],
        [3, 4]],

       [[2, 3],
        [9, 5]]])