numpy常用的的数学函数

数学函数

一维数组的基本运算

一维数组的运算均以 x 为例

x = np.arange(1,9)
print(x)
# [1 2 3 4 5 6 7 8]

加 np.add

y = x+1
print(y)
# [2 3 4 5 6 7 8 9]

z = np.add(x,1)
print(z)
# [2 3 4 5 6 7 8 9]

减 np.subtract

y = x-1
print(y)
# [0 1 2 3 4 5 6 7]

z = np.subtract(x,1)
print(z)
# [0 1 2 3 4 5 6 7]

乘 np.multiply

y = x*2
print(y)
# [ 2  4  6  8 10 12 14 16]

z = np.multiply(x,2)
print(z)
# [ 2  4  6  8 10 12 14 16]

除 np.divide

y = x/2
print(y)
# [0.5 1.  1.5 2.  2.5 3.  3.5 4. ]

z = np.divide(x,2)
print(z)
# [0.5 1.  1.5 2.  2.5 3.  3.5 4. ]

地板除法 np.floor_divide

y = x//2
print(y)
# [0 1 1 2 2 3 3 4]

z = np.floor_divide(x,2)
print(z)
# [0 1 1 2 2 3 3 4]

次幂运算 np.power

y = x**2
print(y)
# [ 1  4  9 16 25 36 49 64]

z = np.power(x,2)
print(z)
# [ 1  4  9 16 25 36 49 64]

二维数组的基本运算

二维数组的使用方法与一维数组的基本运算完全相同

x = np.arange(11,36).reshape(5,-1)
print(x)

# [[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+1
print(y)

# [[12 13 14 15 16]
# [17 18 19 20 21]
# [22 23 24 25 26]
# [27 28 29 30 31]
# [32 33 34 35 36]]

z = np.add(x,1)
print(z)
# [[12 13 14 15 16]
# [17 18 19 20 21]
# [22 23 24 25 26]
# [27 28 29 30 31]
# [32 33 34 35 36]]

y = x-1
print(y)

# [[10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]
# [25 26 27 28 29]
# [30 31 32 33 34]]

z = np.subtract(x,1)
print(z)
# [[10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]
# [25 26 27 28 29]
# [30 31 32 33 34]]

y = x*2
print(y)
# [[22 24 26 28 30]
# [32 34 36 38 40]
# [42 44 46 48 50]
# [52 54 56 58 60]
# [62 64 66 68 70]]

z = np.multiply(x,2)
print(z)
# [[22 24 26 28 30]
# [32 34 36 38 40]
# [42 44 46 48 50]
# [52 54 56 58 60]
# [62 64 66 68 70]]

y = x/2
print(y)
# [[ 5.5  6.   6.5  7.   7.5]
# [ 8.   8.5  9.   9.5 10. ]
# [10.5 11.  11.5 12.  12.5]
# [13.  13.5 14.  14.5 15. ]
# [15.5 16.  16.5 17.  17.5]]

z = np.divide(x,2)
print(z)
# [[ 5.5  6.   6.5  7.   7.5]
# [ 8.   8.5  9.   9.5 10. ]
# [10.5 11.  11.5 12.  12.5]
# [13.  13.5 14.  14.5 15. ]
# [15.5 16.  16.5 17.  17.5]]

地板除法

y = x//2
print(y)

# [[ 5  6  6  7  7]
# [ 8  8  9  9 10]
# [10 11 11 12 12]
# [13 13 14 14 15]
# [15 16 16 17 17]]

z = np.floor_divide(x,2)
print(z)
# [[ 5  6  6  7  7]
# [ 8  8  9  9 10]
# [10 11 11 12 12]
# [13 13 14 14 15]
# [15 16 16 17 17]]

次幂运算

y = x**2
print(y)

# [[ 121  144  169  196  225]
# [ 256  289  324  361  400]
# [ 441  484  529  576  625]
# [ 676  729  784  841  900]
# [ 961 1024 1089 1156 1225]]

z = np.power(x,2)
print(z)

# [[ 121  144  169  196  225]
# [ 256  289  324  361  400]
# [ 441  484  529  576  625]
# [ 676  729  784  841  900]
# [ 961 1024 1089 1156 1225]]

两个数组的基本运算

x = np.arange(11,36).reshape(5,-1)
y = np.arange(1,6)

print(x)
# [[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(y)
# [1 2 3 4 5]

z = x+y
print(z)
# [[12 14 16 18 20]
# [17 19 21 23 25]
# [22 24 26 28 30]
# [27 29 31 33 35]
# [32 34 36 38 40]]

print(np.add(x,y))
# [[12 14 16 18 20]
# [17 19 21 23 25]
# [22 24 26 28 30]
# [27 29 31 33 35]
# [32 34 36 38 40]]

print(x-y)
# [[10 10 10 10 10]
# [15 15 15 15 15]
# [20 20 20 20 20]
# [25 25 25 25 25]
# [30 30 30 30 30]]

print(np.subtract(x,y))
# [[10 10 10 10 10]
# [15 15 15 15 15]
# [20 20 20 20 20]
# [25 25 25 25 25]
# [30 30 30 30 30]]

print( x * y )
# [[ 11  24  39  56  75]
# [ 16  34  54  76 100]
# [ 21  44  69  96 125]
# [ 26  54  84 116 150]
# [ 31  64  99 136 175]]

print(np.multiply(x,y))
# [[ 11  24  39  56  75]
# [ 16  34  54  76 100]
# [ 21  44  69  96 125]
# [ 26  54  84 116 150]
# [ 31  64  99 136 175]]

print(x/y)
# [[11.          6.          4.33333333  3.5         3.        ]
# [16.          8.5         6.          4.75        4.        ]
# [21.         11.          7.66666667  6.          5.        ]
# [26.         13.5         9.33333333  7.25        6.        ]
# [31.         16.         11.          8.5         7.        ]]

print(np.divide(x,y))
# [[11.          6.          4.33333333  3.5         3.        ]
# [16.          8.5         6.          4.75        4.        ]
# [21.         11.          7.66666667  6.          5.        ]
# [26.         13.5         9.33333333  7.25        6.        ]
# [31.         16.         11.          8.5         7.        ]]

地板除法

print(x//y)
# [[11  6  4  3  3]
# [16  8  6  4  4]
# [21 11  7  6  5]
# [26 13  9  7  6]
# [31 16 11  8  7]]

print(np.floor_divide(x,y))
# [[11  6  4  3  3]
# [16  8  6  4  4]
# [21 11  7  6  5]
# [26 13  9  7  6]
# [31 16 11  8  7]]

次幂运算

z = x**np.full([1,5],2)
print(z)
# array([[ 121,  144,  169,  196,  225],
#       [ 256,  289,  324,  361,  400],
#       [ 441,  484,  529,  576,  625],
#       [ 676,  729,  784,  841,  900],
#       [ 961, 1024, 1089, 1156, 1225]], dtype=int32)

np.power(x,np.full([1,5],2))
# array([[ 121,  144,  169,  196,  225],
#       [ 256,  289,  324,  361,  400],
#       [ 441,  484,  529,  576,  625],
#       [ 676,  729,  784,  841,  900],
#       [ 961, 1024, 1089, 1156, 1225]], dtype=int32)

平方根 np.sqrt

x = np.arange(1,5)

y = np.sqrt(x)
print(y)
# [1.         1.41421356 1.73205081 2.        ]

平方 square

y = np.square(x)

print(y)
# [ 1  4  9 16]

次幂运算

x = np.arange(1,5)

z = np.power(x,2)
print(z)
# [ 1  4  9 16]

z = np.power(x,0.5)
print(z)

# [1.         1.41421356 1.73205081 2.        ]

三角函数

np.sin

np.cos

np.tan

np.arcsin

np.arccos

np.arctan

使用方法和数学中的一样,这里不再演示

指数和对数

x = np.arange(1,5)

print(x)
# [1 2 3 4]

np.exp()

e的幂次方

y = np.exp(x)
print(y)

# [ 2.71828183  7.3890561  20.08553692 54.59815003]

np.exp2()

计算 2 的次幂

x = np.arange(1,5)
y = np.exp2(x)

print(x)
print(y)

# [1 2 3 4]
# [ 2.  4.  8. 16.]

np.log()

对数运算,默认底数为自然数 e

z = np.log(y)
print(z)

# [1. 2. 3. 4.]

np.log2()

对数运算,底数为2

x = np.array([2,4,8,16])
y = np.log2(x)

print(y)

# [1. 2. 3. 4.]

np.log10()

对数运算,底数为10

x = np.array([10,100,1000])
y = np.log10(x)

print(y)

# [1. 2. 3.]

numpy的舍入

四舍五入 np.around

np.around(x,decimals = 0)
默认保留为整数,可指定小数点保留位数

x = np.random.rand(3,3)*10
print(x)

# [[7.51546297 9.91206407 1.70905659]
# [2.86415416 2.10984873 1.68739143]
# [9.47675178 1.50837988 3.01792099]]

y = np.around(x)

print(y)
# [[ 8. 10.  2.]
# [ 3.  2.  2.]
# [ 9.  2.  3.]]
 
z = np.around(x,decimals = 2)

print(z)
# [[4.26 8.45 2.66 3.71]
# [4.71 3.13 2.29 9.97]
# [3.63 6.61 0.11 1.34]]

向上取整 np.ceil

x = np.random.rand(3,4)*10

print(x)
# [[4.25898505 8.44619203 2.65959402 3.70606665]
# [4.71188829 3.12919592 2.29090688 9.97376315]
# [3.62651093 6.61429016 0.11042463 1.34112602]]
 
y = np.ceil(x)

print(y)
# [[ 5.  9.  3.  4.]
# [ 5.  4.  3. 10.]
# [ 4.  7.  1.  2.]]

向下取整 np.floor

z = np.floor(x)

print(z)
# [[4. 8. 2. 3.]
# [4. 3. 2. 9.]
# [3. 6. 0. 1.]]

其他函数

裁剪函数 np.clip

np.clip(x,a_min,a_max)
x中小于 a_min 的值会被赋值为 a_min , 大于 a_max 的值 会被赋值为 a_max

x = np.arange(11,36).reshape(5,-1)
print(x)

# [[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.clip(x,a_min = 20 , a_max = 30)

print(y)
# [[20 20 20 20 20]
# [20 20 20 20 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [30 30 30 30 30]]

绝对值 np.abs

x = np.arange(-5,5)

print(x)
# [-5 -4 -3 -2 -1  0  1  2  3  4]

y = np.abs(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]
x = np.arange(-10,25).reshape(5,-1)

print(x)
# [[-10  -9  -8  -7  -6  -5  -4]
# [ -3  -2  -1   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]]
 
z = np.abs(x)

print(z)
# [[10  9  8  7  6  5  4]
# [ 3  2  1  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]]

指示函数 np.sign

元素大于0,则结果为1;元素小于0,则结果为-1;元素等于0时,结果为0

x = np.arange(-5,5)
print(x)
# [-5 -4 -3 -2 -1  0  1  2  3  4]

y = np.sign(x)
print(y)
# [-1 -1 -1 -1 -1  0  1  1  1  1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值