为什么我无法获得此numpy数组的形状? [英] Why can't I get the shape of this numpy array?

查看:80
本文介绍了为什么我无法获得此numpy数组的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scipy.io 导入一个matlab文件,并尝试查找其尺寸.看来,即使文件已加载到python中,也无法给出尺寸.这是为什么?以及如何解决这个问题?

I am importing a matlab file using scipy.io, and trying to find its dimensions. It seems, even though the file is getting loaded into python, it's not able to give the dimensions. Why is that? And how to fix this?

>>> import scipy.io
>>> pref_mat = scipy.io.loadmat('pref_mat_loc.mat')
>>> R=pref_mat
>>> import numpy
>>> R=numpy.array(R)
>>> len(R)
0
>>> R # We see that the first line of the file is getting printed, means the file has been loaded.

array({'pref_mat': 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=uint16), '__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon May 08 23:42:05 2017', '__version__': '1.0', '__globals__': []}, dtype=object)

>>> len(R.shape) # But it appears here as though R is empty
0
>>> R.shape # As does here
()

loadmat 返回的

推荐答案

pref_mat 是一本字典.您已将其包装在一个数组中,该数组为 R .我可以将 pref_mat 的内容推导出为 R 的{}部分:

pref_mat, returned by loadmat is a dictionary. You have wrapped it in an array, R. I can deduce the contents of pref_mat as the {} part of R:

{'pref_mat': 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=uint16), 
'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon May 08 23:42:05 2017', 
'__version__': '1.0', 
'__globals__': []}

所以您感兴趣的阵列是

R = pref_mat['pref_mat']

R 应该具有所需的形状和dtype.虽然在摘要视图中我只能看到0.

That R should have the shape and dtype that you want. Though in summary view I only see 0's.

如果MATLAB已保存单元格或结构,则对象"类型数组的嵌套会变得更加复杂.

If the MATLAB had saved cells or structs the nesting of 'object' type arrays would get more complicated.

这篇关于为什么我无法获得此numpy数组的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆