Convert structured array with various numeric data types to regular array

后端 未结 4 1271
天命终不由人
天命终不由人 2021-01-06 11:24

Suppose I have a NumPy structured array with various numeric datatypes. As a basic example,

my_data = np.array( [(17, 182.1),  (19, 175.6)],  dtype=\'i2,f4\         


        
4条回答
  •  逝去的感伤
    2021-01-06 12:16

    The obvious way works:

    >>> my_data
    array([(17, 182.10000610351562), (19, 175.60000610351562)],
          dtype=[('f0', '>> n = len(my_data.dtype.names)  # n == 2
    >>> my_data.astype(','.join(['f4']*n))
    array([(17.0, 182.10000610351562), (19.0, 175.60000610351562)],
          dtype=[('f0', '>> my_data.astype(','.join(['f4']*n)).view('f4')
    array([  17.       ,  182.1000061,   19.       ,  175.6000061], dtype=float32)
    >>> my_data.astype(','.join(['f4']*n)).view('f4').reshape(-1, n)
    array([[  17.       ,  182.1000061],
           [  19.       ,  175.6000061]], dtype=float32)
    

提交回复
热议问题