当前位置:Gxlcms > Python > 详解python中无法正确读取.mat文件的解决办法

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

时间:2021-07-01 10:21:17 帮助过:125人阅读

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

问题代码如下:

  1. from numpy import *import scipy.io
  2. mnist_train = 'D:\Machine Learning\TensorFlow\Softmax Regression\mnist_dataset\mnist_train.mat'mnist_train_labels = 'D:\Machine Learning\TensorFlow\Softmax Regression\mnist_dataset\mnist_train_labels.mat'x = scipy.io.loadmat(mnist_train)
  3. label = scipy.io.loadmat(mnist_train_labels)
  4. 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']

以上就是详解python中无法正确读取.mat文件的解决办法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行