本文提要:本文将结束NumPy入门阶段需要掌握的所有知识点。
文章目录
一、常用操作(下)
np.stack(array,axis=0)
- arrays: Sequence[ArrayLike]
- 沿新轴连接一系列数组,序列中的数组拥有相同的shape
lis = [[1, 2, 3], [5, 6, 7]]
arrays = [np.array(lis) for _ in range(5)]
print(arrays)
print(np.stack(arrays, axis=0).shape) # 最常用
print(np.stack(arrays, axis=2).shape)
print(np.stack(arrays, axis=1))
np.hstack(tup) / np.vstack(tup)
- tup: Sequence[ArrayLike]
- 第一个是通过水平堆叠来生成数组
- 第二个是通过垂直堆叠来生成数组
a = np.array((1, 2, 3)) # 一维的
b = np.array((4, 5, 6))
print(np.hstack((a,b)))
print(np.vstack((a,b)))
c = np.array([[1], [2], [3]])
d = np.array([[4], [5], [6]])
print(np.hstack((c,d)))
print(np.vstack((c,d)))
np.repeat(a,repeats,axis=None)
- repeats: 重复次数
- axis:指定重复值的轴。没指定时,默认扁平化处理
- 重复数组中的元素
x = np.array([[1, 2], [3, 4]])
print(np.repeat(x, 2)) # 扁平化后依次重复[1 1 2 2 3 3 4 4]
print(np.repeat(x, 2, axis=0)) # 直接对行进行重复
print(np.repeat(x, [2,3], axis=1)) # 对列上的元素进行重复,第一列重复两次,第二列重复三次
np.unique(arr,return_index=False,return_inverse=False,return_counts=False,axis=None)
- arr:输入的数组或array_like
- return_index:为True时,还会返回新数组元素在旧数组中的下标
- return_inverse:为True时,还会返回旧数组元素在新数组中的下标
- return_counts:为True时,还会返回去重数组中的元素在原数组中的出现次数
- axis:指定操作的轴。没指定时,默认扁平化处理
- 返回数组中排序的唯一元素,重复元素会被去除,只保留一个
print(np.unique([3, 3, 1, 1, 2, 2, 2],return_index=True))
print(np.unique([3, 3, 1, 1, 2, 2, 2],return_inverse=True))
print(np.unique([3, 3, 1, 1, 2, 2, 2],return_counts=True))
a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
print(np.unique(a, axis=0)) # 行排序,有重复去重行,确有重复
print(np.unique(a, axis=1)) # 列排序,有重复去重列,没有重复列
np.dot(a,b)
- 两个数组的点积
a = [1, 2, 3]
b = [1, 0, 2]
print(np.dot(a, b)) # 对位相乘再相加,一般用在一维数组运算上
c = np.identity(2, dtype=np.int32)
d = [[4, 1], [2, 2]]
print(np.dot(c, d))
np.matmul(x1,x2)
其用法可以用@操作符来代替
- 两个数组的矩阵乘积
print(np.matmul(c, d))
print(c @ d) # 必须要有一个是ndarray
np.greater(x1,x2) / np.greater_equal(x1,x2) / np.less(x1,x2) / np.less_equal(x1,x2) / np.equal(x1,x2) / np.not_equal(x1,x2)
- x1,x2的形状必须相同,或者可以广播
- 第一个按元素判断x1>x2的结果
- 第二个按元素判断x1>=x2的结果
- 第三个按元素判断x1<x2的结果
- 第四个按元素判断x1<=x2的结果
- 第五个按元素判断x1==x2的结果
- 第六个按元素判断x1!=x2的结果
print(np.greater([4, 2], [2, 2]))
print(np.array([4, 2]) > np.array([2, 2]))
np.sin(x) / np.cos(x) / np.tan(x) / np.arcsin(x) / np.arccos(x) / np.arctan(x)
- x: 角度(就是幅度值)
- 正弦函数 / 余弦函数 / 正切函数 / 反正弦函数 / 反余弦函数 / 反正切函数
# Numpy中具有很多的数学函数工具,对数组中的元素进行批量处理
arr1 = np.array([np.pi/12, np.pi/6, np.pi/4, np.pi/3, np.pi/2, np.pi])
c1 = np.sin(arr1)
print(c1)
arr2 = [-1, -0.5, 0, 0.5, 1]
c2 = np.arccos(arr2)
print(c2)
np.floor(x) / np.ceil(x)
- 返回x的底限 / 上限(可进行批处理)
a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
print(np.floor(a))
print(np.ceil(a))
ndarray.max(axis=None,keepdims=False) / ndarray.min(axis=None,keepdims=False)
- 返回沿给定轴的最大/小值,axis没有指定时,默认为None,表示返回所有元素的最大/小值
lis = [[0, 1, 7, 3], [4, 9, 6, 2], [8, 5, 11, 10]]
arr1 = np.array(lis)
print(arr1)
print(arr1.max())
print(arr1.max(axis=0)) # 有三行,就是三兄弟,现在来比较三兄弟各自的家当
print(arr1.max(axis=1, keepdims=True)) # 比较内层括号中的数,并保持维度不变
ndarray.mean(axis=None,keepdims=False) / ndarray.var(axis=None,keepdims=False) / ndarray.std(axis=None,keepdims=False)
- 返回沿给定轴的平均值/方差/标准差,axis没有指定时,默认为None,表示返回所有元素的平均值/方差/标准差
print(arr1.mean(axis=0))
print(arr1.var(axis=0))
print(arr1.std(axis=0))
np.prod(a,axis=None,keepdims=np._NoValue,initial=np._NoValue) / np.sum(a,axis=None,keepdims=np._NoValue,initial=np._NoValue)
- 返回给定轴上数组元素的乘积/和
print(arr1.prod(axis=0))
print(arr1.prod(axis=0, initial=10)) # 把初始值乘进去
print(arr1.sum(axis=0))
print(arr1.sum(axis=0, initial=10)) # 把初始值加进去
np.exp(x) / np.log(x) / np.log2(x) / np.log10(x)
- 计算e的x幂次方 / 计算x的自然对数 / 计算x的以2为底的对数 / 计算x的以10为底的对数
print(np.exp([1, 2, 3]))
print(np.log([1, np.e, np.e**2]))
print(np.log2([1, 2, 2**4]))
print(np.log10([1, 10, 1e-15]))
np.sort(a,axis=-1)
- 返回排序之后的新数组
a = np.array([[4, 1], [3, 2]])
print(np.sort(a))
print(np.sort(a,axis=0))
print(np.sort(a,axis=None)) # 扁平化处理
np.nonzero(a)
- 返回非零元素的索引
x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
print(np.nonzero(x))
print(x[np.nonzero(x)]) # 组合索引
# 可以使用np.nonzero()获得数组中大于3的索引
y = np.array([[3, 7, 2], [0, 4, 1], [5, 6, 0]])
print(y[y>3]) # 获得值
print(np.nonzero(y>3)) # 获得索引
np.where(condition,x=None,y=None)
- condition: array_like, bool
- x,y: array_like,要么都传参,要么都不传
- 如果传三个参数,条件成立返回x,不成立时返回y
- 如果只传第一个参数,返回符合条件的元素的索引
print(y)
print(y>3)
print(np.where(y>3))
print(np.where(y>3, True, False))
np.argwhere(a)
- 找出数组中按元素分组的非零元素的索引
x = np.arange(6).reshape(2, 3)
coords = np.argwhere(x>1)
print(coords)
print(coords[:, 0]) # 组合索引,有降维
print(coords[:, 0:1]) # 多维切片,没降维
print(x[coords[:, 0], coords[:,1]])
print(x[np.where(x>1)])
print(x[coords[:, 0:1], coords[:,1:2]])
np.maximum(x1,x2) / np.minimum(x1,x2)
- 返回x1和x2逐个元素比较中的最大值/最小值
a = [[2, 3, 4], [1, 5, 2]]
b = [[1, 2, 7], [3, 2, 9]]
print(np.maximum(a, b))
print(np.minimum(a, b))
np.argmax(a,axis=None) / np.argmin(a,axis=None)
- 返回沿轴的最大值/最小值的索引
a = np.array([[2, 3, 4], [1, 2, 5]])
print(a.argmax()) # 默认压扁之后的索引位
print(np.argmax(a, axis=0))
np.random.normal(loc=0.0,scale=1.0,size=None)
- loc: 均值(中心)
- scale:标准差
- size:输出的形状
- 返回从正态分布中抽取的随机样本
print(np.random.normal(3, 2.5, size=(2, 4)))
np.random.randn(d0,d1,...,dn)
- 返回从标准正态分布中抽取给定形状的随机样本
print(np.random.randn()) # 没有参数返回一个值
print(np.random.randn(2, 3))
np.random.rand(d0,d1,...,dn)
- 返回从均匀分布[0,1)中抽取的给定形状的随进样本
print(np.random.rand()) # 没有参数返回一个值
print(np.random.rand(2, 3))
np.random.randint([low,]high,size=None)
- 返回从离散均匀分布[low,high)中抽取的随机整数
print(np.random.randint(20, size=10)) # 离散的
print(np.random.randint(0, 100, size=(3,2)))
np.random.uniform(low=0.0,high=1.0,size=None)
- 返回从均匀分布[low,high)中抽取的随机样本
print(np.random.uniform(20, size=10)) # 连续的
print(np.random.uniform(0, 100, size=(3,2)))
np.random.seed(x)
- 随机数种子
np.random.seed(3)
print(np.random.uniform(1, 2, size=4))
np.random.seed(5)
print(np.random.uniform(1, 2, size=4))
np.random.seed(3) # 下面的随机数被这里的随机数种子固定了
print(np.random.uniform(1, 2, size=4))
# np.random.seed() # 又变得随机了
print(np.random.uniform(1, 2, size=4))
&spm=1001.2101.3001.5002&articleId=151009016&d=1&t=3&u=f7416f30a9df41258857a26e2d3f6356)
889

被折叠的 条评论
为什么被折叠?



