本文介绍了如何访问numpy ndarray的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scipy的loadmat函数将matlab数据文件加载到python中.

I'm using scipy's loadmat function to load a matlab data file into python.

from scipy.io import loadmat

data   = loadmat('data.mat')
fields = data['field']

fields的类型是numpy.ndarray:

print 'fields type={}'.format(type(fields))
print 'fields dtype={}'.format(fields.dtype)
print 'fields shape={}'.format(fields.shape)
fields type=<type 'numpy.ndarray'>
fields dtype=object
fields shape=(5,)

我使用nditer遍历数组:

for x in np.nditer(fields, flags=['refs_ok']):
    print 'x={}'.format(x)
    print 'x type={}'.format(type(x))
    print 'x dtype={}'.format(x.dtype)
    print 'x shape={}'.format(x.shape)
    break
x=[u'ACE']
x type=<type 'numpy.ndarray'>
x dtype=object
x shape=()

IndexError:

如果我尝试访问x的第一个元素,则会得到一个IndexError:

If I try to access the first element of x I get an IndexError:

x[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-102-8c374ae22096> in <module>()
     17     print 'type={}'.format(type(x))
     18     print 'dtype={}'.format(x.dtype)
---> 19     x[0]
     20     break
     21

IndexError: too many indices for array

问题:

  • 为什么type(x)返回nump.ndarray却说数组索引太多"?
  • 如何将x的内容提取到字符串中?
  • How come, if type(x) returns nump.ndarray it says "too many indices for array"?
  • How can I extract the contents of x into a string?

以下是我正在使用的版本:

print 'python version: {}'.format(sys.version)
print 'numpy version: {}'.format(numpy.__version__)
print 'scipy version: {}'.format(scipy.__version__)
python version: 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2]
numpy version: 1.11.0
scipy version: 0.17.1

推荐答案

如果不仔细查看您的错误,我会指出一些陷阱.

Without looking at your errors in detail I can point out some pitfalls.

.mat将包含MATLAB矩阵(始终为2d或更高),单元格和结构.

The .mat will contain MATLAB matrices (always 2d or higher), cells and structures.

loadmat以各种方式呈现它们.有些字典必须按名称索引.有对象数组(dtype = object).并且有nd数字或字符串数​​组.您可能需要遍历多个级别才能获得数字数组.

loadmat renders those in various ways. There are dictionaries that you have to index by name. There are object arrays (dtype=object). And there are nd numeric or string arrays. You may have to work through several levels to get at the numeric array.

检查数组的形状"(大小)及其"dtype".如果shape是()dtype对象,则用y=x[()]提取它.

Check the 'shape' (size) of an array and its 'dtype'. If shape is () and dtype object, then extract it with y=x[()].

这是一个0d对象数组的示例:

Here's an example of such a 0d object array:

In [4]: y=np.arange(3)

In [5]: x=np.empty((), dtype=object)
In [6]: x[()]=y

In [7]: x
Out[7]: array(array([0, 1, 2]), dtype=object)

In [8]: x.shape
Out[8]: ()

In [9]: x.dtype
Out[9]: dtype('O')

In [10]: x[0]
...
IndexError: too many indices for array

In [11]: x[()]
Out[11]: array([0, 1, 2])

x是一个0d数组(x.ndim),因此必须使用0元素元组()进行索引.对于看起来似乎很奇怪的MATLAB程序员.

x is a 0d array (x.ndim), so it must be indexed with a 0 element tuple, (). For a MATLAB programmer that can seem odd.

numpy(通常为Python)中,x[a,b,c]x[(a,b,c)]ind=(a,b,c); x[ind]相同.换句话说,[]中的参数应理解为值的元组. (1,2)是2个元素的元组,(1,)是一个元素((1)只是一个分组),()是0个元素的元组.因此,x[()]只是常规nd索引符号的扩展.这不是特例.

In numpy (Python in general), x[a,b,c] is the same as x[(a,b,c)] and ind=(a,b,c); x[ind]. In other words, the arguments in [] are understood to be a tuple of values. (1,2) is a 2 element tuple, (1,) is one element ( (1) is just a grouping), and () is a 0 element tuple. So x[()] is just an extension of the regular nd indexing notation. It isn't a special case.

这篇关于如何访问numpy ndarray的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:58