其他分享
首页 > 其他分享> > numpy中一元、二元及聚合函数的简单运用

numpy中一元、二元及聚合函数的简单运用

作者:互联网


import numpy as np
a = np.random.randint(-10,20,size=(4,5))
a
array([[ 14,  10,  17,  15,  17],
       [  3,  -6,  10,   8,  -8],
       [ 12,   5,  -1,  12,  17],
       [  2,  13,  13, -10,   3]])
b = np.random.randint(0,10,size=(4,5))
b
array([[9, 2, 9, 1, 8],
       [0, 7, 1, 8, 4],
       [0, 2, 1, 7, 5],
       [2, 1, 6, 0, 9]])
#一元函数的应用aba,sqrt,square,exp,log,log10,log2,log1p,sign,

c = np.abs(a)
c
array([[14, 10, 17, 15, 17],
       [ 3,  6, 10,  8,  8],
       [12,  5,  1, 12, 17],
       [ 2, 13, 13, 10,  3]])
d = np.sqrt(c)
d
array([[3.74165739, 3.16227766, 4.12310563, 3.87298335, 4.12310563],
       [1.73205081, 2.44948974, 3.16227766, 2.82842712, 2.82842712],
       [3.46410162, 2.23606798, 1.        , 3.46410162, 4.12310563],
       [1.41421356, 3.60555128, 3.60555128, 3.16227766, 1.73205081]])
e = np.square(b)
e
array([[ 9],
       [ 0],
       [ 1],
       [25]], dtype=int32)
f = np.exp(b)
f
array([[ 20.08553692],
       [  1.        ],
       [  2.71828183],
       [148.4131591 ]])
g = np.log(b)
g
<ipython-input-12-6f4b0dbe2fee>:1: RuntimeWarning: divide by zero encountered in log
  g = np.log(b)
array([[1.09861229],
       [      -inf],
       [0.        ],
       [1.60943791]])
h = np.sign(a)
h
array([[ 1,  1,  1,  1,  1],
       [ 1, -1,  1,  1, -1],
       [ 1,  1, -1,  1,  1],
       [ 1,  1,  1, -1,  1]])
j = np.ceil(a)
j
array([[ 14.,  10.,  17.,  15.,  17.],
       [  3.,  -6.,  10.,   8.,  -8.],
       [ 12.,   5.,  -1.,  12.,  17.],
       [  2.,  13.,  13., -10.,   3.]])
二元函数运用
#二元函数运用
k = np.add(a,b)
k
array([[ 23,  12,  26,  16,  25],
       [  3,   1,  11,  16,  -4],
       [ 12,   7,   0,  19,  22],
       [  4,  14,  19, -10,  12]])
l
l = np.subtract(a,b)
l
array([[  5,   8,   8,  14,   9],
       [  3, -13,   9,   0, -12],
       [ 12,   3,  -2,   5,  12],
       [  0,  12,   7, -10,  -6]])
z
z = np.floor_divide(k,l)
z
<ipython-input-22-332b19935c94>:1: RuntimeWarning: divide by zero encountered in floor_divide
  z = np.floor_divide(k,l)
array([[ 4,  1,  3,  1,  2],
       [ 1, -1,  1,  0,  0],
       [ 1,  2,  0,  3,  1],
       [ 0,  1,  2,  1, -2]], dtype=int32)
x
x = np.equal(k,l)
x
array([[False, False, False, False, False],
       [ True, False, False, False, False],
       [ True, False, False, False, False],
       [False, False, False,  True, False]])
v = (k>=l)
print(type(v))
<class 'numpy.ndarray'>
n = np.max(k)
n
26
总结:聚合函数可以指定行计算或者列计算,默认是计算整体。
m = np.mean(l,axis=0)
m
array([5.  , 2.5 , 5.5 , 2.25, 0.75])
p
p = np.argmin(k)
p
18

 

标签:一元,12,False,二元,17,10,np,array,numpy
来源: https://blog.csdn.net/lsmax/article/details/118048763