如何在hdf5文件python中保存提取的功能列表

问题描述 投票:0回答:1

我正在从音频文件中提取一些功能,并将其保存在列表中,然后将列表保存在hdf5文件中,但这会导致错误。以前,我直接将功能保存在hdf5文件中,但它只会覆盖所有值,并且仅保存最后一个值。

ampList = []
 mffcslist = []
 centroidlist = []
 i = 0


    ampList.append(Xdb)        # saving extracted feature in a list
    mffcslist.append(mfccs)    
    centroidlist.append(spectral_centroids)

with h5py.File('C:/Users/Aweem Ashar/Desktop/feature.h5', 'a') as f:
    f.close()

    for i in range(len(audio_path)):
        #print(ampList[i])

        f.create_dataset("amplitude", data=ampList[i])
        f.create_dataset("MffC", data=mffcslist[i])
        f.create_dataset("spectral", data=centroidlist[i])

    # plt.show()      # To view Wave graph
python-3.x deep-learning hdf5 h5py
1个回答
0
投票

我在写评论时没有仔细看您的代码。我只是意识到您一次将一个列表元素加载到列表数据中。使用Numpy数组有很多更好/更快的方法。我不知道您正在使用哪种数据,因此创建了一个非常简单的示例,其中ampList中有一些浮点数。我使用np.asarray()将列表转换为Numpy数组,并以1张照片的速度加载到数据集中。更加轻松和紧凑。此方法(带有np.asarray())适用于具有通用类型(所有浮点或所有整数)的元素的任何列表。

我的例子:

import h5py
import numpy as np

ampList = [ 20., 11., 33., 40., 100. ]

with h5py.File('SO_58092765.h5','w') as h5f:
    h5f.create_dataset("amplitude", data=np.asarray(ampList) )
© www.soinside.com 2019 - 2024. All rights reserved.