简体   繁体   English

如何访问numpy ndarray的元素?

[英]How to access elements of numpy ndarray?

I'm using scipy's loadmat function to load a matlab data file into python. 我正在使用scipy的loadmat函数将matlab数据文件加载到python中。

from scipy.io import loadmat  

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

The type of fields is numpy.ndarray : 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,) 

I iterate over the array using nditer : 我使用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: IndexError:

If I try to access the first element of x I get an IndexError : 如果我尝试访问x的第一个元素,我会得到一个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 

Questions: 问题:

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

Here are the versions I'm using: 以下是我正在使用的版本:

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. 没有详细查看您的错误,我可以指出一些陷阱。

The .mat will contain MATLAB matrices (always 2d or higher), cells and structures. .mat将包含MATLAB矩阵(总是2d或更高),单元格和结构。

loadmat renders those in various ways. loadmat以各种方式呈现它们。 There are dictionaries that you have to index by name. 您必须按名称索引字典。 There are object arrays (dtype=object). 有对象数组(dtype = object)。 And there are nd numeric or string arrays. 并且有nd数字或字符串数​​组。 You may have to work through several levels to get at the numeric array. 您可能需要通过几个级别来获取数字数组。

Check the 'shape' (size) of an array and its 'dtype'. 检查数组的“形状”(大小)及其“dtype”。 If shape is () and dtype object, then extract it with y=x[()] . 如果shape是()dtype对象,则用y=x[()]提取它。

Here's an example of such a 0d object array: 这是一个这样的0d对象数组的例子:

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 is a 0d array (x.ndim), so it must be indexed with a 0 element tuple, () . x是一个0d数组(x.ndim),因此必须使用0元素元组()进行索引。 For a MATLAB programmer that can seem odd. 对于一个看似奇怪的MATLAB程序员。

In numpy (Python in general), x[a,b,c] is the same as x[(a,b,c)] and ind=(a,b,c); x[ind] numpy (一般是Python)中, x[a,b,c]x[(a,b,c)]ind=(a,b,c); x[ind] ind=(a,b,c); x[ind] . 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. (1,2)是2元素元组, (1,)是一个元素( (1)只是一个分组), ()是一个0元素元组。 So x[()] is just an extension of the regular nd indexing notation. 所以x[()]只是常规nd索引表示法的扩展。 It isn't a special case. 这不是一个特例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM