2

I want to convert a mat file with size 600 by 600 to numpy array and I got this error "float() argument must be a string or a number, not 'dict'" I am wondering how can I fix it.

    import numpy as np
    import scipy.io as sio
    test = sio.loadmat('Y7.mat')
    data=np.zeros((600,600))
    data[:,:]=test
3
  • Read the loadmat docs more carefully. It produces a dictionary containing arrays. It is not an array itself. Commented Jun 9, 2020 at 3:57
  • Then, how can I convert mat file to numpy array? I tried all the solutions in stack overflow and none of them worked for me. Commented Jun 9, 2020 at 4:00
  • You don't convert it. You find desired array in the loaded dictionary. Did you read the docs?? docs.scipy.org/doc/scipy/reference/generated/… Commented Jun 9, 2020 at 4:09

1 Answer 1

1
In [240]: from scipy.io import loadmat                                                        

Using a test mat file that I have from past SO questions:

In [241]: loadmat('test.mat')                                                                 
Out[241]: 
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sat Mar 21 20:09:30 2020',
 '__version__': '1.0',
 '__globals__': [],
 'x': array([[ 0,  3,  6,  9],
        [ 1,  4,  7, 10],
        [ 2,  5,  8, 11]])}

loadmat has given me a dictionary (see the {}?). This happens to have one array, with key 'x'. So I just access it with standard dictionary indexing:

In [242]: _['x']                                                                              
Out[242]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

x was a variable in the MATLAB session that saved this file.

I don't know anything about what's on your file. print(list(data.keys())) can be used to see those keys/variable names.

I tried to get you to look at the loadmat docs, and see that it:

Returns
    mat_dictdict
        dictionary with variable names as keys, and loaded matrices as values.
Sign up to request clarification or add additional context in comments.

7 Comments

The key and variable of my file ['header', 'version', 'globals', 'Ar'] are Array of float32 , list, bytes, str. Since the mat file version is 7.3 Maltab files I need a HDF5 python library to read MATLAB 7.3 format mat files. I used the solution at this link htps://stackoverflow.com/questions/874461/read-mat-files-in-python but it raised with this error : Unable to open file (file signature not found)
did you try data['Ar']?
If loadmat did not raise an error, and gave you a dict, then this is a valid pre-HDF5 file. You just have to use the right key to access the desired array.
The loadmat did nor raised with an error. How can I use the right key in below code?f = h5py.File('Y7.mat','r') data = f.get('data/variable1') data = np.array(data)
I used the hdf5storage.write command to save it as h5 file and then I compress it to npz file with command np.savez_compressed. The size reduced from 400 kb to 1 kb! I am not sure whether all the data are existed in the npz file or not. The reason I am doing all this stuff is that my mat file size is about 600 kb and I want to save it as npz to reduce the data size to feed 100000 of those to my CNN code.
|

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.