【发布时间】:2020-04-26 15:09:48
【问题描述】:
我想使用 Keras 在 Python 上运行一个神经网络示例程序。我的数据是 Matlab .mat 文件的形式。
train_data.mat (size: 32x32x10,000 single)
train_label.mat (size: 1x10,000 single)
test_data.mat (size: 32x32x2,000 single)
test_label.mat (size: 1x2,000 single)
如何加载上面的 .mat 数据以使用 Keras 替换 Python 中的 MNIST 数据集?
from keras.datasets import mnist
(train_data, train_label), (test_data, test_label) = mnist.load_data()
编辑(用于说明目的)
假设我在 .mat 中的 train_data 包含三个数据,大小为 2x2x3,
val(:,:,1) =
1 1
1 1
val(:,:,2) =
2 2
2 2
val(:,:,3) =
3 3
3 3
用 scipy.io.loadmat 加载后变成如下图,大小为 (2L,2L,3L)
>>> A
array([[[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3]]], dtype=uint8)
如何将其重塑为(3L,2L,2L),即(2L,2L)的三个数据?
回答
>>> import scipy.io
>>> A = scipy.io.loadmat('train_data')
>>> B = A.flatten(1) # flatten to vector
>>> C = B.reshape(3,2,2) # reshape
>>> C
array([[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]],
[[3, 3],
[3, 3]]], dtype=uint8)
【问题讨论】: