gpt4 book ai didi

python - 将 numpy 数组保存到二进制文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:21 27 4
gpt4 key购买 nike

我将我的 numpy 数组保存到一个二进制文件中作为 np.save("image_mean.npy", averaged_mean) .当我打开文件时,观察到 bianry 文件的标题为 “NUMPY V {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), } .

我的查询是什么NUMPY V指的是?如果我想要它作为 NUMPY F “NUMPY F {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), } , 我怎样才能改变 np.save API?

最佳答案

V 是 header 数据的长度(包括空格填充和终止换行符)。

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, e.g. x01.

  3. The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. 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.

您示例中的标题数据的长度(包括单个换行符)达到 71 。所以这使得 magic_string + 4 + HEADER_LEN 等于 81 ,它不能被 16 整除,所以下一个可整除的数字是 96 ,因此标题数据用 15 个空格填充所以该总长度等于 96 。这使得 header 长度为 - 86 。这是 V

>>> chr(86)
'V'

关于python - 将 numpy 数组保存到二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com