問題描述
下面的x[...]
是什么意思?
a = np.arange(6).reshape(2,3)
for x in np.nditer(a, op_flags=['readwrite']):
x[...] = 2 * x
推薦答案
雖然建議重復Python Ellipsis 對象做什么? 在一般 python
上下文中回答了這個問題,我認為它在 nditer
循環中的使用需要添加信息.
While the proposed duplicate What does the Python Ellipsis object do? answers the question in a general python
context, its use in an nditer
loop requires, I think, added information.
https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#modifying-array-values
Python 中的正則賦值只是更改本地或全局變量字典中的引用,而不是修改現有變量.這意味著簡單地分配給 x 不會將值放入數組的元素中,而是將 x 從數組元素引用切換為對您分配的值的引用.要實際修改數組的元素,x 應使用省略號進行索引.
Regular assignment in Python simply changes a reference in the local or global variable dictionary instead of modifying an existing variable in place. This means that simply assigning to x will not place the value into the element of the array, but rather switch x from being an array element reference to being a reference to the value you assigned. To actually modify the element of the array, x should be indexed with the ellipsis.
該部分包含您的代碼示例.
That section includes your code example.
所以用我的話來說,x[...] = ...
就地修改了x
;x = ...
會破壞到 nditer
變量的鏈接,而不是更改它.它類似于 x[:] = ...
但適用于任何維度的數組(包括 0d).在這種情況下,x
不僅僅是一個數字,它還是一個數組.
So in my words, the x[...] = ...
modifies x
in-place; x = ...
would have broken the link to the nditer
variable, and not changed it. It's like x[:] = ...
but works with arrays of any dimension (including 0d). In this context x
isn't just a number, it's an array.
也許最接近這個 nditer
迭代的東西,沒有 nditer
是:
Perhaps the closest thing to this nditer
iteration, without nditer
is:
In [667]: for i, x in np.ndenumerate(a):
...: print(i, x)
...: a[i] = 2 * x
...:
(0, 0) 0
(0, 1) 1
...
(1, 2) 5
In [668]: a
Out[668]:
array([[ 0, 2, 4],
[ 6, 8, 10]])
請注意,我必須直接索引和修改 a[i]
.我無法使用 x = 2*x
.在這個迭代中,x
是一個標量,因此不可變
Notice that I had to index and modify a[i]
directly. I could not have used, x = 2*x
. In this iteration x
is a scalar, and thus not mutable
In [669]: for i,x in np.ndenumerate(a):
...: x[...] = 2 * x
...
TypeError: 'numpy.int32' object does not support item assignment
但在 nditer
的情況下,x
是一個 0d 數組,并且是可變的.
But in the nditer
case x
is a 0d array, and mutable.
In [671]: for x in np.nditer(a, op_flags=['readwrite']):
...: print(x, type(x), x.shape)
...: x[...] = 2 * x
...:
0 <class 'numpy.ndarray'> ()
4 <class 'numpy.ndarray'> ()
...
而且因為是0d,所以不能用x[:]
代替x[...]
And because it is 0d, x[:]
cannot be used instead of x[...]
----> 3 x[:] = 2 * x
IndexError: too many indices for array
更簡單的數組迭代也可能提供洞察力:
A simpler array iteration might also give insight:
In [675]: for x in a:
...: print(x, x.shape)
...: x[:] = 2 * x
...:
[ 0 8 16] (3,)
[24 32 40] (3,)
這會在 a
的行(第一個暗淡)上進行迭代.x
是一維數組,可以使用 x[:]=...
或 x[...]=...代碼>.
this iterates on the rows (1st dim) of a
. x
is then a 1d array, and can be modified with either x[:]=...
or x[...]=...
.
如果我從下一個 external_loop 標志-external-loop" rel="noreferrer">section,x
現在是一維數組,x[:] =
可以工作.但是 x[...] =
仍然有效并且更通用.x[...]
用于所有其他 nditer
示例.
And if I add the external_loop
flag from the next section, x
is now a 1d array, and x[:] =
would work. But x[...] =
still works and is more general. x[...]
is used all the other nditer
examples.
In [677]: for x in np.nditer(a, op_flags=['readwrite'], flags=['external_loop']):
...: print(x, type(x), x.shape)
...: x[...] = 2 * x
[ 0 16 32 48 64 80] <class 'numpy.ndarray'> (6,)
比較這個簡單的行迭代(在二維數組上):
Compare this simple row iteration (on a 2d array):
In [675]: for x in a:
...: print(x, x.shape)
...: x[:] = 2 * x
...:
[ 0 8 16] (3,)
[24 32 40] (3,)
這會在 a
的行(第一個暗淡)上進行迭代.x
是一維數組,可以使用 x[:] = ...
或 x[...] = ...代碼>.
this iterates on the rows (1st dim) of a
. x
is then a 1d array, and can be modified with either x[:] = ...
or x[...] = ...
.
閱讀并試驗這個 nditer
頁面,一直到最后.nditer
本身在 python
中并沒有那么有用.它不會加速迭代 - 直到您將代碼移植到 cython
.np.ndindex
是為數不多的非編譯 numpy
函數之一使用 nditer
.
Read and experiment with this nditer
page all the way through to the end. By itself, nditer
is not that useful in python
. It does not speed up iteration - not until you port your code to cython
.np.ndindex
is one of the few non-compiled numpy
functions that uses nditer
.
這篇關于“三點"是什么意思?在 Python 中,當索引什么看起來像一個數字時意味著什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!