5 数组的属性
- numpy.ndarray.ndim 用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1,二维数组的秩为 2,以此类推。
- numpy.ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
- numpy.ndarray.size 数组中所有元素的总量,相当于数组的 shape 中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。
- numpy.ndarray.dtype ndarray 对象的元素类型。
- numpy.ndarray.itemsize 以字节的形式返回数组中每一个元素的大小。
**注意:**在 ndarray 中所有元素必须是同一类型,否则会自动向下转换, int->float->str 。
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
b = np.array([1, 2, 3, 4, '5'])
print(b) # ['1' '2' '3' '4' '5']
c = np.array([1, 2, 3, 4, 5.0])
print(c) # [1. 2. 3. 4. 5.]
6 副本与视图
在 Numpy 中,尤其是在做 数组运算或数组操作 时,返回结果不是数组的 副本 就是 视图。
在 Numpy 中,所有 赋值运算 不会为数组和数组中的任何元素创建副本。
7 索引与切片
数组索引机制指的是用方括号([])加序号的形式引用单个数组元素,它的用处很多,比如抽取元素,选取数组的几个元素,甚至为其赋一个新值。
7.1 整数索引
7.2 切片索引
切片操作是指抽取数组的一部分元素生成新数组。对 python 列表进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。
**用法:**把几个用冒号( start:stop:step )隔开的数字置于方括号内。
**注意:**区间左闭右开!
(如省去第一个数字,numpy 会认为第一个数字是0;如省去第二个
数字,numpy 则会认为第二个数字是数组的最大索引值;如省去最后一个数字,它将会被理解为1,也就是抽取所有元素而不再考虑间隔。)
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]])
print(x[2:])
# [[21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]
print(x[:2])
# [[11 12 13 14 15]
# [16 17 18 19 20]]
print(x[-2:])
# [[26 27 28 29 30]
# [31 32 33 34 35]]
print(x[:-2])
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]]
x[0::2, 1::3] = 0
print(x)
# [[11 0 13 14 0]
# [16 17 18 19 20]
# [21 0 23 24 0]
# [26 27 28 29 30]
# [31 0 33 34 0]]
# 通过对每个以逗号分隔的维度执行单独的切片,你可以对多维数组进行切片。
7.3 dots 索引
NumPy 允许使用 … 表示足够多的冒号来构建完整的索引列表。
**例子:**如果 x 是 5 维数组:
- x[1,2,…] 等于 x[1,2,:,:,:]
- x[…,3] 等于 x[:,:,:,:,3]
- x[4,…,5,:] 等于 x[4,:,:,5,:]
7.4 整数数组索引
方括号内传入多个索引值,可以同时选择多个元素。
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]
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]
使用np,array
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]]
使用take
import numpy as np
r = [0, 1, -1]
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]])
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]]
7.5 布尔索引
import numpy as np
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.]
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 > 25 print(y)
# [[False False False False False]
# [False False False False False]
# [False False False False False]
# [ True True True True True]
# [ True True True True True]]
print(x[x > 25])
# [26 27 28 29 30 31 32 33 34 35]
8 数组迭代
用法: apply_along_axis(func1d, axis, arr)
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.]