无法在将新附加的字符串列保存到numpy数组上时执行np.savetxt [英] unable to do np.savetxt on save newly appended string column to numpy array

查看:239
本文介绍了无法在将新附加的字符串列保存到numpy数组上时执行np.savetxt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的numpy数组mfcc具有mfcc值,并且形状为(5911,20).
我有一个列表a =[],其中有5911个标签,例如apple cow dog.
我想将这些标签附加到mfcc numpy数组.

I have numpy array mfcc having mfcc values , and is of shape (5911,20).
I have one list a =[] which has 5911 labels like apple cow dog.
I want to append the these labels to the mfcc numpy array.

STEP1 我将带有标签的列表转换为数组:

STEP1 I converted list with labels to an array :

at = np.array(a)
print (at)
print at.shape
print type(at)

['apple' 'apple' 'apple' ..., 'cow' 'cow' 'cow']
(5912,)
<type 'numpy.ndarray'>

STEP2 ,我确保atmfcc的尺寸相同:

STEP2 I made sure both at and mfcc were of same dimensions:

if len(at) > len(mfcc):
    at= at[ :-1]

STEP3 然后,我将它们堆叠在一起.

STEP3 Then I stacked them together.

mfcc_with_labels=np.hstack((mfcc_with_labels,at[:,None]))
print mfcc_with_labels.shape 

(5911,21)

问题步骤,现在我想将此mfcc_with_labels保存到文件中.这样我以后就可以将其馈送到神经网络了.

PROBLEM STEP Now I want to save this mfcc_with_labels to a file. So that I can feed it to a neural network later.

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'指定为选项,但是什么也没有发生.

I tried specifying 'fmt = %s' as an option but nothing happens.

我检查了 mfcc_with_labels[1]和堆叠/追加确实起作用,

I inspected and mfcc_with_labels[1] and the stacking/appending did work,

['-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''apple']

['-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' 'apple']

无法理解为什么不保存它.

Unable to comprehend why it is not being saved.

我已经看过了: numpy初学者:使用numpy编写数组.savetxt 使用文件处理程序时numpy savetxt失败 如何组合numpy数组和一个文本列,并导出到csv

请指导我如何正确保存此新的numpy数组. 我来自R编程背景,在python中,像将R数据帧这样的结构保存起来一样,是否有任何简单的python等同于保存该数组?

Please guide me how to save this new numpy array properly. I'm from an R programming background, in python is there any easy of python equivalent of saving this array like an R data frame kind of structure?

最终目标是将其发送到神经网络.

Final goal is to send this into a neural network.

推荐答案

savetxt的默认fmt%.18e.尝试使用数字

The default fmt for savetxt is %.18e. Try that with a number

In [84]: '%.18e'%12
Out[84]: '1.200000000000000000e+01'

实际格式是该字符串被复制21次(列数)并与定界符连接.

The actual format is that string replicated 21 times (the number of columns) and joined with the delimiter.

但是您的数组有一个字符串dtype,并包含字符串(因为您附加了标签.不适用于该格式.

But your array has a string dtype, and contains strings (because you appended the labels. That doesn't work with that format.

您的mfcc_with_labels[1]

In [86]:     row = np.array(['-5.04415946628e-14', '1.9026074286e-14', '3.425843
    ...: 34296e-14', 'apple'])
In [87]: row
Out[87]: 
array(['-5.04415946628e-14', '1.9026074286e-14', '3.42584334296e-14',
       'apple'], dtype='<U18')

'%s'fmt应该起作用;这种格式可以做到:

'%s' fmt should work; this formatting does:

In [88]: '%s,%s,%s,%s'%tuple(row)
Out[88]: '-5.04415946628e-14,1.9026074286e-14,3.42584334296e-14,apple'

这篇关于无法在将新附加的字符串列保存到numpy数组上时执行np.savetxt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆