Python method of reading an mat file and saving it in pickle format

  • 2021-01-03 20:58:22
  • OfStack

I am working on Theano these days. I want to convert mat file to pickle format and load Python.

Matlab treats a 1-dimensional array as a matrix of n*1, but there are differences between vector and matrix in Numpy, and Theano also makes a distinction between the two.

I'll just post the code, there's nothing to say = =


from scipy.io import loadmat
import numpy, cPickle
 
data_dict=loadmat(r'E:\dataset\CIFAR10\CIFAR10_small.mat') #need an r!
 
my_array=numpy.array([1,1])
for key in data_dict.keys():
 if type(data_dict[key]) == type(my_array):
  #print matrix information
  print key, type(data_dict[key]),
  print data_dict[key].shape
 
#shape(n,1) (matrix in theano) -> shape(n,) (vector in theano)
print data_dict['Ytr'].shape
Ytr=numpy.hstack(data_dict['Ytr'])
Yte=numpy.hstack(data_dict['Yte'])
Yte=numpy.hstack(data_dict['Yte'])
print Ytr.shape
 
train_set=(data_dict['Xtr'],Ytr)
valid_set =(data_dict['Xte'],Yte)
test_set =(data_dict['Xte'],Yte)
 
output = open('cifar10_small_v.pkl', 'wb')
 
cPickle.dump(train_set, output)
cPickle.dump(valid_set, output)
cPickle.dump(test_set, output)
 
output.close()
print 'save is done'
 
pkl_file = open('cifar10_small_v.pkl', 'rb')
 
data1 = cPickle.load(pkl_file) # is train_set
data2 = cPickle.load(pkl_file) # is valid_set
data3 = cPickle.load(pkl_file) # is test_set
 
print type(data1[1]),data1[1].shape
 
pkl_file.close()

Related articles: