资讯专栏INFORMATION COLUMN

python中无法正确读取.mat文件的解决办法

eechen / 2748人阅读

摘要:在中导入本地数据文件时,总是无法得到正确的数据。此时,如果直接输出,会看到以下结果可见,如果本地文件包含了额外的信息,则单纯使用无法直接读取到所需数据,还应该补充一行对应的代码。

在python中导入本地.mat数据文件时,总是无法得到正确的数据。

问题代码如下:

from numpy import *
import scipy.io

mnist_train = "D:Machine LearningTensorFlowSoftmax Regressionmnist_datasetmnist_train.mat"
mnist_train_labels = "D:Machine LearningTensorFlowSoftmax Regressionmnist_datasetmnist_train_labels.mat"

x = scipy.io.loadmat(mnist_train)
label = scipy.io.loadmat(mnist_train_labels)

print(x.shape)
上段代码输出的结果是(1,1),而对应的数据应是(60000,784)。此时,如果直接输出x,会看到以下结果:
"""
[[ {"__version__": "1.0", "__header__": b"MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Tue Nov 29 12:43:31 2011", 
"mnist_train": array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ...,
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32), "__globals__": []}]]
"""
可见,如果本地mat文件包含了额外的信息,则单纯使用scipy.io.loadmat()无法直接读取到所需数据,还应该补充一行对应的代码。
x = scipy.io.loadmat(mnist_train)
train_x = x["mnist_train"]
label = scipy.io.loadmat(mnist_train_labels)
train_label = label["mnist_train_labels"]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38508.html

相关文章

  • pythonmat矩阵和matlabnpy矩阵实现互相转换

      小编写这篇文章的目的,主要是给大家介绍关于python和npy矩阵的相关介绍,下面会给大家做出一个详细的解答,希望可以给各位读者带来帮助。  mat矩阵和npy矩阵互相转换  numpy.narray矩阵保存为mat文件  importnumpyasnp   importscipy.ioasio   mat_path='your_mat_save_path'   mat=np....

    89542767 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<