I have a basic doubt in numpy. I am using Python 2.7, numpy-1.9.2 on Ubuntu 14.04.
我对numpy有一个基本的怀疑。我在Ubuntu 14.04上使用Python 2.7,numpy-1.9.2。
For example, I initialize a 2d numpy array as a = np.zeros((10,10))
.
例如,我将一个2d numpy数组初始化为a = np.zeros((10,10))。
I then try to index a portion of it using the range function as the indices by the following way:
然后我尝试使用范围函数作为索引来索引其中的一部分,方法如下:
a[range(0,5),range(0,5)]
. I get an array of shape (5,). What I want is the first 5 rows and columns of the 2d array a
.
一个[范围(0,5),范围(0,5)]。我得到一个形状数组(5,)。我想要的是2d数组的前5行和列a。
When I perform a[:5,:5]
, it seems to give me an array of shape (5,5).
当我执行[:5,:5]时,它似乎给了我一个形状的数组(5,5)。
Can someone explain to me why using the range function for specifying the index is failing? I am still confused about numpy indexing even after working with it for almost an year.
有人可以向我解释为什么使用范围函数来指定索引失败?即使在使用它近一年之后,我仍然对numpy索引感到困惑。
Thanks for your help in advance.
感谢您的帮助。
1 个解决方案
#1
With range
you are using integer array indexing
as described here:
对于范围,您使用整数数组索引,如下所述:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing
To get the equivalent of a[0:5,0:5]
, you have to take advantage of 'broadcasting'. Here the 1st index is a column vector
为了得到[0:5,0:5]的等价物,你必须利用'广播'。这里第一个索引是列向量
a[np.arange(0,5)[:,None],range(0,5)]
In [137]: np.arange(0,5)[:,None]
Out[137]:
array([[0],
[1],
[2],
[3],
[4]])
I could go into more detail, but you could just as well read that doc.
我可以详细介绍一下,但您也可以阅读该文档。
np.ix_
is a utility that helps generate this sort of indexing arrays:
np.ix_是一个帮助生成这种索引数组的实用程序:
In [507]: np.ix_(range(0,5),range(0,5))
Out[507]:
(array([[0],
[1],
[2],
[3],
[4]]), array([[0, 1, 2, 3, 4]]))
This (5,1) array broadcasts against a (1,5) array to produce a (5,5) indexing array.
这个(5,1)数组广播(1,5)数组以产生(5,5)索引数组。
MATLAB and numpy have choose alternative advanced indexing approaches:
MATLAB和numpy选择了替代的高级索引方法:
In MATLAB/Octave, a([1,2,3],[1,2,3])
indexes a (3,3) block. In numpy
, a[[1,2,3],[1,2,3]]
indexes the (3,) diagonal.
在MATLAB / Octave中,a([1,2,3],[1,2,3])索引(3,3)块。在numpy中,[[1,2,3],[1,2,3]]索引(3,)对角线。
a(sub2ind(size(a),[1,2,3],[1,2,3]))
is the Octave diagonal; a[np.ix_([1,2,3],[1,2,3])]
is the numpy
block.
a(sub2ind(size(a),[1,2,3],[1,2,3]))是Octave对角线; a [np.ix _([1,2,3],[1,2,3])]是numpy块。
#1
With range
you are using integer array indexing
as described here:
对于范围,您使用整数数组索引,如下所述:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing
To get the equivalent of a[0:5,0:5]
, you have to take advantage of 'broadcasting'. Here the 1st index is a column vector
为了得到[0:5,0:5]的等价物,你必须利用'广播'。这里第一个索引是列向量
a[np.arange(0,5)[:,None],range(0,5)]
In [137]: np.arange(0,5)[:,None]
Out[137]:
array([[0],
[1],
[2],
[3],
[4]])
I could go into more detail, but you could just as well read that doc.
我可以详细介绍一下,但您也可以阅读该文档。
np.ix_
is a utility that helps generate this sort of indexing arrays:
np.ix_是一个帮助生成这种索引数组的实用程序:
In [507]: np.ix_(range(0,5),range(0,5))
Out[507]:
(array([[0],
[1],
[2],
[3],
[4]]), array([[0, 1, 2, 3, 4]]))
This (5,1) array broadcasts against a (1,5) array to produce a (5,5) indexing array.
这个(5,1)数组广播(1,5)数组以产生(5,5)索引数组。
MATLAB and numpy have choose alternative advanced indexing approaches:
MATLAB和numpy选择了替代的高级索引方法:
In MATLAB/Octave, a([1,2,3],[1,2,3])
indexes a (3,3) block. In numpy
, a[[1,2,3],[1,2,3]]
indexes the (3,) diagonal.
在MATLAB / Octave中,a([1,2,3],[1,2,3])索引(3,3)块。在numpy中,[[1,2,3],[1,2,3]]索引(3,)对角线。
a(sub2ind(size(a),[1,2,3],[1,2,3]))
is the Octave diagonal; a[np.ix_([1,2,3],[1,2,3])]
is the numpy
block.
a(sub2ind(size(a),[1,2,3],[1,2,3]))是Octave对角线; a [np.ix _([1,2,3],[1,2,3])]是numpy块。