English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Python, slicing means taking elements from one given index to another given index.
We pass the slice instead of the index like this:[start:end].
We can also define the step length as follows:[start:end:step].
If we do not pass start, it is considered as 0.
If we do not pass end, it is considered as the length of the array within the dimension.
If we do not pass step, it is considered as 1.
Let's look at a specific operation example
>>> import numpy as np >>> arr = np.array([1, 2, 3, 4, 5, 6, 7]) >>> print(arr[1:5)) # Slice index 1 到索引 5 elements [2 3 4 5] >>> print(arr[4:]) # Slice array index 4 to the end of the elements [5 6 7] >>> print(arr[:4)) # Slice from the beginning to the index 4(excluding) elements [1 2 3 4]
使用减号运算符从末尾开始引用索引:
从末尾开始的索引 3 到末尾开始的索引 1,对数组进行裁切:
>>> import numpy as np >>> arr = np.array([1, 2, 3, 4, 5, 6, 7]) >>> print(arr[-3:-1]) [5 6]
使用 step 值确定裁切的步长
>>> import numpy as np >>> arr = np.array([1, 2, 3, 4, 5, 6, 7]) >>> print(arr[1:5:2]) # 从索引 1 到索引 5,返回相隔的元素 [2 4] >>> print(arr[::2]) # 返回数组中相隔的元素 [1 3 5 7]
]) # 从第二个元素开始,对从索引 1 到索引 4]) # 从第二个元素开始,对从索引
>>> import numpy as np >>> arr = np.array([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] >>> print(arr[1, 1:4]) # 从第二个元素开始,对从索引 1 到索引 4]) # 从两个元素中返回索引 [7 8 9] >>> print(arr[0:2, 2]) # 从两个元素中返回索引 2 [3 8] >>> print(arr[0:2, 1:4]) # 从两个元素裁切索引 1 到索引 4(不包括),这将返回一个 2-D 数组 [[2 3 4] [7 8 9]]
NumPy 提供了比一般的 Python 序列更多的索引方式。除了之前看到的用整数和切片的索引外,数组可以由整数数组索引、布尔索引及花式索引。
以下实例获取数组中(0,0),(1,1)和(2,0)位置处的元素。
>>> import numpy as np >>> x = np.array([1, 2], [3, 4], [5, 6]] >>> y = x[[1,2], [1,0]] >>> print (y) [1 4 5]
以下实例获取了 4]],而列索引是 [3 ]],而列索引是 [3,3]],而列索引是 [2]],而列索引是 [2]]。
>>> import numpy as np >>> x = np.array([ 1, 2], 3, 4, 5], 6, 7, 8], 9, 10, 11]] >>> print (x) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] >>> rows = np.array([3,3]] >>> cols = np.array([2],[0,2]] >>> y = x[rows,cols] >>> print (y) [[ 0 2] [ 9 11]]
返回的结果是包含每个角元素的 ndarray 对象。
可以借助切片 : 或 … 与索引数组组合。如下面实例:
>>> import numpy as np >>> a = np.array([1,2,3], [4,5,6],7,8,9]] >>> b = a[1:3, 1:3] >>> c = a[1:3,1,2]] >>> d = a[...,1:] >>> print(b) [[5 6] [8 9]] >>> print(c) [[5 6] [8 9]] >>> print(d) [[2 3] [5 6] [8 9]]
我们可以通过一个布尔数组来索引目标数组。
布尔索引通过布尔运算(如:比较运算符)来获取符合指定条件的元素的数组。
以下实例获取大于 5 的元素:
>>> import numpy as np >>> x = np.array([ 1, 2], 3, 4, 5], 6, 7, 8], 9, 10, 11]] >>> print (x) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] >>> print (x[x > 5]) # Now we will print out the elements greater than 5 elements [ 6 7 8 9 10 11]
The following example uses ~ (complement operator) to filter NaN.
>>> import numpy as np >>> a = np.array([np.nan, 1,2,np.nan,3,4,5]) >>> print(a[~np.isnan(a)]) [ 1. 2. 3. 4. 5j])
The following example demonstrates how to filter out non-complex elements from an array.
>>> import numpy as np >>> a = np.array([1, 2+6j, 5, 3.5+5j]) >>> print(a[np.iscomplex(a)]) [2.0+6.j 3.5+5.j]
Indexing is using integer arrays for indexing.
Indexing is based on the values of the index array as the subscripts of the target array. For using a one-dimensional integer array as an index, if the target is a one-dimensional array, then the index result is the element at the corresponding position; if the target is a two-dimensional array, then it is the row corresponding to the index.
Indexing is different from slicing, as it always copies the data to a new array.
>>> import numpy as np >>> x=np.arange(32).reshape((8,4)) >>> print(x[[4,2,1,7]) # Pass sequential index arrays [[16 17 18 19] [ 8 9 10 11] [ 4 5 6 7] [28 29 30 31]] >>> print(x[[-4,-2,-1,-7]) # Pass reverse index arrays [[16 17 18 19] [24 25 26 27] [28 29 30 31] [ 4 5 6 7]] >>> print(x[np.ix_([1,5,7,2],[0,3,1,2]) # Pass multiple index arrays (use np.ix_) [[ 4 7 5 6] [20 23 21 22] [28 31 29 30] [ 8 11 9 10]]