简体   繁体   English

NumPy 2D数组普通切片与基于布尔的切片

[英]NumPy 2D array Ordinary Slicing vs Boolean based Slicing

I am new to Numpy, and I was experimenting with 2D Arrays Numpy, I made the following observations when an array is sliced in 2 different ways 我是Numpy的新手,我正在尝试2D Arrays Numpy,当以2种不同的方式对数组进行切片时,我做了以下观察

a = numpy.array([[1,2,3,4,5],[1,2,3,4,5]])
slice1 = a[:,:3]
slice1[0,0] = 100
print(a) 
-- gives, 100 2 3 4 5
           1  2 3 4 5

This behaviour is expected as per numpy docs But Consider this slicing 根据numpy docs,此行为是预期的,但请考虑此切片

a = numpy.array([[1,2,3,4,5],[1,2,3,4,5]])
slice2 = a[[True,True],:3]
slice2[0,0] = 100
print(a) 
-- gives us
1 2 3 4 5
1 2 3 4 5

Could anyone explain the difference between these 2 approaches 谁能解释这两种方法之间的区别

From the NumPy indexing documentation : NumPy索引文档中

Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view ). 高级索引总是返回数据的副本 (与返回视图的基本切片相反)。

After all, there's no way to make the strides work if you want to make some_arr[[True, False, False, True, True, False]] return a view. 毕竟,如果要使some_arr[[True, False, False, True, True, False]]返回视图some_arr[[True, False, False, True, True, False]]无法使some_arr[[True, False, False, True, True, False]]工作。

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

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