numpy中的赋值中arr [:]是什么意思? [英] What is the meaning of arr[:] in assignment in numpy?
问题描述
我偶尔会使用numpy
,并且我试图使我对向量进行矢量化处理变得更加聪明.我正在阅读一些代码,并试图理解以下内容的语义:
I occasionally use numpy
, and I'm trying to become smarter about how I vectorize operations. I'm reading some code and trying to understand the semantics of the following:
arr_1[:] = arr_2
在这种情况下
我知道在arr[:, 0]
中,我们正在选择数组的第一列,但是我对arr_1[:] = arr_2
和arr_1 = arr_2
I understand that in arr[:, 0]
, we're selecting the first column of the array, but I'm confused about what the difference is between arr_1[:] = arr_2
and arr_1 = arr_2
推荐答案
您的问题涉及基本Python语法和numpy
特定细节的混合.在很多方面,列表是相同的,但不完全相同.
Your question involves a mix of basic Python syntax, and numpy
specific details. In many ways it is the same for lists, but not exactly.
arr[:, 0]
返回arr
的第一列(视图),arr[:,0]=10
将该列的值设置为10.
arr[:, 0]
returns the 1st column of arr
(a view), arr[:,0]=10
sets the values of that column to 10.
arr[:]
返回arr
(alist[:]
返回列表的副本). arr[:]=arr2
执行就地替换;将arr
的值更改为arr2
的值. arr2
的值将根据需要广播和复制.
arr[:]
returns arr
(alist[:]
returns a copy of a list). arr[:]=arr2
performs an inplace replacement; changing the values of arr
to the values of arr2
. The values of arr2
will be broadcasted and copied as needed.
arr=arr2
设置arr
变量指向的对象.现在arr
和arr2
指向同一事物(无论是数组,列表还是其他任何事物).
arr=arr2
sets the object that the arr
variable is pointing to. Now arr
and arr2
point to the same thing (whether array, list or anything else).
arr[...]=arr2
在复制所有数据时也可以使用
arr[...]=arr2
also works when copying all the data
在交互式会话中玩这些动作.尝试使用arr2
形状的变体来查看如何广播值.还要检查id(arr)
以查看变量指向的对象.然后arr.__array_interface__
查看数组的数据缓冲区.这样可以帮助您将视图与副本区分开来.
Play about with these actions in an interactive session. Try variations in the shape of arr2
to see how values get broadcasted. Also check id(arr)
to see the object that the variable points to. And arr.__array_interface__
to see the data buffer of the array. That helps you distinguish views from copies.
这篇关于numpy中的赋值中arr [:]是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!