简体   繁体   English

[:][:] 和 [:,:] 之间的 NumPy 有什么区别?

[英]What is the difference in NumPy between [:][:] and [:,:]?

I am quite familiar with python programming but I found some strange cases where the following two lines of code provided different results (assuming that the two arrays are 2-dimensional):我对 python 编程非常熟悉,但是我发现了一些奇怪的情况,其中以下两行代码提供了不同的结果(假设两个 arrays 是二维的):

A[:][:] = B[:][:]

and

A[:,:] = B[:,:]

I am wondering if there is any case, explication.我想知道是否有任何情况,解释。

Any hint?有什么提示吗?

Example:例子:

>>> x = numpy.array([[1, 2], [3, 4], [5, 6]])
>>> x
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> x[1][1]
4                 # expected behavior
>>> x[1,1]
4                 # expected behavior
>>> x[:][1]
array([3, 4])     # huh?
>>> x[:,1]
array([2, 4, 6])  # expected behavior

Let's take a step back.让我们退后一步。 Try this:尝试这个:

>>> x = np.arange(6)

>>> x
array([0, 1, 2, 3, 4, 5])

>>> x[:]
array([0, 1, 2, 3, 4, 5])

>>> x[:][:]
array([0, 1, 2, 3, 4, 5])

>>> x[:][:][:][:][:][:][:][:][:][:]
array([0, 1, 2, 3, 4, 5])

It looks like x[:] is equal to x .看起来x[:]等于x (Indeed, x[:] creates a copy of x .) (实际上, x[:]创建了x的副本。)

Therefore, x[:][1] == x[1] .因此, x[:][1] == x[1]


Is this consistent with what we should expect?这是否符合我们的预期? Why should x[:] be a copy of x ?为什么x[:]应该是x的副本? If you're familiar with slicing, these examples should clarify:如果您熟悉切片,这些示例应该可以阐明:

>>> x[0:4]
array([0, 1, 2, 3])

>>> x[0:6]
array([0, 1, 2, 3, 4, 5])

>>> x[0:]
array([0, 1, 2, 3, 4, 5])

>>> x[:]
array([0, 1, 2, 3, 4, 5])

We can omit the 0 and 6 and numpy will figure out what the maximum dimensions are for us.我们可以省略06 ,numpy 将计算出我们的最大尺寸。


Regarding the first part of your question, to create a copy of B , you can do any of the following:关于问题的第一部分,要创建B副本,您可以执行以下任何操作:

A = B[:, :]
A = B[...]
A = np.copy(B)

Normally, we use coordinates like x,y with the horizontal coordinate first and the vertical coordinate second.通常,我们使用像x,y这样的坐标,首先是水平坐标,然后是垂直坐标。 So numpy makes it convenient to access elements from 2D arrays by giving two coordinates in this order.因此, numpy通过按此顺序给出两个坐标,可以方便地访问 2D arrays 中的元素。

However, a 2D array is really an array of arrays;但是,二维数组实际上是 arrays 的数组; note the definition:注意定义:

foo = numpy.array([
    [1, 2],
    [3, 4],
    [5, 6]
])

If you ignore the numpy.array constructor, this is a list of lists.如果忽略numpy.array构造函数,这是一个列表列表。 The outer list has three elements (each of which is a list), and the value at index 1 is foo[1] == [3, 4] .外部列表包含三个元素(每个元素都是一个列表),索引 1 处的值是foo[1] == [3, 4] This makes sense because the matrix is represented as a list of rows , so the index used in the outer list controls the vertical coordinate, ie which row you access.这是有道理的,因为矩阵表示为rows列表,因此外部列表中使用的索引控制垂直坐标,即您访问的行。

So, in an expression like foo[1][0] == 3 , the index 1 is the vertical coordinate, and 0 is the horizontal coordinate.因此,在像foo[1][0] == 3这样的表达式中,索引 1 是垂直坐标,而 0 是水平坐标。 That is, it's equivalent to writing foo[x,y] .也就是说,它相当于写foo[x,y] It's backwards compared to the usual convention, but it follows logically from the fact that a matrix is a list of rows.与通常的约定相比,它是倒退的,但它从逻辑上遵循矩阵是行列表这一事实。 If the matrix were a list of columns, then foo[x][y] would be correct.如果矩阵是列列表,则foo[x][y]将是正确的。

For numpy arrays, this applies just the same for the slice operator.对于numpy arrays,这同样适用于切片运算符。 The expression foo[:][1] is equivalent to foo[1,:] , not foo[:,1] .表达式foo[:][1]等价于foo[1,:] ,而不是foo[:,1]

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

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