亲宝软件园·资讯

展开

Python读取mat文件,并保存为pickle格式 Python读取mat文件,并保存为pickle格式的方法

hdc 人气:2

这两天在搞Theano,要把mat文件转成pickle格式载入Python。

Matlab是把一维数组当做n*1的矩阵的,但Numpy里还是有vector和matrix的区别,Theano也是对二者做了区分。

直接把代码贴出来吧,好像也没什么可讲的 = =

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()

以上这篇Python读取mat文件,并保存为pickle格式的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论