资讯专栏INFORMATION COLUMN

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

eechen / 3141人阅读

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

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

问题代码如下:

</>复制代码

  1. from numpy import *
  2. import scipy.io
  3. mnist_train = "D:Machine LearningTensorFlowSoftmax Regressionmnist_datasetmnist_train.mat"
  4. mnist_train_labels = "D:Machine LearningTensorFlowSoftmax Regressionmnist_datasetmnist_train_labels.mat"
  5. x = scipy.io.loadmat(mnist_train)
  6. label = scipy.io.loadmat(mnist_train_labels)
  7. print(x.shape)
上段代码输出的结果是(1,1),而对应的数据应是(60000,784)。此时,如果直接输出x,会看到以下结果:

</>复制代码

  1. """
  2. [[ {"__version__": "1.0", "__header__": b"MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Tue Nov 29 12:43:31 2011",
  3. "mnist_train": array([[ 0., 0., 0., ..., 0., 0., 0.],
  4. [ 0., 0., 0., ..., 0., 0., 0.],
  5. [ 0., 0., 0., ..., 0., 0., 0.],
  6. ...,
  7. [ 0., 0., 0., ..., 0., 0., 0.],
  8. [ 0., 0., 0., ..., 0., 0., 0.],
  9. [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32), "__globals__": []}]]
  10. """
可见,如果本地mat文件包含了额外的信息,则单纯使用scipy.io.loadmat()无法直接读取到所需数据,还应该补充一行对应的代码。

</>复制代码

  1. x = scipy.io.loadmat(mnist_train)
  2. train_x = x["mnist_train"]
  3. label = scipy.io.loadmat(mnist_train_labels)
  4. 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元查看
<