numpy中的矩阵索引 [英] Matrix indexing in Numpy

查看:560
本文介绍了numpy中的矩阵索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在涉及矩阵操作的小型Python脚本的开发过程中,我感到困惑,因此我启动了一个外壳来玩一个玩具示例,并加深了对Numpy中矩阵索引的理解.

I was growing confused during the development of a small Python script involving matrix operations, so I fired up a shell to play around with a toy example and develop a better understanding of matrix indexing in Numpy.

这就是我所做的:

>>> import numpy as np
>>> A = np.matrix([1,2,3])
>>> A
matrix([[1, 2, 3]])
>>> A[0]
matrix([[1, 2, 3]])
>>> A[0][0]
matrix([[1, 2, 3]])
>>> A[0][0][0]
matrix([[1, 2, 3]])
>>> A[0][0][0][0]
matrix([[1, 2, 3]])

您可以想象,这不是帮助我更好地理解了Numpy中的矩阵索引.对于我将其描述为自身数组"的事物,这种行为是有意义的,但是我怀疑在他们的头脑中是否有人会选择它作为科学图书馆中矩阵的模型.

As you can imagine, this has not helped me develop a better understanding of matrix indexing in Numpy. This behavior would make sense for something that I would describe as "An array of itself", but I doubt anyone in their right mind would choose that as a model for matrices in a scientific library.

那么,我获得的输出的逻辑是什么?为什么矩阵对象的第一个元素是自身?

What is, then, the logic to the output I obtained? Why would the first element of a matrix object be itself?

PS:我知道如何获取矩阵的第一项.我感兴趣的是此设计决策背后的逻辑.

PS: I know how to obtain the first entry of the matrix. What I am interested in is the logic behind this design decision.

我不是在问如何访问矩阵元素,或者为什么矩阵行的行为类似于矩阵.我要定义一个矩阵的行为,当用单个数字索引时.这是数组的典型操作,但是所产生的行为与您期望从数组中获得的行为完全不同.我想知道这是如何实现的,以及设计决策背后的逻辑是什么.

I'm not asking how to access a matrix element, or why a matrix row behaves like a matrix. I'm asking for a definition of the behavior of a matrix when indexed with a single number. It's an action typical of arrays, but the resulting behavior is nothing like the one you would expect from an array. I would like to know how this is implemented and what's the logic behind the design decision.

推荐答案

索引后查看形状:

In [295]: A=np.matrix([1,2,3])
In [296]: A.shape
Out[296]: (1, 3)
In [297]: A[0]
Out[297]: matrix([[1, 2, 3]])
In [298]: A[0].shape
Out[298]: (1, 3)

此行为的关键是np.matrix始终为2d.因此,即使选择一行(A[0,:]),结果仍然是2d,形状为(1,3).因此,您可以随心所欲地串[0]条,而且不会发生新的情况.

The key to this behavior is that np.matrix is always 2d. So even if you select one row (A[0,:]), the result is still 2d, shape (1,3). So you can string along as many [0] as you like, and nothing new happens.

您要使用A[0][0]完成什么?与A[0,0]一样吗? 对于基np.ndarray类,它们是等效的.

What are you trying to accomplish with A[0][0]? The same as A[0,0]? For the base np.ndarray class these are equivalent.

请注意,Python解释器会将索引转换为__getitem__调用.

Note that Python interpreter translates indexing to __getitem__ calls.

 A.__getitem__(0).__getitem__(0)
 A.__getitem__((0,0))

[0][0]是2个索引操作,而不是一个.因此,第二个[0]的效果取决于第一个[0]的产生.

[0][0] is 2 indexing operations, not one. So the effect of the second [0] depends on what the first produces.

对于数组A[0,0]等效于A[0,:][0].但是对于矩阵,您需要执行以下操作:

For an array A[0,0] is equivalent to A[0,:][0]. But for a matrix, you need to do:

In [299]: A[0,:][:,0]
Out[299]: matrix([[1]])  # still 2d

============================

=============================

一个数组本身",但是我怀疑在他们的头脑中是否有人会选择它作为科学图书馆中矩阵的模型.

"An array of itself", but I doubt anyone in their right mind would choose that as a model for matrices in a scientific library.

那么,我获得的输出的逻辑是什么?为什么矩阵对象的第一个元素是自身?

What is, then, the logic to the output I obtained? Why would the first element of a matrix object be itself?

此外,A [0 ,:]与A [0]不同

In addition, A[0,:] is not the same as A[0]

鉴于这些评论,让我提出一些说明.

In light of these comments let me suggest some clarifications.

A[0]并不意味着返回第一个元素".表示沿第一轴进行选择.对于一维数组,表示第一项.对于二维数组,它表示第一行.对于ndarray,它将是一维数组,但对于matrix,则是另一个matrix.因此,对于二维数组或矩阵,A[i,:]A[i]是同一件事.

A[0] does not mean 'return the 1st element'. It means select along the 1st axis. For a 1d array that means the 1st item. For a 2d array it means the 1st row. For ndarray that would be a 1d array, but for a matrix it is another matrix. So for a 2d array or matrix, A[i,:] is the same thing as A[i].

A[0]不仅返回自身.它返回一个新矩阵.不同的id:

A[0] does not just return itself. It returns a new matrix. Different id:

In [303]: id(A)
Out[303]: 2994367932
In [304]: id(A[0])
Out[304]: 2994532108

它可能具有相同的数据,形状和步幅,但这是一个新对象.它与多行矩阵的ith行一样独特.

It may have the same data, shape and strides, but it's a new object. It's just as unique as the ith row of a many row matrix.

大多数独特的matrix活动在numpy/matrixlib/defmatrix.py中定义.我本来建议您查看matrix.__getitem__方法,但是大多数操作是在np.ndarray.__getitem__中执行的.

Most of the unique matrix activity is defined in: numpy/matrixlib/defmatrix.py. I was going to suggest looking at the matrix.__getitem__ method, but most of the action is performed in np.ndarray.__getitem__.

np.matrix类已添加到numpy中,以方便老式的MATLAB程序员. numpy数组几乎可以具有任意数量的维,即0、1,.. MATLAB仅允许使用2个,尽管大约2000年的发行版将其推广到2个或更多.

np.matrix class was added to numpy as a convenience for old-school MATLAB programmers. numpy arrays can have almost any number of dimensions, 0, 1, .... MATLAB allowed only 2, though a release around 2000 generalized it to 2 or more.

这篇关于numpy中的矩阵索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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