Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read .mat file in Python. But the shape of the data changed

 % save .mat file in the matlab    
train_set_x=1:50*1*51*61*23;   
train_set_x=reshape(train_set_x,[50,1,51,61,23]);   
save(['pythonTest.mat'],'train_set_x','-v7.3');

The data obtained in the matlab is in the size of (50,1,51,61,23).

I load the .mat file in Python with the instruction of this link.

The code is as follows:

import numpy as np, h5py
f = h5py.File('pythonTest.mat', 'r')
train_set_x = f.get('train_set_x')
train_set_x = np.array(train_set_x)

The output of train_set_x.shape is (23L, 61L, 51L, 1L, 50L). It is expected to be (50L, 1L, 51L, 61L, 23L). So I changed the shape by

train_set_x=np.transpose(train_set_x, (4,3,2,1,0))

I am curious about the change in data shape between Python and matlab. Is there some errors in my code?

like image 577
sha li Avatar asked Sep 01 '16 06:09

sha li


1 Answers

You do not have any errors in the code. There is a fundamental difference between Matlab and python in the way they treat multi-dimensional arrays.
Both Matalb and python store all the elements of the multi-dim array as a single contiguous block in memory. The difference is the order of the elements:
Matlab, (like fortran) stores the elements in a column-first fashion, that is storing the elements according to the dimensions of the array, for 2D:

 [1 3;
  2 4]

In contrast, Python, stores the elements in a row-first fashion, that is starting from the last dimension of the array:

[1 2;
 3 4];

So a block in memory with size [m,n,k] in Matlab is seen by python as an array of shape [k,n,m].

For more information see this wiki page.

BTW, instead of transposing train_set_x, you might try setting its order to "Fortran" order (col-major as in Matlab):

 train_set_x = np.array(train_set_x, order='F')
like image 183
Shai Avatar answered Oct 19 '22 23:10

Shai