简体   繁体   中英

Saving numpy array to a binary file

I saved my numpy array to a binary file as np.save("image_mean.npy", averaged_mean) . When I open the file, observed the bianry file's header as “NUMPY V {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), } .

My query is what NUMPY V refers to? If I want it as NUMPY F “NUMPY F {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), } , how can I change in np.save API?

V is the length of the header data (include space paddings and the terminating newline).

As given in the documentation -

  1. The first 6 bytes are a magic string: exactly “x93NUMPY”.

  2. The next 1 byte is an unsigned byte: the major version number of the file format, eg x01.

  3. The next 1 byte is an unsigned byte: the minor version number of the file format, eg x00. Note: the version of the file format is not tied to the version of the numpy package.

  4. The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN.

  5. The next HEADER_LEN bytes form the header data describing the array's format. It is an ASCII string which contains a Python literal expression of a dictionary. It is terminated by a newline ('n') and padded with spaces ('x20') to make the total length of the magic string + 4 + HEADER_LEN be evenly divisible by 16 for alignment purposes.

The length of the header data from your example (including a single newline) comes to 71 . So that makes magic_string + 4 + HEADER_LEN equal 81 , which is not divisible by 16 , so the next divisible number is 96 , hence the header data is padded with 15 spaces so that that total length becomes equal to 96 . This makes the header length to be - 86 . Which is V .

>>> chr(86)
'V'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM