首页 > 解决方案 > 2-D ndarray 的 Numpy fromfile 的填充顺序是什么?

问题描述

我正在尝试使用该numpy.fromfile()函数读取结构化二进制文件。就我而言,我有一个numpy.dtype()用于定义用户定义的数据类型以与np.fromfile().
我将在这里复制数据结构的相关部分(因为完整的结构相当长):

('RawData', np.int32, (2, BlockSize))  

这会将BlockSize*2int32 的数量读入字段RawData,将产生一个2xBlockSize矩阵。这是我遇到麻烦的地方,因为我想复制 Matlab 的fread()函数的行为,其中矩阵按列顺序填充。至于 NumPy 的fromfile(),没有提到(至少我找不到)。

fromfile()NumPy应该像 Matlab 一样工作并不重要fread(),但我必须知道 NumPy 是如何fromfile()工作的以进行相应的编码。

fromfile()现在的问题是,在使用自定义数据类型时,NumPy 函数中二维数组的填充顺序是什么?

标签: pythonpython-3.xnumpycustom-data-type

解决方案


fromfiletofile读/写平面,一维数组:

In [204]: x = np.arange(1,11).astype('int32')                                                          
In [205]: x.tofile('data615')                                                                          

fromfile返回一维数组:

In [206]: np.fromfile('data615',np.int32)                                                              
Out[206]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int32)

x.reshape(2,5).tofile(...)会保存同样的东西。 tofile不保存dtypeshape信息。

重新整形为 2d,默认顺序是 'C':

In [207]: np.fromfile('data615',np.int32).reshape(2,5)                                                 
Out[207]: 
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]], dtype=int32)

但它可以更改为 MATLAB,如:

In [208]: np.fromfile('data615',np.int32).reshape(2,5, order='F')                                      
Out[208]: 
array([[ 1,  3,  5,  7,  9],
       [ 2,  4,  6,  8, 10]], dtype=int32)

底层databuffer是相同的,只是一个一维字节数组。

编辑

该文件可以读取为 2 整数结构:

In [249]: np.fromfile('data615','i4,i4')                                                               
Out[249]: 
array([(1,  2), (3,  4), (5,  6), (7,  8), (9, 10)],
      dtype=[('f0', '<i4'), ('f1', '<i4')])
In [250]: _['f0']                                                                                      
Out[250]: array([1, 3, 5, 7, 9], dtype=int32)

它仍然是一个一维数组,但数字按 2 分组。

转换为复杂:

In [252]: xx = np.fromfile('data615','i4,i4')                                                          
In [253]: xx['f0']+1j*xx['f1']                                                                         
Out[253]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j])
In [254]: _.dtype                                                                                      
Out[254]: dtype('complex128')

如果数据已经保存为浮点数,我们可以直接将它们加载为复数:

In [255]: x.astype(np.float32).tofile('data615f')                                                      
In [257]: xx = np.fromfile('data615f',np.complex64)                                                    
In [258]: xx                                                                                           
Out[258]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j], dtype=complex64)

从整数序列中获取复数的另一种方法:

In [261]: np.fromfile('data615', np.int32).reshape(5,2)                                                
Out[261]: 
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]], dtype=int32)
In [262]: xx = np.fromfile('data615', np.int32).reshape(5,2)                                           
In [263]: xx[:,0]+1j*xx[:,1]                                                                           
Out[263]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j])

推荐阅读