圆月山庄资源网 Design By www.vgjia.com

这两天在搞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格式的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
Python,mat,pickle

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com