Python 保存数组至.mat文件,报错:AttributeError: 'numpy.ndarray' object has no attribute 'items'

来源:互联网 发布:淘宝助理取消二次验证 编辑:程序博客网 时间:2024/06/05 06:08

Python保存数组的方式有很多种,如savetxt,但这个函数只能保存一维或二维数组。为了保存高维数组,同时能够在matlab载入,可以使用scipy.io.savemat()函数,例子:

import numpy as npimport scipy.io as sioA = np.array([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])print Aprint A.shapesio.savemat('data.mat', {"foo":A})

得到输出:
这里写图片描述
在matlab中导入该数组:

>>load('data.mat')

这里写图片描述
可以看到,matlab与Python对矩阵的保存上有一定的变换,从Python到matlab为:’页’->’行’,’行’->’列’,’列’->’页’,即在Python中的矩阵形状为3×4×5,在matlab中变为5×3×4

另外,在Python中如果保存成.mat文件时代码为:sio.savemat('data.mat', A),则会报错AttributeError: 'numpy.ndarray' object has no attribute 'items'

阅读全文
0 0
原创粉丝点击