0

I was trying to follow the example of "https://github.com/EBjerrum/Deep-Chemometrics/blob/master/Deep_Chemometrics_with_data_augmentation.py.ipynb", but when executing the first part of the code I immediately get the error.

#code
import scipy.io as sio
import numpy as np

def get_xY(filename, maxx=600):

#sio.whosmat(filename)

matcontents = sio.loadmat(filename)
keys = matcontents.keys()
for key in list(keys):
    if key[0] == '_':
        keys.remove(key)

keys.sort()

d = {}            
for key in keys:
    data = matcontents[key][0][0]
    if key[-1] == "Y":
        Ydata = data[5]
        d[key] = Ydata
    else:
        xdata = data[5][:,:maxx]
        d[key] = xdata
        d["axisscale"]= data[7][1][0][0][:maxx].astype(np.float)

return d

filename = 'Dataset/nir_shootout_2002.mat'
dataset = get_xY(filename)

' AttributeError: 'dict_keys' object has no attribute 'remove' '

  • 1
    Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Off-site links are rarely acceptable. – Prune Jan 23 '20 at 17:34
  • 1
    [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. – Prune Jan 23 '20 at 17:34
  • 1
    Please include the *entire* error message; this includes the traceback. – Prune Jan 23 '20 at 17:49
  • Prune is right. Adding the entire traceback is best added as text. Jorge has an image with the stacktrace in it, but this makes searching harder. Please copy and paste the text of the entire error in the future. – shanecandoit Jan 23 '20 at 17:52

1 Answers1

1

It seems like changing keys.remove(key) to del keys[key] worked for them. (From a comment)


You are getting this problem when you load the matlab file and the code expecting a dict where it doesn't find a dict.

The specific error is 'dict_keys' object has no attribute 'remove'. That's how I know that python isn't finding a dict.

Your code:

matcontents = sio.loadmat(filename)
keys = matcontents.keys()

Change that to:

matcontents = sio.loadmat(filename)
print('matcontents',type(matcontents),matcontents)
keys = matcontents.keys()
print('keys',type(keys),keys)

To make sure that the data is loaded as you expect.

This page also mentions that newer versions of matlab files (7.3) must be imported differently. Read .mat files in Python

shanecandoit
  • 581
  • 1
  • 3
  • 11
  • I still have the same problem, check the link and poll the entries mentioned there but without success. – Jorge M. Ropero Vega Jan 23 '20 at 18:35
  • What did this line `print('matcontents',type(matcontents),matcontents)` and this line `print('keys',type(keys),keys)` produce? – shanecandoit Jan 23 '20 at 18:37
  • Change `keys.remove(key)` to `keys.pop(key, None)` – shanecandoit Jan 23 '20 at 19:01
  • 'remove' no longer appears, but now it appears is 'pop' http://prntscr.com/qry519 AttributeError: 'dict_keys' object has no attribute 'pop' – Jorge M. Ropero Vega Jan 23 '20 at 19:29
  • Change `keys.pop(key,None)` to `del keys[key]`. From https://stackoverflow.com/questions/11277432/how-to-remove-a-key-from-a-python-dictionary. If you can edit your post and paste the output there. Many people wont open screenshots and parse them for text. Good luck. – shanecandoit Jan 23 '20 at 19:33