pythonpandasnumpyh5pymat-file

How to load all variables from a .mat file(saved as -v7.3) in python?


I have a .mat file saved as -v7.3 in matlab and contains 1000 variables named x1 to x1000. My objective is to transfer(or load) all these variables into something like X_train which will be used as an input data for an encoder-decoder based neural network. I can access a single variable using the following code:

import h5py
with h5py.File('input.mat', 'r') as f:
    x1 = list(f['x1'])

But I want to access all 1000 variables and store them somewhere to feed them in my network (Any other way beside storing them and feeding them into network is also appreciated). All variables are of size 12x24x12. I also thought of looping through all the variables and store them in a pandas dataframe(I don't know exactly how that works) by using below code in addition to above code:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array(x1))

but I got an error 'ValueError: Must pass 2-d input' and dropped the looping through idea.

I looked online but could only find the answer to get the list of variables but not the variables themselves and got no solution to my problem. So in short i need a way to store or feed all 1000 3-d variables as input for an encoder-decoder based neural network.


Solution

  • alist = [f[k][:] for k in f.keys()]
    

    should load all arrays into a list.

    np.stack(alist)
    

    should create a 4d array