是否有一种方法可以将假定存储为向量的字符串转换为向量?

时间:2020-07-30 23:07:21

标签: python mfcc

我将mfcc数据保存到了熊猫数据框中。我的问题是,当我想将其保存为数字矩阵时,将其保存为str类型。

i=0
for file in train_csv2['fname']:
signal, sr = librosa.load(file)
mfcc = librosa.feature.mfcc(signal, sr=sr,
                            n_mfcc=13, 
                            n_fft=2048, 
                            hop_length=512)
mfcc = mfcc.T
train_csv2.at[i, 'FFT'] = mfcc
i += 1

运行此代码后,我的数据框train_csv2已将所有值存储为字符串。由于我使用了大量的音频文件,我花了3.5个小时来运行它。

有没有办法转换字符串...

[[-8.46002580e+00  7.08650460e-01 -1.35593844e+01 ... -9.93381115e+00\n  -3.62304403e+00  4.12729614e+00]\n [ 9.54518993e+00 -2.74505349e+00 -1.17114286e+01 ... -8.55533103e+00\n   1.88484478e-01  7.91402262e+00]\n [ 1.49292642e+01 -1.56909610e+01 -1.10280461e+01 ... -8.37234667e+00\n   9.35007917e+00  1.34211000e+01]\n ...\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]]

进入列表/向量?

2 个答案:

答案 0 :(得分:1)

我尝试了@Nicolescu建议的上述方法,并且在进行了一些tweeking后就可以了! (它最初返回了一个空列表的列表)

由于您使用的是DataFrame,因此我建议对上述答案进行一些更改:

def clean_feat(input):    
    first_clean = input[1:len(input) - 1]
    second_clean = first_clean.replace('\n', '')
    last = second_clean.split()
    return_list = []
    for array in last:
        return_list.append([float(array)])
    return return_list

results = [clean_feat(x) for x in train_csv2['FFT']]

希望能提供帮助,并感谢Nicolescu Ionut Lucian的最初想法帮助我格式化了自己的代码!

答案 1 :(得分:0)

尝试以下代码:

 def test_1(self):
    input = '[[-8.46002580e+00  7.08650460e-01 -1.35593844e+01 -9.93381115e+00\n  -3.62304403e+00  4.12729614e+00]\n [ 9.54518993e+00 -2.74505349e+00 -1.17114286e+01 -8.55533103e+00\n   1.88484478e-01  7.91402262e+00]\n [ 1.49292642e+01 -1.56909610e+01 -1.10280461e+01 -8.37234667e+00\n   9.35007917e+00  1.34211000e+01]\n ...\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]\n [-6.22648700e+02  0.00000000e+00  0.00000000e+00 ...  0.00000000e+00\n   0.00000000e+00  0.00000000e+00]]'
    first_clean = input[1:len(input) - 1]
    second_clean = first_clean.replace('\n', '')
    third_clean = re.findall('\[(.*?)\]', second_clean)
    return_list = []
    for array in third_clean:
        return_list.append([float(arr) for arr in array.split(' ') if arr])
    return return_list