【漫漫转码路】Python Day 26

Numpy(部分)

expend_dims

np.expend_dims(a, axis):膨胀,将a处上传的array_like在axis轴处膨胀升维,加一

# 例如
import numpy as np
list1 = [[1, 2], [2, 3], [3, 4]] # (3, 2)原数据是这个形状
np1 = np.array(list1)
np2 = np.expand_dims(np1, 0) # 在0轴处膨胀,就是在0轴加一维
print(np2)
print(np2.shape)
print('---------------------------')
np2 = np.expand_dims(np1, 1)
print(np2)
print(np2.shape)
print('---------------------------')
np2 = np.expand_dims(np1, 2)
print(np2)
print(np2.shape)
print('---------------------------')
np2 = np.expand_dims(np1, (1, 2)) # 可以传元组,就是一下子传好几个轴
print(np2)
print(np2.shape)
# 终端显示
[[[1 2] 
 [2 3] 
 [3 4]]]
(1, 3, 2) # 在0轴加一维,形状直接变成这样
---------------------------
[[[1 2]]
 [[2 3]]
 [[3 4]]]
(3, 1, 2) # 在1轴加一维
---------------------------
[[[1]
 [2]]
 [[2]
 [3]]
 [[3]
 [4]]]
(3, 2, 1) # 在2轴加一维
---------------------------
[[[[1 2]]]
 [[[2 3]]]
 [[[3 4]]]]
(3, 1, 1, 2)

squeeze

np.squeeze(a, axis=None):压缩,将轴上为1的维度压缩掉,如果不传参数,会默认压缩所有的轴上为1的维度,如果上传参数,则固定只压缩上传的那几个轴,如果上传了轴,但是轴上没有可压缩的,就会报错

# 例如
import numpy as np
np1 = np.arange(6).reshape(1, 2, 3, 1)
np3 = np.squeeze(np1, 0) # 指定压缩0轴
print(np3)
print(np3.shape)
np3 = np.squeeze(np1, (0, 3)) # 指定压缩0轴和3轴
print(np3)
print(np3.shape)
np3 = np.squeeze(np1) # axis为None,则默认压缩能压缩的所有
print(np3)
print(np3.shape)
# 终端显示
[[[0]
 [1]
 [2]]
 [[3]
 [4]
 [5]]]
(2, 3, 1)
[[0 1 2]
 [3 4 5]]
(2, 3)
[[0 1 2]
 [3 4 5]]
(2, 3)

concatenate

np.concatenate((a1,a2,a3,…an),axis=0):拼接,将多个数组按照指定轴axis拼接,如果axis为None,则会先将数组扁平化,然后拼接
被拼接的轴可以不一样,结果会将被凭借的轴相加
但是其他未被拼接的轴必须一样
不会自动广播
不会升维
一般是两个以上

# 例如
import numpy as np
np1 = np.arange(6).reshape(1, 2, 3, 1)
np2 = np.arange(12).reshape(2, 2, 3, 1)
c= np.concatenate((np1, np2))
print(c)
print(c.shape)
c= np.concatenate((np1, np2), None)
print(c)
print(c.shape)
# 终端显示
[[[[ 0]
 [ 1]
 [ 2]]
 [[ 3]
 [ 4]
 [ 5]]]
 [[[ 0]
 [ 1]
 [ 2]]
 [[ 3]
 [ 4]
 [ 5]]]
 [[[ 6]
 [ 7]
 [ 8]]
 [[ 9]
 [10]
 [11]]]]
(3, 2, 3, 1) # 0轴处相加
[ 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 11] # axis为None,则会扁平化
(18,)

此外
如果只传自己,且不以元组形式上传,会降维,且有奇怪的东西

#例如
import numpy as np
np1 = np.arange(12).reshape(2, 3, 2)
np2 = np.arange(24).reshape(2, 2, 2, 3, 1)
c = np.concatenate((np2))
print(c.shape)
c = np.concatenate((np2),0)
print(c.shape)
c = np.concatenate((np2),1)
print(c.shape)
c = np.concatenate((np2),2)
print(c.shape)
c = np.concatenate((np2),3)
print(c.shape)
c = np.concatenate((np2),4)
print(c.shape)
# 终端显示
(4, 2, 3, 1)
(4, 2, 3, 1)
(2, 4, 3, 1)
(2, 2, 6, 1)
(2, 2, 3, 2)
numpy.AxisError: axis 4 is out of bounds for array of dimension 4

数据少一些

# 例如
import numpy as np
np2 = np.arange(4).reshape(2, 2)
c = np.concatenate((np2), 0)
print(c.shape)
print(c)
np2 = np.arange(8).reshape(2, 2, 2)
c = np.concatenate((np2), 1)
print(c.shape)
print(c)
# 终端显示
(4,)
[0 1 2 3]
(2, 4)
[[0 1 4 5]
 [2 3 6 7]]

concatenate可以自拼接,但是规律还没有研究出来

stack / hstack / vstack

stack

np.stocl(arrays, axis=0):堆叠,将放在序列中的数组沿新轴堆叠,默认0轴堆叠
会升维

# 例如
import numpy as np
np1 = np.arange(6).reshape(2, 3)
np2 = np.arange(6).reshape(2, 3)
c = np.stack((np1, np2), axis=1) # 沿1轴堆叠,就是1轴处等于堆叠的数组个数,其他轴不变(2,2,3)
print(c)
print(c.shape)
c = np.stack((np1, np2), axis=0) # 沿0轴堆叠,就是0轴处等于堆叠的数组个数,其他轴不变(2,2,3)
print(c)
print(c.shape)
c = np.stack((np1, np2), axis=2) # 沿2轴堆叠,就是0轴处等于堆叠的数组个数,其他轴不变(2,3,2)
print(c)
print(c.shape)
# 终端显示
[[[0 1 2] 
 [0 1 2]] 
 [[3 4 5] 
 [3 4 5]]]
(2, 2, 3) # 1轴堆叠
[[[0 1 2] 
 [3 4 5]] 
 [[0 1 2] 
 [3 4 5]]]
(2, 2, 3) # 0轴堆叠
[[[0 0] 
 [1 1] 
 [2 2]] 
 [[3 3] 
 [4 4] 
 [5 5]]] 
(2, 3, 2) # 2轴堆叠

hstack

np.hstack(tup):将序列tup内的数组水平堆叠,即根据第二个轴进行拼接,如果是一维数据,则根据第一个轴拼接
不升维

# 例如
import numpy as np
np1 = np.arange(6).reshape(2, 3)
np2 = np.arange(6).reshape(2, 3)
c = np.hstack((np1, np2))
print(c)
print(c.shape)
np1 = np.arange(6).reshape(6,)
np2 = np.arange(6).reshape(6,)
c = np.hstack((np1, np2))
print(c)
print(c.shape)
# 终端显示
[[0 1 2 0 1 2]
 [3 4 5 3 4 5]]
(2, 6) # 沿1轴堆叠
[0 1 2 3 4 5 0 1 2 3 4 5]
(12,) # 对于一维数据则按照0轴堆叠

等价于np.concatenate((np1, np2), 1)

vstack

np.vstack(tup):垂直堆叠,将序列tup中的数组按照第一个轴进行堆叠,如果是一维数据,则会将一维数据转置后堆叠
不升维

# 例如
np1 = np.arange(4).reshape(2, 2)
np2 = np.arange(2).reshape(2,) # [0 1],先转置成(1, 2),也就是[[0 2]]然后进行堆叠
c = np.vstack((np1, np2))
print(c)
print(c.shape)
# 终端显示
[[0 1]
 [2 3]
 [0 1]]
(3, 2)

repeat

np.repeat(a, repeats, axis=None):将类数组a在指定轴axis重复repeats次,如果不指定axis,则会将数据想扁平化,然后再重复
注意,是逐个元素重复

# 例如
import numpy as np
np1 = np.arange(4).reshape(2, 2)
c= np.repeat(np1, 2)
print(c)
c= np.repeat(np1, 2, 0) # 0轴重复
print(c)
c= np.repeat(np1, 2, 1) # 1轴重复
print(c)
c= np.repeat(1, 2)
print(c)
c= np.repeat(np1, (1,2), 0) # 重复次数可以写元组,表示轴0的第一个元素重复一次,第二个元素重复2次
print(c)
c= np.repeat(np1, (1,2), 1)
print(c)
# 终端显示
[0 0 1 1 2 2 3 3] # 逐元素重复
[[0 1]
 [0 1] # 重复完之后进行下一个元素
 [2 3]
 [2 3]]
[[0 0 1 1]
 [2 2 3 3]] # 逐个元素先重复
 [1 1] # 标量重复,然后组成一维数组
 [[0 1]
 [2 3]
 [2 3]]
[[0 1 1]
 [2 3 3]]

unique

np.unique(ar, retuen_index=False, return_inverse=False, return_counts=False, axis=None):将传入的数组ar指定的轴axis中重复元素去掉,只保留一个,并排序,
如果不指定轴axis,则会扁平化处理
retuen_index=True的时候,会返回新数组元素在旧数组中的下标
return_inverse=True的时候,会返回新数组元素在旧数组中的下标
return_counts=True的时候,会返回去重数组中在原数组中重复次数
在保证数组方正性的情况下去重

# 例如
import numpy as np
list1 = [[1, 0, 0], [1, 0, 0], [2, 3, 4]]
np1 = np.array(list1)
print(np1)
a = np.unique(np1) # 扁平化处理
print(a)
a = np.unique(np1, return_index=True) # 扁平化处理
print(a)
a = np.unique(np1, return_index=True, return_inverse=True) # 扁平化处理
print(a)
a = np.unique(np1, return_index=True, return_inverse=True, return_counts=True) # 扁平化处理
print(a)
a = np.unique(np1, axis=0) 
print(a)
a = np.unique(np1, return_index=True, axis=0) 
print(a)
a = np.unique(np1, return_index=True, axis=1) 
print(a)
# 终端显示
[[1 0 0]
 [1 0 0]
 [2 3 4]]
[0 1 2 3 4] # 扁平化,去重,排序
(array([0, 1, 2, 3, 4]), array([1, 0, 6, 7, 8], dtype=int64)) # 这个索引是新数组元素在旧数组扁平化之后的索引
(array([0, 1, 2, 3, 4]), array([1, 0, 6, 7, 8], dtype=int64), array([1, 0, 0, 1, 0, 0, 2, 3, 4], dtype=int64)) # 新数组在旧数组中的索引
(array([0, 1, 2, 3, 4]), array([1, 0, 6, 7, 8], dtype=int64), array([1, 0, 0, 1, 0, 0, 2, 3, 4], dtype=int64), array([4, 2, 1, 1, 1], dtype=int64)) # 新数组元素在旧数组中重复次数
[[1 0 0]
 [2 3 4]]
(array([[1, 0, 0],
 [2, 3, 4]]), array([0, 2], dtype=int64)) # 对0轴去重排序,注意返回的索引,不是常规的索引,是针对0轴的索引,0轴有三个元素,分别是[1, 0, 0], [1, 0, 0], [2, 3, 4],索引对应0,1,2去掉了重复的1,
(array([[0, 0, 1],
 [0, 0, 1],
 [3, 4, 2]]), array([1, 2, 0], dtype=int64)) # 如果将[1, 0, 0]去重之后,就不能保证数组的方正性,所以不会去重,只会排序,[1, 0, 0]排序[0, 0, 1],因为索引要对应,不能说返回两部分索引,所以[2, 3, 4]也只能按照这个排序
# 例如
import numpy as np
list1 = [[[0, 1, 2, 3],
 [0, 1, 2, 3],
 [4, 5, 6, 7]],
 [[11, 12, 13, 14],
 [11, 12, 13, 14],
 [15, 16, 17, 18]]]
a = np.array(list1)
c = np.unique(a, axis=1, return_index=True)
print(c)
list1 = [[[0, 1, 2, 3],
 [0, 1, 2, 3],
 [4, 5, 6, 7]],
 [[11, 12, 13, 14],
 [15, 16, 17, 18],
 [11, 12, 13, 14]]]
a = np.array(list1)
c = np.unique(a, axis=1, return_index=True)
print(c)
# 终端显示
(array([[[ 0, 1, 2, 3],
 [ 4, 5, 6, 7]],
 [[11, 12, 13, 14],
 [15, 16, 17, 18]]]), array([0, 2], dtype=int64)) # 看返回的索引
(array([[[ 0, 1, 2, 3],
 [ 0, 1, 2, 3],
 [ 4, 5, 6, 7]],
 [[11, 12, 13, 14],
 [15, 16, 17, 18],
 [11, 12, 13, 14]]]), array([0, 1, 2], dtype=int64)) # 如果两个部分不一致,上面是0和1重复,下面是0和2重复,则不会去重

dot

np.dot(a, b):两个数组的点乘(内积)
形状为(m,n)的矩阵和形状为(n,x)的矩阵相乘,结果是形状为(m,x)
因此,
形状为(m,n)的矩阵和形状为(n,)的矩阵相乘,结果是形状为(m,)(一维矩阵)

# 例如
import numpy as np
np1 = np.arange(4).reshape(2, 2)
np2 = np.arange(6).reshape(2, 3)
a = np.dot(np1, np2)
print(a.shape)
np1 = np.arange(4).reshape(2, 2)
np2 = np.arange(2).reshape(2, )
a = np.dot(np1, np2)
print(a.shape)
# 终端显示
(2, 3)
(2,)

matmul

np.matmul(x1,x2):两个数组的乘积,
多维乘积
也可以直接用@表示

# 例如
import numpy as np
np1 = np.array([[1, 0],
 [0, 1]]) # (2, 2)
np2 = np.array([1, 2]) # (2, )
np3 = np.array([[1], [2]]) # (2, 1)
print(np.matmul(np1, np2)) # (2, 2)*(2, )=(2, )
print(np.matmul(np1, np3)) # (2, 2)*(2, 1)=(2, 1)
# 终端显示
[1 2] # 把[1, 2]转置一下求出数据,但是结果还是这么显示
[[1] 
 [2]]

多维矩阵相乘

# 例如
import numpy as np
np1 = np.arange(24).reshape(2, 3, 4)
np2 = np.arange(24).reshape(2, 4, 3)
print(np.matmul(np1, np2).shape)
np1 = np.arange(24).reshape(2, 3, 4)
np2 = np.arange(12).reshape(1, 4, 3)
print(np.matmul(np1, np2).shape)
np1 = np.arange(24).reshape(2, 3, 4)
np2 = np.arange(6).reshape(2, 1, 3)
print(np.matmul(np1, np2).shape)
np1 = np.arange(24).reshape(2, 3, 4)
np2 = np.arange(48).reshape(4, 4, 3)
print(np.matmul(np1, np2).shape)
# 终端显示
(2, 3, 3) # 可以相乘
(2, 3, 3) # 发生了广播,只有最高维度会广播
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 4) # 报错
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,3,4)->(2,newaxis,newaxis) (4,4,3)->(4,newaxis,newaxis) and requested shape (3,3) # 报错

greater / greater_equal / less / less_equal / equal / not_equal

greater(x1,x2):大于
greater_equal(x1,x2):大于等于
less (x1,x2):小于
less_equal (x1,x2):小于等于
equal (x1,x2):等于
not_equal(x1,x2):不等于
逐元素比较
x1和x2形状必须相同,或者可以广播
这些用法一样,这里只举例一个

# 例如
import numpy as np
np1 = np.array([[1, 0],
 [0, 1]]) # (2, 2)
np2 = np.array([0, 2]) # (2, )
print(np.greater(np1, np2))
print(np.greater_equal(np1, np2))

sin / cos / tan / arcsin / arccos / arctan

sin(x):x为弧度值,求正弦,与main不同,可以批量求值,x上传array_like
cos (x):x为弧度值,求余弦,与main不同,可以批量求值,x上传array_like
tan (x):x为弧度值,求正切,与main不同,可以批量求值,x上传array_like
arcsin (x):x为数值,求反正弦,与main不同,可以批量求值,x上传array_like
arccos (x):x为数值,求反余弦,与main不同,可以批量求值,x上传array_like
arctan(x):x为数值,求反正切,与main不同,可以批量求值,x上传array_like

# 例如
import numpy as np
print(np.sin(np.pi/2))
print(np.arcsin(0.5))
np1 = np.array([[np.pi, np.pi/2],
 [np.pi/3, np.pi/4]])
np2 = np.array([[1, 0.5],
 [0.333, 0.25]]) 
print(np.sin(np1))
print(np.arcsin(np2))
# 终端显示
1.0
0.5235987755982989
[[1.22464680e-16 1.00000000e+00] # 批量求
 [8.66025404e-01 7.07106781e-01]]
[[1.57079633 0.52359878] # 批量求
 [0.33948338 0.25268026]

floor / ceil

floor(x):x是一个标量或者一个array_like,返回其向下取整结果
ceil(x):x是一个标量或者一个array_like,返回其向上取整结果

# 例如
import numpy as np
print(np.floor(3.14))
print(np.ceil(3.14))
np1 = np.array([[-1.2, -0.2],
 [1.1, 3.9]])
print(np.floor(np1))
print(np.ceil(np1))
# 终端显示
3.0 # 向下取整
4.0 # 向上取整
[[-2. -1.]
 [ 1. 3.]] # 向下取整
[[-1. -0.]
 [ 2. 4.]] # 向上取整

max / min

ndarrary.max(axis=None, keepdims=False):求取最大值,如果轴axis不传,则默认全部数据,如果上传axis,则返回该轴最大值,keepdims=True则会保持维度
ndarrary.min(axis=None, keepdims=False):求取最小值,如果轴axis不传,则默认全部数据,如果上传axis,则返回该轴最小值,keepdims=True则会保持维度

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np1.max()) 
print(np1.max(0))
print(np1.max(0, keepdims=True))
print(np1.max(1))
print(np1.max(1, keepdims=True))
# 终端显示
3 # 不指定轴,默认全局
[2 2 3] # 指定0轴,0轴一共三个元素,[1, 2, 2],[0, 1, 3],[2, 1, 0],(竖着求),第一个位置最大的是2,第二个位置最大的是2,第三个位置最大的是3,所以返回2,2,3
[[2 2 3]] # 打开keepdims,是否保持维度,原来是二维所以结果还是2维
[2 3 2] # 指定1轴,1轴是2维有3个一维,就是对一维的数据求最大值,[1, 2, 2]中最大的是2,[0, 1, 3]最大的是3,[2, 1, 0]最大的是2,因此结果2, 3,2
[[2] # 保持维度,因为原来是1维数组的最大值,所以还是一维数组,
 [3]
 [2]]

mean / var / std

np.mean(axis=None, keepdims=False):求平均数,如果轴axis不传,则默认全部数据,如果上传axis,则返回该轴平均值,keepdims=True则会保持维度
np.var(axis=None, keepdims=False):求方差,如果轴axis不传,则默认全部数据,如果上传axis,则返回该轴方差,keepdims=True则会保持维度
np.std(axis=None, keepdims=False):求标准差,如果轴axis不传,则默认全部数据,如果上传axis,则返回该轴标准差,keepdims=True则会保持维度

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np1.mean()) 
print(np1.mean(0))
print(np1.mean(0, keepdims=True))
print(np1.mean(1))
print(np1.mean(1, keepdims=True))
# 终端显示
1.3333333333333333
[1. 1.33333333 1.66666667]
[[1. 1.33333333 1.66666667]]
[1.66666667 1.33333333 1. ]
[[1.66666667]
 [1.33333333]
 [1. ]]

prod / sum

np.prod(a, axis=None, keepdims=False, initial=np.No_value):累乘,返回给定数轴上的乘积,如果不传数轴axis,则会默认全部数据的乘积,initial是初始值,类似于sum函数的初始值
np.sum(a, axis=None, keepdims=False, initial=np.No_value):累加,返回给定数轴上的和,如果不传数轴axis,则会默认全部数据的和,initial是初始值,类似于sum函数的初始值

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.prod(np1)) # 默认全部数据乘积
print(np.prod(np1, axis=0)) # 指定0轴
print(np.prod(np1, axis=0, keepdims=True)) # 打开keepdims
print(np.prod(np1, axis=0, keepdims=True, initial=3)) # 初始值为3
print(np.prod(np1, axis=1)) # 指定1轴
print(np.prod(np1, axis=1, keepdims=True))
print(np.prod(np1, axis=1, keepdims=True, initial=3))
# 终端显示
0
[0 2 0]
[[0 2 0]]
[[0 6 0]]
[4 0 0]
[[4]
 [0]
 [0]]
[[12]
 [ 0]
 [ 0]]

exp / log / log2 / log10

np.exp(x):x可以是标量或array_like,求取e的x次方
np.log (x):x可以是标量或array_like,求取loge
np.log2(x):x可以是标量或array_like,求取loge2
np.log10(x):x可以是标量或array_like,求取loge10

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.exp(2))
print(np.exp(np1))
# 终端显示
7.38905609893065 # 传一个数
[[ 2.71828183 7.3890561 7.3890561 ] # 传数组
 [ 1. 2.71828183 20.08553692]
 [ 7.3890561 2.71828183 1. ]]

sort

np.sort(a, axis=-1):对返回按轴axis排序的新数组,如果axis为None,则扁平化处理,默认axis=-1是最后一个轴

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.sort(np1, axis=None))
print(np.sort(np1, axis=0))
print(np.sort(np1, axis=1))
# 终端显示
[0 0 1 1 1 2 2 2 3] # 扁平化处理
[[0 1 0] # 对0轴排序,竖着排,
 [1 1 2]
 [2 2 3]]
[[1 2 2] # 对1轴排序,横着排
 [0 1 3]
 [0 1 2]]

nonzero

np.nonzero(a):返回非零元素的索引,根据维度返回,索引可以和花式索引或布尔索引相结合

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.nonzero(np1))
print(np1[[0, 0, 0, 1, 1, 2, 2], [0, 1, 2, 1, 2, 0, 1]]) # 花式索引
# 终端显示
(array([0, 0, 0, 1, 1, 2, 2], dtype=int64), array([0, 1, 2, 1, 2, 0, 1], dtype=int64)) # 可以看到结果是分维度来的
[1 2 2 1 3 2 1] # 将上面的两个维度进行花式索引,可以得到非零元素

还可以和布尔索引相结合

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np1 >= 2)
print(np.nonzero(np1 >= 2))
print(np1[np.nonzero(np1 >= 2)])
# 终端显示
[[False True True] # 布尔索引
 [False False True]
 [ True False False]]
(array([0, 0, 1, 2], dtype=int64), array([1, 2, 2, 0], dtype=int64)) # True是1,False是0,将所有True取出来索引
[2 2 3 2] #再用一次,可以取出来所有数据

where

np.where(condition, x=None, Y=None):只传condition,返回满足条件的索引,
三个都传,如果满足条件,返回X,不满足条件,返回Y
不能只传一个X或只传一个Y
可以用于数组

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.where(np1 >= 2))
print(np.where(np1 >= 2, np1, np1*2))
print(np.where([[True, False, True],[False, True, False],[True, False, False]],[[3, 3, 3],[4, 4, 4],[5, 5, 5]],[[33, 33, 33],[44, 44, 44],[55, 55, 55]]))
# 终端显示
(array([0, 0, 1, 2], dtype=int64), array([1, 2, 2, 0], dtype=int64)) # 只返回索引
[[2 2 2]
 [0 2 3]
 [2 2 0]] # 替换
[[ 3 33 3]
 [44 4 44]
 [ 5 55 55]] # True则从X中取,False则从Y中取

argwhere

np.argwhere(a):返回元素中按元素分组的非零元素的索引

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.argwhere(np1))
# 终端显示
[[0 0] # 以元素索引的形式
 [0 1]
 [0 2]
 [1 1]
 [1 2]
 [2 0]
 [2 1]]

maximum

np.maximum(x1, x2):逐个元素返回最大值
可以广播

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
np2 = np.array([[2, 1, 0],
 [3, 4, 2],
 [0, 1, 3]]) # (3, 3)
np3 = np.array([1, 2, 3])
print(np.maximum(np1, np2))
print(np.maximum(np1, np3)) # 可以广播
# 终端显示
[[2 2 2]
 [3 4 3]
 [2 1 3]]
[[1 2 3]
 [1 2 3]
 [2 2 3]]

argmax / argmin

np.argmax(a, axis=None):返回沿轴的最大值的索引
np.argmin(a, axis=None):返回沿轴的最小值的索引
没有指定轴,则会扁平化索引
返回沿轴索引

# 例如
import numpy as np
np1 = np.array([[1, 2, 2],
 [0, 1, 3],
 [2, 1, 0]]) # (3, 3)
print(np.argmax(np1))
print(np.argmax(np1, 0))
print(np.argmax(np1, 1))
# 终端显示 
5 # 不指定轴则扁平化处理
[2 0 1] # 沿轴最大值的索引,沿0轴,就是竖着比较,[1,0,2]中最大值是2,其在0轴的索引是2,[2,1,1]中最大值是2,其在0轴的索引是0,[2,3,0]中最大值是3,其在0轴的索引是1,
[1 2 0] # 沿轴最大值的索引,沿1轴,就是横着比较,[1, 2, 2]中最大值是2,其在1轴的索引是1,[0, 1, 3]中最大值是3,其在1轴的索引是2,[2, 1, 0]中最大值是2,其在1轴的索引是0,

random

random.normal

np.random.normal(loc=0.0, scale=1.0, size=None):返回从正态分布中抽取的随机样本
loc是均值(中心),scale是标准差,size是数组形状
默认是标准正态分布
数都是随机的

# 例如
import numpy as np
print(np.random.normal(3, 5, (2, 3)))
# 终端显示
[[6.5685352 8.36012983 3.47017779]
 [8.07871987 2.42283811 4.15373768]]

random.randn

np.random.randn(d0, d1, …, dn):从标准正态分布中抽取随机样本,可以上传数组的形状
没有参数则随机返回一个数

# 例如
import numpy as np
print(np.random.randn()) # 随机返回一个数
print(np.random.randn(2)) # 随机返回两个数
print(np.random.randn(2, 3)) #随机返回一个数组
# 终端显示
-0.4835689653578073
[0.45678862 0.1008438 ]
[[ 1.10744212 -0.38539519 -1.36481226]
 [ 2.14196969 1.4920537 -0.26504477]]

random.round

np.random.round(d0, d1, …, dn):从均匀正态分布[0,1)中抽取随机样本,可以上传数组的形状
没有参数则随机返回一个数

# 例如
import numpy as np
print(np.random.rand())
print(np.random.rand(2))
print(np.random.rand(2, 3))
# 终端显示
0.5786012704263588
[0.80299364 0.72953769]
[[0.50766482 0.02612626 0.1571127 ]
 [0.04093911 0.67791842 0.44337148]]

random.radint

np.random.randint(low, high=None,size=None):返回从离散均匀分布[low,high)中抽取的随机整数

# 例如
import numpy as np
print(np.random.randint(3)) # 3以内
print(np.random.randint(3, 10)) # 3到10
print(np.random.randint(3, 10, (2, 3))) # 3到10,返回数组
# 终端显示
1
4
[[7 3 5]
 [7 7 9]]

random.uniform

np.random.uniform(low=0.0,high=1.0,size=None):返回从均匀分布[0,1)中抽取的随机样本

# 例如
import numpy as np
print(np.random.uniform())
print(np.random.uniform(3))
print(np.random.uniform(3, 10))
print(np.random.uniform(3, 10, (2, 3)))
# 终端显示
0.39469774986851736 # 默认[0,1)
1.4110007261939952 # [0,3)
9.87745655384224 # # [3,10)
[[5.26624999 8.58147261 5.44015896]
 [7.42920111 8.47692187 4.79666162]]

random.seed

np.random.seed(x):随机数种子
将随机数固定下来

# 例如
import numpy as np
np.random.seed(1) # 固定随机数
print(np.random.uniform())
np.random.seed(2)
print(np.random.uniform(3))
np.random.seed(3)
print(np.random.uniform(3, 10))
np.random.seed(4)
print(np.random.uniform(3, 10, (2, 3)))
np.random.seed() # 取消固定随机数
print(np.random.uniform())
作者:Mr_Oak原文地址:https://blog.csdn.net/Mr_Oak/article/details/128001848

%s 个评论

要回复文章请先登录注册