I would like to write Numpy arrays with shape (3, 225, 400) into a binary file.
These arrays are basically generated by using a screen buffer, and each screen has a label. My goal is to save each screen with its label.
numpy.save receives only two arguments: file pointer and array to be saved. The only option seems to be appending labels to arrays as follows:
with open(file, 'wb') as f:
np.save(f, np.append(buffer, [label]) )
However, I would not prefer this. Another approach might be saving only the array and then writing " \t label " like regular binary writing:
with open(file, 'wb') as f:
np.save(f, buffer)
f.write("\t" + label)
I am not sure whether np.save moves the file pointer to new line after saving.
Considering the fact that I will save hundreds of thousands of array-label pairs in a high frequency, what would you suggest in terms of efficiency?
dtypeofbuffer? Probably some numeric. What is the nature oflabel? Make sure you look atnp.append(buffer,[label])before you save it. Check the shape and dtype, as well as some values. There isn't a way of adding a label attribute to an array, either before or duringnp.save. It is probably best to use the file name as 'label', or have a separate file that pairs filenames and labels. Or look into using HDF5 files (h5py), which can save multiple arrays, along with 'label' attributes.