[英]Difference between slicing of a numpy array
So, mat
is a NumPy array and I create different views from the array using slicing operation, with different rows as row1, row2, row3
.因此,
mat
是一个 NumPy 数组,我使用切片操作从数组创建不同的视图,不同的行为row1, row2, row3
。
Then I try to modify each row, but why am I not able to modify the actual array mat
in case of row3
?然后我尝试修改每一行,但是为什么在
row3
的情况下我无法修改实际的数组mat
?
mat = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
row1 = mat[0, :] #[1 2 3 4]
row2 = mat[1:2, :] #[[5 6 7 8]]
row3 = mat[[2], :] #[[ 9 10 11 12]]
row1[0] = -1 #[-1 2 3 4]
row2[0,0] = -5 #[[-5 6 7 8]]
row3[0,0] = -9 # [[-9 10 11 12]]
print(mat)
The output in this case is这种情况下的输出是
[[-1 2 3 4]
[-5 6 7 8]
[ 9 10 11 12]]
Why is row3
not referencing to the original array?为什么
row3
不引用原始数组?
您在row3
上执行的索引操作被认为是advanced_indexing ,numpy 将始终在高级索引期间创建副本,并在正常索引期间创建视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.