Skip to content Skip to sidebar Skip to footer

How To Convert Regular Numpy Array To Record Array?

I read in a sequence of numbers with np.array(f.read().split(),dtype=np.float64) Then I convert this to a 2-D array using np.reshape(). After this, how do to convert arr to a rec

Solution 1:

To convert a plain numpy array to a structured array, use view:

import numpy as np

filename = 'unstructured-file.txt'
nfields = 3
names = ('r','g','b')
with open(filename,'r') as f:
    arr = np.array(f.read().split(),dtype=np.float64)
    arr = arr.reshape(-1,nfields)
    out = arr.view(dtype=zip(names,['float64']*len(names))).copy()

Post a Comment for "How To Convert Regular Numpy Array To Record Array?"