[英]How do I read Row Wise instead of column wise with h5py?
I have this matlab file that is of shape 70x10,000,000 (10,000,000 columns 70 rows) Whats annoying is that when I run this line of code which is supposed to print that chunk of data,我有这个 matlab 文件,其形状为 70x10,000,000(10,000,000 列,70 行)
f = h5py.File(filepath, 'r')
item = list(f.items())[0][1]
print(item)
it reshapes it into 10,000,000x70 (10,000,000 rows, 70 columns) Is there a way to keep the original shape?它将其重塑为 10,000,000x70(10,000,000 行,70 列)有没有办法保持原始形状?
h5py returns HDF5 data as Numpy arrays. h5py 将 HDF5 数据返回为 Numpy arrays。 So, the key to using h5py is using Numpy methods when needed.
因此,使用 h5py 的关键是在需要时使用 Numpy 方法。 You can easily transpose an array using
np.transpose()
.您可以使用
np.transpose()
轻松转置数组。 A simple example is provided below.下面提供了一个简单的示例。 It creates a HDF5 file with 2 datasets: 1) an array with shape (20,5), and 2) the transposed array with shape (5,20).
它创建一个包含 2 个数据集的 HDF5 文件:1) 形状为 (20,5) 的数组,以及 2) 形状为 (5,20) 的转置数组。 Then it extracts the 2 arrays and uses
np.transpose()
to switch row/column order.然后它提取 2 arrays 并使用
np.transpose()
切换行/列顺序。
with h5py.File('SO_67031436','w') as h5w:
arr = np.arange(100.).reshape(20,5)
h5w.create_dataset('ds_1',data=arr)
h5w.create_dataset('ds_1t',data=np.transpose(arr))
with h5py.File('SO_67031436','r') as h5r:
for name in h5r:
print(name,',shape=',h5r[name].shape)
arr=np.transpose(h5r[name][:])
print('transposed shape=',arr.shape)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.