0

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 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 I made sure both at and mfcc were of same dimensions:

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

STEP3 Then I stacked them together.

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

(5911,21)

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")

and it throws a huge 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')

**

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

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

Unable to comprehend why it is not being saved.

I already looked at : 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

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.

3
  • I tried specifying 'fmt = %s' as an option but nothing happens. That's awfully vague. No error message? No file output? Are you sure you are looking in the right place for that file? Commented Feb 9, 2018 at 4:15
  • I too found that weird , there is no error no warning absolutely nothing. I even tried ` locate mfcc_with_labels` ` from terminal . nothing showed up. Let me restart my laptop and try again/. Commented Feb 9, 2018 at 4:18
  • ok fmt ='%s' worked. Commented Feb 9, 2018 at 4:27

1 Answer 1

2

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

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

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

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

Your 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 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'
Sign up to request clarification or add additional context in comments.

1 Comment

I see the point you are trying to make. the fmt =%s worked. For some reason ubuntu was not able to locate the files. a restart solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.