⭐在numpy中,所有赋值运算(eg.=)不会为数组和数组中的任何元素创建副本**!!!**要用numpy.ndarray.copy()函数才能创建一个副本。
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x y[0] = -1 print(x) # [-1 2 3 4 5 6 7 8] print(y) # [-1 2 3 4 5 6 7 8] x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x.copy() y[0] = -1 print(x) # [1 2 3 4 5 6 7 8] print(y) # [-1 2 3 4 5 6 7 8]⭐数组切片操作返回的对象只是原数组的视图
import numpy as np x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) y = x y[::2, :3:2] = -1 # 类似arange函数,[start:stop:step] print(x) # [[-1 12 -1 14 15] # [16 17 18 19 20] # [-1 22 -1 24 25] # [26 27 28 29 30] # [-1 32 -1 34 35]] print(y) # [[-1 12 -1 14 15] # [16 17 18 19 20] # [-1 22 -1 24 25] # [26 27 28 29 30] # [-1 32 -1 34 35]]可以形成新的数组
x[2] x[2,1] x[2][1]python的列表(list)进行切片操作得到的数组是原数组的副本,但是!!!对于numpy数据进行切片操作得到的数组则是指向相同缓冲区的视图!
切片语法:x[start:stop:step]
-n表示倒数第n,...表示全部,即与原数组的维度相同;step=-1可以表示倒序排列
假设x是五维数组
x[1,2,...] 等于 x[1,2,:,:,:]x[...,3] 等于 x[:,:,:,:,3]x[4,...,5,:] 等于 x[4,:,:,5,:]方括号内传入多个索引值,可以同时选择多个元素。对于多维数组,若只有一个索引值,则取行优先。
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) r = [0, 1, 2] print(x[r]) # [1 2 3] r = [0, 1, -1] print(x[r]) # [1 2 8] x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) r = [0, 1, 2] print(x[r]) # [[11 12 13 14 15] # [16 17 18 19 20] # [21 22 23 24 25]] r = [0, 1, -1] print(x[r]) # [[11 12 13 14 15] # [16 17 18 19 20] # [31 32 33 34 35]] r = [0, 1, 2] c = [2, 3, 4] y = x[r, c] print(y) # [13 19 25] import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) r = np.array([[0, 1], [3, 4]]) print(x[r]) # [[1 2] # [4 5]] x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) r = np.array([[0, 1], [3, 4]]) print(x[r]) # [[[11 12 13 14 15] # [16 17 18 19 20]] # # [[26 27 28 29 30] # [31 32 33 34 35]]] # 获取了 5X5 数组中的四个角的元素。 # 行索引是 [0,0] 和 [4,4],而列索引是 [0,4] 和 [0,4]。 r = np.array([[0, 0], [4, 4]]) c = np.array([[0, 4], [0, 4]]) y = x[r, c] print(y) # [[11 15] # [31 35]] import numpy as np x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) y = x[0:3, [1, 2, 2]] print(y) # [[12 13 13] # [17 18 18] # [22 23 23]]numpy.take(a, indices, axis=None, out=None, mode="raise")沿着坐标轴提取元素
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) r = [0, 1, 2] print(np.take(x, r)) # [1 2 3] r = [0, 1, -1] print(np.take(x, r)) # [1 2 8] x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) r = [0, 1, 2] print(np.take(x, r, axis=0)) # [[11 12 13 14 15] # [16 17 18 19 20] # [21 22 23 24 25]] r = [0, 1, -1] print(np.take(x, r, axis=0)) # [[11 12 13 14 15] # [16 17 18 19 20] # [31 32 33 34 35]] r = [0, 1, 2] c = [2, 3, 4] y = np.take(x, [r, c]) print(y) # [[11 12 13] # [13 14 15]]⭐注意:使用切片索引到numpy数组时,生成的数组视图将始终是原始数组的子数组, 但是!整数数组索引,不是其子数组,是形成新的数组。
常用于条件搜索
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x > 5 print(y) # [False False False False False True True True] print(x[x > 5]) # [6 7 8] x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5]) y = np.logical_not(np.isnan(x)) print(x[y]) # [1. 2. 3. 4. 5.]apply_along_axis(funcld, axis, arr)类似R里面的apply,Apply a function to 1-D slices along the given axis。
import numpy as np x = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35]]) y = np.apply_along_axis(np.sum, 0, x) print(y) # [105 110 115 120 125] y = np.apply_along_axis(np.sum, 1, x) print(y) # [ 65 90 115 140 165] y = np.apply_along_axis(np.mean, 0, x) print(y) # [21. 22. 23. 24. 25.] y = np.apply_along_axis(np.mean, 1, x) print(y) # [13. 18. 23. 28. 33.] def my_func(x): return (x[0] + x[-1]) * 0.5 y = np.apply_along_axis(my_func, 0, x) print(y) # [21. 22. 23. 24. 25.] y = np.apply_along_axis(my_func, 1, x) print(y) # [13. 18. 23. 28. 33.]