我正在尝试使用 numpy.fromfile() 函数读取结构化二进制文件。就我而言,我有一个 numpy.dtype()
,用于定义与 np.fromfile()
一起使用的用户定义的数据类型。
我将在这里重现数据结构的相关部分(因为完整的结构相当长):
('RawData', np.int32, (2, BlockSize))
这会将 BlockSize*2
个 int32 读取到字段 RawData
中,并生成一个 2xBlockSize
矩阵。这就是我遇到麻烦的地方,因为我想复制 Matlab fread() 的行为函数,其中矩阵填充为 column order 。至于NumPy的fromfile()
,没有提到这一点(至少我找不到它)。
NumPy 的 fromfile()
应该像 Matlab 的 fread()
一样工作并不重要,但我必须知道 NumPy 的 fromfile()
是如何工作的> 进行相应的编码。
现在的问题是,使用自定义数据类型时,NumPy fromfile()
函数中二维数组的填充顺序是什么?
最佳答案
fromfile
和 tofile
读/写平面、一维、数组:
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
不保存dtype
或shape
信息。
reshape 为二维,默认顺序为“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')
如果数据已保存为 float ,我们可以直接将它们加载为复数:
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])
关于python - 二维 ndarray 的 Numpy fromfile 的填充顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61545395/