我有 numpy 数组mfcc
具有 mfcc 值 ,且形状为 (5911,20)。
我有一份清单a =[]
其中有 5911 个标签,例如apple
cow
dog
.
我想将这些标签附加到mfcc
numpy 数组。
STEP1我将带有标签的列表转换为数组:
at = np.array(a)
print (at)
print at.shape
print type(at)
['apple' 'apple' 'apple' ..., 'cow' 'cow' 'cow']
(5912,)
<type 'numpy.ndarray'>
STEP2我确定了两者at
and mfcc
具有相同的尺寸:
if len(at) > len(mfcc):
at= at[ :-1]
STEP3然后我把它们叠在一起。
mfcc_with_labels=np.hstack((mfcc_with_labels,at[:,None]))
print mfcc_with_labels.shape
(5911,21)
问题步骤现在我想保存这个mfcc_with_labels
到一个文件。这样我就可以稍后将其输入神经网络。
np.savetxt("mfcc_with_labels.txt", mfcc, newline= "\n", delimiter="/t")
它引发了巨大的ERROR
**
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-7709c644ca06> in <module>()
1 print mfcc_features_with_times_and_labels.shape
2
----> 3 np.savetxt("mfcc_with_labels.txt", mfcc, newline= "\n", delimiter="/t")
/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.pyc in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1256 raise TypeError("Mismatch between array dtype ('%s') and "
1257 "format specifier ('%s')"
-> 1258 % (str(X.dtype), format))
1259 if len(footer) > 0:
1260 footer = footer.replace('\n', '\n' + comments)
TypeError: Mismatch between array dtype ('|S32') and format specifier ('%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e/t%.18e')
**
我尝试指定“fmt = %s”作为选项,但没有任何反应。
我检查并mfcc_with_labels[1]
并且堆叠/附加确实有效,
['-498.357912575''-3.40930872496e-14''1.55285010312e-14'
'-5.31554105812e-14' '4.81736993039e-15' '-3.17281148841e-14'
'5.24276966145e-15' '-3.58849635039e-14' '3.11248820963e-14'
'-6.31521494552e-15' '1.96551267563e-14' '1.26848188878e-14'
'6.53784651891e-14' '-3.15089835366e-14' '2.84134910594e-14'
'1.03625144071e-13''-5.52444866686e-14''-5.04415946628e-14'
'1.9026074286e-14''3.42584334296e-14''苹果']
无法理解为什么它没有被保存。
我已经看过了:numpy 初学者:使用 numpy.savetxt 编写数组 https://stackoverflow.com/questions/10920318/numpy-beginner-writing-an-array-using-numpy-savetxt
使用文件处理程序时 numpy savetxt 失败 https://stackoverflow.com/questions/47273840/numpy-savetxt-fails-when-using-file-handler
如何组合 numpy 数组和文本列并导出到 csv https://stackoverflow.com/questions/23687990/how-to-combine-a-numpy-array-and-a-text-column-and-export-to-csv
请指导我如何正确保存这个新的 numpy 数组。
我来自 R 编程背景,在 python 中是否有任何简单的 python 相当于将这个数组保存为 R 数据帧类型的结构?
最终目标是将其发送到神经网络中。