2

I'm trying to follow a tutorial on sound classification in neural networks, and I've found 3 different versions of the same tutorial, all of which work, but they all reach a snag at this point in the code, where I get the "AttributeError: 'Series' object has no attribute 'label'" issue. I'm not particularly au fait with either NNs or Python, so apologies if this is something trivial like a deprecation error, but I can't seem to figure it out myself.

def parser(row):
   # function to load files and extract features
   file_name = os.path.join(os.path.abspath(data_dir), 'Train/train', str(row.ID) + '.wav')

   # handle exception to check if there isn't a file which is corrupted
   try:
      # here kaiser_fast is a technique used for faster extraction
      X, sample_rate = librosa.load(file_name, res_type='kaiser_fast') 
      # we extract mfcc feature from data
      mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0) 
   except Exception as e:
      print("Error encountered while parsing file: ", file)
      return None, None
 
   feature = mfccs
   label = row.Class
 
   return [feature, label]

temp = train.apply(parser, axis=1)
temp.columns = ['feature', 'label']

from sklearn.preprocessing import LabelEncoder

X = np.array(temp.feature.tolist())
y = np.array(temp.label.tolist())

lb = LabelEncoder()

y = np_utils.to_categorical(lb.fit_transform(y))

As mentioned, I've seen three different tutorials on the same subject, all of which end with the same "temp = train.apply(parser, axis=1) temp.columns = ['feature', 'label']" fragment, so I'm assuming this is assigning correctly, but I don't know where it's going wrong otherwise. Help appreciated!

Edit: Traceback as requested, turns out I'd added the wrong traceback. Also I've since found out that this is a case of converting the series object to a dataframe, so any help with that would be great.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-1613f53e2d98> in <module>()
  1 from sklearn.preprocessing import LabelEncoder
  2 
----> 3 X = np.array(temp.feature.tolist())
  4 y = np.array(temp.label.tolist())
  5 

/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   4370             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4371                 return self[name]
-> 4372             return object.__getattribute__(self, name)
   4373 
   4374     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'feature'
ZeLobster
  • 33
  • 1
  • 6

1 Answers1

3

Your current implementation of parser(row) method returns a list for each row of data from train DataFrame. But this is then collected as a pandas.Series object.

So your temp is actually a Series object. Then the following line dont have any effect:

temp.columns = ['feature', 'label']

Since temp is a Series, it does not have any columns, and hence temp.feature and temp.label dont exist and hence the error.

Change your parser() method as following:

def parser(row):
    ...
    ...
    ...

    # Return pandas.Series instead of List
    return pd.Series([feature, label])

By doing this, the apply method from temp = train.apply(parser, axis=1) will return a DataFrame, so your other code will work.

I cannot say about the tutorials you are following. Maybe they followed an older version of pandas which allowed a list to be automatically converted to DataFrame.

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • Thank you for your help! This seems to have solved the problem, now I just have to fix the new problem I'm facing further down the code. Ah, programming... :) – ZeLobster Aug 05 '18 at 19:22
  • I've already voted for your answer, but can't upvote it due to my reputation level. This is my first post on SO so sorry for not accepting sooner, I didn't know. :) – ZeLobster Aug 07 '18 at 09:02