【问题标题】:How to turn a numpy array to a numpy object?如何将 numpy 数组转换为 numpy 对象?
【发布时间】:2020-07-15 19:06:33
【问题描述】:

我有一个如下的 NumPy 数组:

[[[  0   0]]

 [[  0 479]]

 [[639 479]]

 [[639   0]]]

我想把它转换成这样的东西:

[(  0   0)

 (  0 479)

 (639 479)

 (639   0), dtype=dtype([('x', '<i2'), ('y', '<i2')])]

我曾尝试使用以下功能:

def flat(contour):
        for point in contour:
            yield tuple(point[0])

像这样:

contour=np.fromiter(flat(contour),Point)

但这给了我以下错误:ValueError: setting an array element with a sequence. 那么如何将 NumPy 数组的某些“维度”转换为 NumPy“对象”(或在 NumPy 中调用的其他任何内容)

【问题讨论】:

    标签: python numpy


    【解决方案1】:
    In [117]: arr = np.array([[[0,0]],[[0,479]],[[639,479]],[[639,0]]])                                  
    In [118]: arr                                                                                        
    Out[118]: 
    array([[[  0,   0]],
    
           [[  0, 479]],
    
           [[639, 479]],
    
           [[639,   0]]])
    In [119]: arr.shape                                                                                  
    Out[119]: (4, 1, 2)
    

    你显然想要structured arrayhttps://numpy.org/devdocs/user/basics.rec.html#

    有一个方便的工具可以将数值数组转换为结构化数组:

    In [120]: import numpy.lib.recfunctions as rf                                                        
    In [121]: rf.unstructured_to_structured(arr,names=['x','y'])                                         
    Out[121]: 
    array([[(  0,   0)],
           [(  0, 479)],
           [(639, 479)],
           [(639,   0)]], dtype=[('x', '<i8'), ('y', '<i8')])
    In [122]: _.shape                                                                                    
    Out[122]: (4, 1)
    

    或使用您想要的数据类型:

    In [126]: rf.unstructured_to_structured(arr,dtype=np.dtype([('x', '<i2'), ('y', '<i2')]))            
    Out[126]: 
    array([[(  0,   0)],
           [(  0, 479)],
           [(639, 479)],
           [(639,   0)]], dtype=[('x', '<i2'), ('y', '<i2')])
    

    或创建一个具有所需数据类型和形状的“空白”数组,并分配字段:

    In [127]: res = np.zeros((4,1), dtype=np.dtype([('x', '<i2'), ('y', '<i2')]))                        
    In [128]: res                                                                                        
    Out[128]: 
    array([[(0, 0)],
           [(0, 0)],
           [(0, 0)],
           [(0, 0)]], dtype=[('x', '<i2'), ('y', '<i2')])
    In [129]: res['x'] = arr[:,:,0]                                                                      
    In [130]: res['y'] = arr[:,:,1]                                                                      
    In [131]: res                                                                                        
    Out[131]: 
    array([[(  0,   0)],
           [(  0, 479)],
           [(639, 479)],
           [(639,   0)]], dtype=[('x', '<i2'), ('y', '<i2')])
    

    或来自元组列表(在您的情况下为元组列表):

    In [132]: arr.tolist()                                                                               
    Out[132]: [[[0, 0]], [[0, 479]], [[639, 479]], [[639, 0]]]
    
    In [134]: [[tuple(i) for i in x] for x in arr.tolist()]                                              
    Out[134]: [[(0, 0)], [(0, 479)], [(639, 479)], [(639, 0)]]
    
    In [135]: np.array([[tuple(i) for i in x] for x in arr.tolist()], dtype=[('x', '<i2'), ('y', '<i2')])
         ...:                                                                                            
    Out[135]: 
    array([[(  0,   0)],
           [(  0, 479)],
           [(639, 479)],
           [(639,   0)]], dtype=[('x', '<i2'), ('y', '<i2')])
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      相关资源
      最近更新 更多