task2 索引: y[::2, :3:2] = -1 起初这个没看懂,后来查了知道这个是切片的意思。 y[a🅱️c,d:e:f] a b c表示start:stop:step 逗号前表示行,逗号后表示列 numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。
布尔索引: x = np.array([1, 2, 3, 4, 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.]
y = np.logical_not(np.isnan(x))意思就是输出x数组中不为空的元素
x = np.linspace(0, 2 * np.pi, 50)表示:
线性空间,取值范围为0到2pi,默认包含100个点 如:linspace(0,2pi,50),那么只取50个点
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] 对每列求和 0表示列,1表示行; y = np.apply_along_axis(np.sum, 1, x) print(y) # [ 65 90 115 140 165]
1.函数原型 numpy.apply_along_axis(func, axis, arr, *args, **kwargs)
2.作用: 将arr数组的每一个元素经过func函数变换形成的一个新数组
3.参数介绍: 其中func,axis,arr是必选的 func是我们写的一个函数 axis表示函数func对arr是作用于行还是列 arr便是我们要进行操作的数组了 可选参数:*args, **kwargs。都是func()函数额外的参数。