python && matlab 导入.mat格式数据集并可视化
以Yale_32x32.mat为例
提取密码:xwgt
1.python 导入
import numpy as np
import matplotlib.pyplot as plt
from scipy import io
### load dataset and get x as a dictionary
x=io.loadmat('C:\\Users\yh\Desktop\ML\Yale_32x32.mat')
### get x' key
print(x.keys())
### set up a array 1024(32X32)
a=np.array(1024)
### get feature' first line
a=x['fea'][0]
### reshape a(1X1024) to 32X32
a.shape=32,32
###transpose a
a=a.T
plt.imshow(a,cmap='gray')
plt.axis('off')
plt.show()
显示效果:
2.matlab导入
% x is a struct
x=load('C:\Users\yh\Desktop\ML\Yale_32x32.mat');
f=x.fea;
a=f(1,:);
a=reshape(a,32,32);
%The dataset is a set of gray images. If it's colorful ,you can reshape(use
%(a,32,32,3)) it to RGB images
a=uint8(a);
img=imshow(a);
效果显示: