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

发布时间:2019-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了python中无法正确读取.mat文件的解决办法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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']

脚本宝典总结

以上是脚本宝典为你收集整理的python中无法正确读取.mat文件的解决办法全部内容,希望文章能够帮你解决python中无法正确读取.mat文件的解决办法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。