English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Der Schnitt in Python bedeutet, Elemente von einem gegebenen Index bis zu einem anderen Index zu bringen.
Wir übergeben den Schnitt so, wie wir es tun, anstatt der Indizes:[start:end].
Wir können auch den Schritt definieren, wie folgt:[start:end:step].
Wenn wir start nicht übergeben, wird es als 0 betrachtet.
Wenn wir end nicht übergeben, wird es als die Länge des Arrays in der Dimension betrachtet.
Wenn wir step nicht übergeben, wird es als 1.
Sehen wir uns einen konkreten Beispieloperation
>>> import numpy as np ,对数组进行裁切:1, 2, 3, 4, 5, 6, 7]) ]) # 从第二个元素开始,对从索引1:5]) # Schneiden der Index 1 ]) # 从两个元素裁切索引 5 的元素 [2 3 4 5] ]) # 从第二个元素开始,对从索引4:]) # Schneiden des Arrays an der Index 4 bis zum Ende der Elemente [5 6 7] >>> print(arr[:4]] # Schneiden vom Anfang bis zur Index 4der Elemente (nicht einschließlich) [1 2 3 4]
使用减号运算符从末尾开始引用索引:
从末尾开始的索引 3 negative slicing 1使用减号运算符从末尾开始引用索引:
>>> import numpy as np ,对数组进行裁切:1, 2, 3, 4, 5, 6, 7]) ]) # 从第二个元素开始,对从索引-3:-1]) [5 6]
到末尾开始的索引
>>> import numpy as np ,对数组进行裁切:1, 2, 3, 4, 5, 6, 7]) ]) # 从第二个元素开始,对从索引1:5:2STEP Schrittlänge 1 ]) # 从两个元素裁切索引 5使用 step 值确定裁切的步长 [2 4] >>> arr = np.array([2]) # 从索引 [1 3 5 7]
]) # 返回数组中相隔的元素 1 ]) # 从两个元素裁切索引 4裁切
>>> import numpy as np D 数组1, 2, 3, 4, 5],6, 7, 8, 9, 10]] ]) # 从第二个元素开始,对从索引1, 1:4>>> arr = np.array([ 1 ]) # 从两个元素裁切索引 4>>> print(arr[ [7 8 9] ]) # 从两个元素中返回索引2, 2]) # 从第二个元素开始,对从索引 2 [3 8] ]) # 从两个元素中返回索引2, 1:4>>> print(arr[0: 1 ]) # 从两个元素裁切索引 4到索引 2-(不包括),这将返回一个 [[2 3 4] [7 8 9]]
D 数组
整数数组索引1,1以下实例获取数组中(0,0),(2)和(
>>> import numpy as np ,0)位置处的元素。1, 2],3, 4],5, 6]] >>> x = np.array([1,2>>> y = x[[1], >>> print(y) [1 4 5]
,0]] 4以下实例获取了3 X3,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]) # 现在我们会打印出大于 5 的元素 [ 6 7 8 9 10 11]
以下实例使用了 ~(取补运算符)来过滤 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])
以下实例演示如何从数组中过滤掉非复数元素。
>>> 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]
花式索引指的是利用整数数组进行索引。
花式索引根据索引数组的值作为目标数组的某个轴的下标来取值。对于使用一维整型数组作为索引,如果目标是一维数组,那么索引的结果就是对应位置的元素;如果目标是二维数组,那么就是对应下标的行。
花式索引与切片不同,它总是将数据复制到新数组中。
>>> import numpy as np >>> x=np.arange(32).reshape(8,4)) >>> print (x[[4,2,1,7]) # 传入顺序索引数组 [[16 17 18 19] [ 8 9 10 11] [ 4 5 6 7] [28 29 30 31]] >>> print (x[[-4,-2,-1,-7]) # 传入倒序索引数组 [[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)) # 传入多个索引数组(要使用np.ix_) [[ 4 7 5 6] [20 23 21 22] [28 31 29 30] [ 8 11 9 10]]