这两天在搞Theano,要把mat文件转成pickle格式载入Python。
Matlab是把一维数组当做n*1的矩阵的,但Numpy里还是有vector和matrix的区别,Theano也是对二者做了区分。
直接把代码贴出来吧,好像也没什么可讲的 = =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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格式的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/orangehdc/article/details/39758779