Skip to content Skip to sidebar Skip to footer

What Is The Way Data Is Stored In *.npy?

I'm saving NumPy arrays using numpy.save function. I want other developers to have capability to read data from those file using C language. So I need to know,how numpy organizes b

Solution 1:

The npy file format is documented in numpy's NEP 1 — A Simple File Format for NumPy Arrays.

For instance, the code

>>>dt=numpy.dtype([('outer','(3,)<i4'),...                ('outer2',[('inner','(10,)<i4'),('inner2','f8')])])>>>a=numpy.array([((1,2,3),((10,11,12,13,14,15,16,17,18,19),3.14)),...               ((4,5,6),((-1,-2,-3,-4,-5,-6,-7,-8,-9,-20),6.28))],dt)>>>numpy.save('1.npy', a)

results in the file:

934E 554D 5059                      magic ("\x93NUMPY")01                                     major version (1)00                                     minor version (0)9600                                  HEADER_LEN (0x0096=150)7B 276465736372273A 205B 28276F75746572272C 20273C 6934272C 2028332C 29292C 2028276F7574657232272C 205B 2827696E 6E 6572272C 
20273C 6934272C 202831302C 29292C 202827696E 6E 657232                Header, describing the data structure
272C 20273C 663827"{'descr': [('outer', '<i4', (3,)),
29 5D 29 5D 2C 20 27 66                            ('outer2', [
6F 72 74 72 61 6E 5F 6F                               ('inner', '<i4', (10,)), 
72 64 65 72 27 3A 20 46                               ('inner2', '<f8')]
61 6C 73 65 2C 20 27 73                            )],
68 61 70 65 27 3A 20 28                  'fortran_order': False,
32 2C 29 2C 20 7D 20 20                  'shape': (2,), }"202020202020202020202020200A 

010000000200000003000000(1,2,3)0A 0000000B 0000000C 0000000D 0000000E 0000000F00000010000000110000001200000013000000(10,11,12,13,14,15,16,17,18,19)1F85 EB 51 B8 1E 09403.14040000000500000006000000(4,5,6)
FF FF FF FF FE FF FF FF FD FF FF FF
FC FF FF FF FB FF FF FF FA FF FF FF
F9 FF FF FF F8 FF FF FF F7 FF FF FF 
EC FF FF FF                            (-1,-2,-3,-4,-5,-6,-7,-8,-9,-20)1F85 EB 51 B8 1E 19406.28

Solution 2:

The format is described in numpy/lib/format.py, where you can also see the Python source code used to load npy files. np.load is defined here.

Post a Comment for "What Is The Way Data Is Stored In *.npy?"