如何访问numpy ndarray的元素? [英] How to access elements of numpy ndarray?
问题描述
我正在使用 scipy 的 loadmat
函数将 matlab 数据文件加载到 python 中.
from scipy.io import loadmatdata = loadmat('data.mat')字段 = 数据['字段']
fields
的类型是 numpy.ndarray
:
print 'fields type={}'.format(type(fields))打印 'fields dtype={}'.format(fields.dtype)打印 'fields shape={}'.format(fields.shape)
<块引用>
fields type=字段数据类型=对象字段形状=(5,)
我使用 nditer
遍历数组:
for x in np.nditer(fields, flags=['refs_ok']):打印 'x={}'.format(x)打印 'x type={}'.format(type(x))打印 'x dtype={}'.format(x.dtype)打印 'x shape={}'.format(x.shape)休息
<块引用>
x=[u'ACE']x type=x 数据类型=对象x 形状=()
索引错误:
如果我尝试访问 x
的第一个元素,我会得到一个 IndexError
:
x[0]
<块引用>
---------------------------------------------------------------------------IndexError 回溯(最近一次调用最后一次)<ipython-input-102-8c374ae22096>在 <module>()17 打印 'type={}'.format(type(x))18 打印 'dtype={}'.format(x.dtype)--->19 x[0]20 休息21IndexError:数组的索引太多
问题:
- 怎么会,如果
type(x)
返回nump.ndarray
它说数组的索引太多"? - 如何将
x
的内容提取为字符串?
以下是我使用的版本:
print 'python 版本:{}'.format(sys.version)打印 'numpy 版本:{}'.format(numpy.__version__)打印 'scipy 版本:{}'.format(scipy.__version__)
<块引用>
python 版本:2.7.6(默认,2015 年 6 月 22 日,17:58:13)[海湾合作委员会 4.8.2]numpy 版本:1.11.0scipy 版本:0.17.1
无需详细查看您的错误,我就可以指出一些陷阱.
.mat 将包含 MATLAB 矩阵(始终为 2d 或更高)、单元格和结构.
loadmat
以各种方式呈现这些.有些词典必须按名称索引.有对象数组(dtype=object).还有 nd 个数字或字符串数组.您可能需要通过多个级别才能获得数值数组.
检查数组的形状"(大小)及其dtype".如果形状是 ()
和 dtype
对象,则使用 y=x[()]
提取它.
以下是此类 0d 对象数组的示例:
在[4]中:y=np.arange(3)在 [5]: x=np.empty((), dtype=object)在 [6] 中:x[()]=y在 [7] 中:xOut[7]: 数组(数组([0, 1, 2]), dtype=object)在 [8]: x.shape出[8]:()在 [9] 中:x.dtype输出[9]:dtype('O')在 [10] 中:x[0]...IndexError:数组的索引太多在 [11] 中:x[()]出[11]:数组([0, 1, 2])
x
是一个 0d 数组 (x.ndim),所以它必须用一个 0 元素元组索引,()
.对于一个看起来很奇怪的 MATLAB 程序员.
在numpy
(一般Python)中,x[a,b,c]
与x[(a,b,c)] 相同
和 ind=(a,b,c);x[ind]
.换句话说,[]
中的参数被理解为一个值元组.(1,2)
是一个 2 元素元组, (1,)
是一个元素( (1)
只是一个分组),并且()
是一个 0 元素元组.所以 x[()]
只是常规 nd
索引符号的扩展.这不是特例.
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']
The type of fields
is 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
:
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:
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
Questions:
- How come, if
type(x)
returnsnump.ndarray
it says "too many indices for array"? - How can I extract the contents of
x
into a string?
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.
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.
Check the 'shape' (size) of an array and its 'dtype'. If shape is ()
and dtype
object, then extract it with y=x[()]
.
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
is a 0d array (x.ndim), so it must be indexed with a 0 element tuple, ()
. For a MATLAB programmer that can seem odd.
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的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!