NumPy
https://www.runoob.com/numpy/numpy-indexing-and-slicing.html
https://blog.csdn.net/weixin_44385465/article/details/115050077
ndarray对象
ndarray 内部结构
属性获取
import numpy as np
# 秩
np.ndim # 即轴的数量或维度的数量
#形状
np.shape # 数组的维度,对于矩阵,n 行 m 列
#大小
np.size # 数组元素的总个数,相当于 .shape 中 n*m 的值
#元素数据类型
np.dtype # np 对象的元素类型
#元素占用空间大小
np.itemsize # np 对象中每个元素的大小,以字节为单位
#对象内存布局
np.flags # np 对象的内存信息
#实部
np.real # np元素的实部
#虚部
np.imag # np 元素的虚部
#内存地址
np.data #包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。
数组
常见数组创建
#创建空矩阵
numpy.empty(shape, dtype = float, order = 'C')
# 创建 0 矩阵
numpy.zeros(shape, dtype = float, order = 'C')
# 创建 1 矩阵
numpy.ones(shape, dtype = None, order = 'C') # 创建指定形状的数组,数组元素以 1 来填充
#创建对角矩阵 np.eye(5) 创建5行5列的二维对角阵
numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')
#已有列表、元组创建
numpy.asarray(a, dtype = None, order = None)
# x = [1,2,3] or x = (1,2,3)
# a = np.asarray(x)
创建序列数组
# 根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray
numpy.arange(start, stop, step, dtype)
# 创建一个一维等差数列数组,范围是[start,stop],元素个数num。linspace翻译成线性空间?
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# 创建一个一维等比数列数组
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
np.arange(0,12).reshape(3,4) # 创建序列数组并转换成3行4列矩阵
创建概率分布的数组
#创建shape形状的高斯(正态)分布数组
np.random.randn(shape)
#a = np.random.randn(10)
#创建均值为loc,标准差为scale,形状为size的高斯(正态)分布
np.random.normal(loc, scale, size)
#b = np.random.normal(0, 1, (2,4)) 均值为0,标准差为1,形状为(2,4)的二维数组
#创建shape形状的均匀分布数组
np.random.rand(shape)
#c = np.random.rand(2,3) 生成一个形状为(2,3)的均匀分布二维数组
#创建一个从[low, high)中随即采样的,样本数量为size的均匀分布
np.random.uniform(low, high, size)
#d = np.random.uniform(-1,1,10)
切片
索引的图示理解
高维切片
…用来表示高维情况下,选中该维度的所有数据,并与其他维度选取结果取交集
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print (a[...,1]) # 第2列元素
print (a[1,...]) # 第2行元素
print (a[...,1:]) # 第2列及剩下所有元素 嵌套的切片 1:由第二个元素开始到最后一个元素,步距默认为1
# 切片 ":" 或 "..." 与索引数组组合
b = a[1:3, 1:3] # 位于第2,3行且第2,3列部分的元素
c = a[1:3,[1,2]] # 同上
输出结果展示:
[2 4 5]
[3 4 5]
[[2 3]
[4 5]
[5 6]]
索引
a = np.arange(10) # [0 1 2 3 4 5 6 7 8 9]
b = a[5]
print(b) # 5 获取数组中第6个位置的元素
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0,1,2], [0,1,0]] # 索引结果1维
print(y) # [1 4 5] 获取数组中(0,0),(1,1)和(2,0)位置处的元素
# 获取 4X3 矩阵中的四个角的元素
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
rows = np.array([[0,0],[3,3]])
cols = np.array([[0,2],[0,2]])
y = x[rows,cols] # 索引结果2维
print(y)
#获取数组中 (0,0),(0,2),(3,0),(3,2) 位置处的元素
# array([[ 0, 2],[ 9, 11]])
布尔索引
x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])
x[x > 5]# array([ 6, 7, 8, 9, 10, 11])
# 使用"~" 过滤NaN 数据丢失 浮点型
a = np.array([np.nan, 1,2,np.nan,3,4,5])
a[~np.isnan(a)] # array([1., 2., 3., 4., 5.])
# 筛选复数 浮点型
a = np.array([1, 2+6j, 5, 3.5+5j])
a[np.iscomplex(a)] # array([2. +6.j, 3.5+5.j])
花式索引
x=np.arange(32).reshape((8,4))
x[[4,2,1,7]] # 原矩阵的第4,2,1,7行构成的新矩阵(第0行起计)
x[[-4,-2,-1,-7]] # 原矩阵的倒数第4,2,1,7行构成的新矩阵(第1行起计)
x[np.ix_([1,5,7,2],[0,3,1,2])]
print(x)
print(x.shape)
print(x[[4,2,1,7]])
print(x[np.ix_([1,5,7,2],[0,3,1,2])])
# 原矩阵的第4,2,1,7行构成的新矩阵(第0行起计),并将列按0,3,1,2顺序排序
#如果np.xi_ 中输入两个列表,则第一个列表存的是待提取元素的行标,第二个列表存的是待提取元素的列标,第一个列表中的每个元素都会遍历第二个列表中的每个值,构成新矩阵的一行元素。
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]
(8, 4)
[[16 17 18 19]
[ 8 9 10 11]
[ 4 5 6 7]
[28 29 30 31]]
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
数值计算
算数运算
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a * b
d = a + b
print(c)
print(d)
[ 10 40 90 160]
[11 22 33 44]
广播
注:当运算中两个数组的形状不同时,numpy自动触发广播机制
a = np.array(