Python HDF文件未使用h5py正确保存

Python HDF文件未使用h5py正确保存,python,numpy,hdf5,h5py,Python,Numpy,Hdf5,H5py,我正试图用h5py将numpy数组保存在HDF文件中,如下所示: with h5py.File("mfcc_aligned.hdf", "w") as aligned_f: # do stuff to create two numpy arrays, training_X and training_Y print(len(training_X)) # this returns the number of elements I expect in the the numpy arr

我正试图用h5py将numpy数组保存在HDF文件中,如下所示:

with h5py.File("mfcc_aligned.hdf", "w") as aligned_f:
    # do stuff to create two numpy arrays, training_X and training_Y
    print(len(training_X)) # this returns the number of elements I expect in the the numpy arr
    aligned_f.create_dataset("train_X", data=training_X)
    aligned_f.create_dataset("train_Y", data=training_Y)
    # if I add a line here to access the datasets I just created, I see that aligned_f does indeed have two keys train_X and train_Y with the shapes I expect
但是,当程序结束时,我检查文件
mfcc_aligned.hdf
,它正好是800字节(比我预期的小得多),并且没有键。我不知道这里发生了什么事

提前感谢您的任何见解

您是否尝试过(未在评论中格式化):


我对您的代码没有任何问题:

In [59]: import h5py
In [60]: training_X = np.arange(12).reshape(3,4)
In [61]: training_Y = np.arange(3).reshape(3,1)
In [62]: with h5py.File("mfcc_aligned.hdf", "w") as aligned_f:
    ...:     # do stuff to create two numpy arrays, training_X and training_Y
    ...:     print(len(training_X)) # this returns the number of elements I expe
    ...: ct in the the numpy arr
    ...:     aligned_f.create_dataset("train_X", data=training_X)
    ...:     aligned_f.create_dataset("train_Y", data=training_Y)
    ...:     
3
In [63]: f = h5py.File("mfcc_aligned.hdf")
In [64]: list(f.keys())
Out[64]: ['train_X', 'train_Y']
In [66]: f['train_X'].value
Out[66]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [67]: f['train_Y'][:]
Out[67]: 
array([[0],
       [1],
       [2]])
In [68]: ll mfcc_aligned.hdf
-rw-rw-r-- 1 paul 2204 Mar 31 14:10 mfcc_aligned.hdf

您是否尝试过:使用h5py.File('mfcc_aligned.hdf','r')作为hf:print=hf['train_X'][:]这是什么?
In [59]: import h5py
In [60]: training_X = np.arange(12).reshape(3,4)
In [61]: training_Y = np.arange(3).reshape(3,1)
In [62]: with h5py.File("mfcc_aligned.hdf", "w") as aligned_f:
    ...:     # do stuff to create two numpy arrays, training_X and training_Y
    ...:     print(len(training_X)) # this returns the number of elements I expe
    ...: ct in the the numpy arr
    ...:     aligned_f.create_dataset("train_X", data=training_X)
    ...:     aligned_f.create_dataset("train_Y", data=training_Y)
    ...:     
3
In [63]: f = h5py.File("mfcc_aligned.hdf")
In [64]: list(f.keys())
Out[64]: ['train_X', 'train_Y']
In [66]: f['train_X'].value
Out[66]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [67]: f['train_Y'][:]
Out[67]: 
array([[0],
       [1],
       [2]])
In [68]: ll mfcc_aligned.hdf
-rw-rw-r-- 1 paul 2204 Mar 31 14:10 mfcc_aligned.hdf