【发布时间】:2018-02-09 03:10:31
【问题描述】:
我有 numpy 数组 mfcc 具有 mfcc 值,并且形状为 (5911,20)。
我有一个列表a =[],其中有 5911 个标签,例如applecowdog。
我想将这些标签附加到 mfcc numpy 数组中。
第 1 步 我将带有标签的列表转换为数组:
at = np.array(a)
print (at)
print at.shape
print type(at)
['apple' 'apple' 'apple' ..., 'cow' 'cow' 'cow']
(5912,)
<type 'numpy.ndarray'>
STEP2 我确保at 和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")
它会抛出一个巨大的错误 **
---------------------------------------------------------------------------
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 beginner: writing an array using numpy.savetxt numpy savetxt fails when using file handler How to combine a numpy array and a text column and export to csv
请指导我如何正确保存这个新的 numpy 数组。 我来自 R 编程背景,在 python 中是否有任何简单的 python 相当于将这个数组保存为 R 数据框类型的结构?
最终目标是将其发送到神经网络。
【问题讨论】:
-
I tried specifying 'fmt = %s' as an option but nothing happens.这非常模糊。没有错误信息?没有文件输出?您确定您正在寻找该文件的正确位置吗? -
我也觉得很奇怪,没有错误没有警告绝对没有。我什至尝试从终端`locate mfcc_with_labels`。什么都没有出现。让我重新启动我的笔记本电脑,然后再试一次/。
-
ok fmt ='%s' 工作。