Numpy入门教程:04. 数学函数
背景
什么是 NumPy 呢?
NumPy 这个词来源于两个单词 – Numerical
和Python
。其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景:
- 执行各种数学任务,如:数值积分、微分、内插、外推等。因此,当涉及到数学任务时,它形成了一种基于 Python 的 MATLAB 的快速替代。
- 计算机中的图像表示为多维数字数组。NumPy 提供了一些优秀的库函数来快速处理图像。例如,镜像图像、按特定角度旋转图像等。
- 在编写机器学习算法时,需要对矩阵进行各种数值计算。如:矩阵乘法、求逆、换位、加法等。NumPy 数组用于存储训练数据和机器学习模型的参数。
向量化和广播
向量化和广播这两个概念是 numpy 内部实现的基础。有了向量化,编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。
广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。
若两个数组的各维度兼容,也就是两个数组的每一维等长,或其中一个数组为一维,那么广播机制就适用。如果这两个条件不满足,numpy就会抛出异常,说两个数组不兼容。
【例】
import numpy as npx = np.arange(4)
xx = x.reshape(4, 1)
y = np.ones(5)
z = np.ones((3, 4))print(x.shape) # (4,)
print(y.shape) # (5,)
print(xx.shape) # (4, 1)
print(z.shape) # (3, 4)print(x + y)
# ValueError: operands could not be broadcast together with shapes (4,) (5,) print((xx + y).shape) # (4, 5)
print(xx + y)
# [[1. 1. 1. 1. 1.]
# [2. 2. 2. 2. 2.]
# [3. 3. 3. 3. 3.]
# [4. 4. 4. 4. 4.]]print((x + z).shape) # (3, 4)
print(x + z)
# [[1. 2. 3. 4.]
# [1. 2. 3. 4.]
# [1. 2. 3. 4.]]a = np.array([0.0, 10.0, 20.0, 30.0])
b = np.array([1.0, 2.0, 3.0])
c = a[:, np.newaxis] + b
print(c)
# [[ 1. 2. 3.]
# [11. 12. 13.]
# [21. 22. 23.]
# [31. 32. 33.]]
数学函数
算数运算
numpy.add(x1, x2, *args, **kwargs)
Add arguments element-wise.numpy.subtract(x1, x2, *args, **kwargs)
Subtract arguments element-wise.numpy.multiply(x1, x2, *args, **kwargs)
Multiply arguments element-wise.numpy.divide(x1, x2, *args, **kwargs)
Returns a true division of the inputs, element-wise.numpy.floor_divide(x1, x2, *args, **kwargs)
Return the largest integer smaller or equal to the division of the inputs.numpy.power(x1, x2, *args, **kwargs)
First array elements raised to powers from second array, element-wise.
在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。
【例】
import numpy as npx = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x + 1
print(y)
print(np.add(x, 1))
# [2 3 4 5 6 7 8 9]y = x - 1
print(y)
print(np.subtract(x, 1))
# [0 1 2 3 4 5 6 7]y = x * 2
print(y)
print(np.multiply(x, 2))
# [ 2 4 6 8 10 12 14 16]y = x / 2
print(y)
print(np.divide(x, 2))
# [0.5 1. 1.5 2. 2.5 3. 3.5 4. ]y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [0 1 1 2 2 3 3 4]y = x ** 2
print(y)
print(np.power(x, 2))
# [ 1 4 9 16 25 36 49 64]
【例】
import numpy as npx = np.array([[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)
print(np.add(x, 1))
# [[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)
print(np.subtract(x, 1))
# [[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)
print(np.multiply(x, 2))
# [[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)
print(np.divide(x, 2))
# [[ 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)
print(np.floor_divide(x, 2))
# [[ 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)
print(np.power(x, 2))
# [[ 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]]
【例】
import numpy as npx = np.array([[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.arange(1, 6)
print(y)
# [1 2 3 4 5]z = x + y
print(z)
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]]z = x - y
print(z)
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]]z = x * y
print(z)
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]]z = x / y
print(z)
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. ]]z = x // y
print(z)
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)
print(np.power(x, np.full([5, 5], 2)))
# [[ 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]]
【例】
import numpy as npx = np.array([[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.arange(1, 26).reshape([5, 5])
print(y)
# [[ 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]]z = x + y
print(z)
print(np.add(x, y))
# [[12 14 16 18 20]
# [22 24 26 28 30]
# [32 34 36 38 40]
# [42 44 46 48 50]
# [52 54 56 58 60]]z = x - y
print(z)
print(np.subtract(x, y))
# [[10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]]z = x * y
print(z)
print(np.multiply(x, y))
# [[ 11 24 39 56 75]
# [ 96 119 144 171 200]
# [231 264 299 336 375]
# [416 459 504 551 600]
# [651 704 759 816 875]]z = x / y
print(z)
print(np.divide(x, y))
# [[11. 6. 4.33333333 3.5 3. ]
# [ 2.66666667 2.42857143 2.25 2.11111111 2. ]
# [ 1.90909091 1.83333333 1.76923077 1.71428571 1.66666667]
# [ 1.625 1.58823529 1.55555556 1.52631579 1.5 ]
# [ 1.47619048 1.45454545 1.43478261 1.41666667 1.4 ]]z = x // y
print(z)
print(np.floor_divide(x, y))
# [[11 6 4 3 3]
# [ 2 2 2 2 2]
# [ 1 1 1 1 1]
# [ 1 1 1 1 1]
# [ 1 1 1 1 1]]z = x ** np.full([5, 5], 2)
print(z)
print(np.power(x, np.full([5, 5], 2)))
# [[ 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]]
三角函数
numpy.sin(x, *args, **kwargs)
Trigonometric sine, element-wise.numpy.cos(x, *args, **kwargs)
Cosine element-wise.numpy.tan(x, *args, **kwargs)
Compute tangent element-wise.numpy.arcsin(x, *args, **kwargs)
Inverse sine, element-wise.numpy.arccos(x, *args, **kwargs)
Trigonometric inverse cosine, element-wise.numpy.arctan(x, *args, **kwargs)
Trigonometric inverse tangent, element-wise.
通用函数(universal function)通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。
三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的sqrt()
函数、用来取对数的log()
函数和求正弦值的sin()
函数。
【例】
import numpy as npx = np.linspace(start=0, stop=np.pi / 2, num=10)
print(x)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]y = np.sin(x)
print(y)
# [0. 0.17364818 0.34202014 0.5 0.64278761 0.76604444
# 0.8660254 0.93969262 0.98480775 1. ]z = np.arcsin(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]y = np.cos(x)
print(y)
# [1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
# 7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
# 1.73648178e-01 6.12323400e-17]z = np.arccos(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]y = np.tan(x)
print(y)
# [0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
# 8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
# 5.67128182e+00 1.63312394e+16]z = np.arctan(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
指数和对数
numpy.exp(x, *args, **kwargs)
Calculate the exponential of all elements in the input array.numpy.log(x, *args, **kwargs)
Natural logarithm, element-wise.numpy.exp2(x, *args, **kwargs)
Calculate2**p
for allp
in the input array.numpy.log2(x, *args, **kwargs)
Base-2 logarithm ofx
.numpy.log10(x, *args, **kwargs)
Return the base 10 logarithm of the input array, element-wise.
【例】The natural logarithm log
is the inverse of the exponential function, so that log(exp(x)) = x
. The natural logarithm is logarithm in base e
.
import numpy as npx = np.arange(1, 5)
print(x)
# [1 2 3 4]
y = np.exp(x)
print(y)
# [ 2.71828183 7.3890561 20.08553692 54.59815003]
z = np.log(y)
print(z)
# [1. 2. 3. 4.]
加法与乘法函数
numpy.sum(a[, axis=None, dtype=None, out=None, …])
Sum of array elements over a given axis.
通过不同的 axis
,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0
,则沿着纵轴进行操作;axis=1
,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:++设axis=i
,则 numpy 沿着第i
个下标变化的方向进行操作++。
聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray
类实现了多个这样的函数。
【例】返回给定轴上的数组元素的总和。
import numpy as npx = np.array([[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.sum(x)
print(y) # 575y = np.sum(x, axis=0)
print(y) # [105 110 115 120 125]y = np.sum(x, axis=1)
print(y) # [ 65 90 115 140 165]
numpy.cumsum(a, axis=None, dtype=None, out=None)
Return the cumulative sum of the elements along a given axis.
【例】返回给定轴上的数组元素的累加和。
import numpy as npx = np.array([[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.cumsum(x)
print(y)
# [ 11 23 36 50 65 81 98 116 135 155 176 198 221 245 270 296 323 351
# 380 410 441 473 506 540 575]y = np.cumsum(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 27 29 31 33 35]
# [ 48 51 54 57 60]
# [ 74 78 82 86 90]
# [105 110 115 120 125]]y = np.cumsum(x, axis=1)
print(y)
# [[ 11 23 36 50 65]
# [ 16 33 51 70 90]
# [ 21 43 66 90 115]
# [ 26 53 81 110 140]
# [ 31 63 96 130 165]]
numpy.prod(a[, axis=None, dtype=None, out=None, …])
Return the product of array elements over a given axis.
【例】返回给定轴上数组元素的乘积。
import numpy as npx = np.array([[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.prod(x)
print(y) # 788529152y = np.prod(x, axis=0)
print(y)
# [2978976 3877632 4972968 6294624 7875000]y = np.prod(x, axis=1)
print(y)
# [ 360360 1860480 6375600 17100720 38955840]
numpy.cumprod(a, axis=None, dtype=None, out=None)
Return the cumulative product of elements along a given axis.
【例】返回给定轴上数组元素的累乘。
import numpy as npx = np.array([[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.cumprod(x)
print(y)
# [ 11 132 1716 24024 360360 5765760
# 98017920 1764322560 -837609728 427674624 391232512 17180672
# 395155456 893796352 870072320 1147043840 905412608 -418250752
# 755630080 1194065920 -1638662144 -897581056 444596224 -2063597568
# 788529152]y = np.cumprod(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 176 204 234 266 300]
# [ 3696 4488 5382 6384 7500]
# [ 96096 121176 150696 185136 225000]
# [2978976 3877632 4972968 6294624 7875000]]y = np.cumprod(x, axis=1)
print(y)
# [[ 11 132 1716 24024 360360]
# [ 16 272 4896 93024 1860480]
# [ 21 462 10626 255024 6375600]
# [ 26 702 19656 570024 17100720]
# [ 31 992 32736 1113024 38955840]]
四舍五入
numpy.around(a, decimals=0, out=None)
Evenly round to the given number of decimals.
【例】将数组舍入到给定的小数位数。
import numpy as npx = np.random.rand(3, 3) * 10
print(x)
# [[6.59144457 3.78566113 8.15321227]
# [1.68241475 3.78753332 7.68886328]
# [2.84255822 9.58106727 7.86678037]]y = np.around(x)
print(y)
# [[ 7. 4. 8.]
# [ 2. 4. 8.]
# [ 3. 10. 8.]]y = np.around(x, decimals=2)
print(y)
# [[6.59 3.79 8.15]
# [1.68 3.79 7.69]
# [2.84 9.58 7.87]]
numpy.ceil(x, *args, **kwargs)
Return the ceiling of the input, element-wise.numpy.floor(x, *args, **kwargs)
Return the floor of the input, element-wise.
【例】
import numpy as npx = np.random.rand(3, 3) * 10
print(x)
# [[0.67847795 1.33073923 4.53920122]
# [7.55724676 5.88854047 2.65502046]
# [8.67640444 8.80110812 5.97528726]]y = np.ceil(x)
print(y)
# [[1. 2. 5.]
# [8. 6. 3.]
# [9. 9. 6.]]y = np.floor(x)
print(y)
# [[0. 1. 4.]
# [7. 5. 2.]
# [8. 8. 5.]]
杂项
numpy.clip(a, a_min, a_max, out=None, **kwargs):
Clip (limit) the values in an array.
Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1]
is specified, values smaller than 0 become 0, and values larger than 1 become 1.
【例】裁剪(限制)数组中的值。
import numpy as npx = np.array([[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]]
numpy.sqrt(x, *args, **kwargs)
Return the non-negative square-root of an array, element-wise.numpy.square(x, *args, **kwargs)
Return the element-wise square of the input.
【例】
import numpy as npx = np.arange(1, 5)
print(x) # [1 2 3 4]y = np.sqrt(x)
print(y)
# [1. 1.41421356 1.73205081 2. ]y = np.square(x)
print(y)
# [ 1 4 9 16]
numpy.absolute(x, *args, **kwargs)
Calculate the absolute value element-wise.numpy.abs(x, *args, **kwargs)
is a shorthand for this function.
【例】
import numpy as npx = 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]y = np.absolute(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]
当前活动
我是 终身学习者“老马”,一个长期践行“结伴式学习”理念的 中年大叔。
我崇尚分享,渴望成长,于2010年创立了“LSGO软件技术团队”,并加入了国内著名的开源组织“Datawhale”,也是“Dre@mtech”、“智能机器人研究中心”和“大数据与哲学社会科学实验室”的一员。
愿我们一起学习,一起进步,相互陪伴,共同成长。
后台回复「搜搜搜」,随机获取电子资源!
欢迎关注,请扫描二维码:
Numpy入门教程:04. 数学函数相关推荐
- numpy 数组抽取_清晰易懂的Numpy入门教程
原标题:清晰易懂的Numpy入门教程 翻译 | 石头 来源 | Machine Learning Plus Numpy是python语言中最基础和最强大的科学计算和数据处理的工具包,如数据分析工具pa ...
- python numpy教程_Python中的Numpy入门教程
这篇文章主要介绍了 Python 中的 Numpy 入门教程,着重讲解了矩阵中的数组操作 , 需要的 朋友可以参考下 1 . Numpy 是什么 很简单, Numpy 是 Python 的一个科学计算 ...
- Matlab入门教程--基本运算与函数(一)
Matlab入门教程--基本运算与函数(一) 在MATLAB下进行基本数学运算,只需将运算式直接打入提示号(>>)之後,并按入Enter键即可.例如: >>(5*2+1.3-0 ...
- 【CC2530入门教程-04】CC2530的定时/计数器原理与应用
[CC2530入门教程-06]CC2530的ADC工作原理与应用 [CC2530入门教程-05]CC2530的串行接口原理与应用 [CC2530入门教程-04]CC2530的定时/计数器原理与应用 [ ...
- TypeScript入门教程 之 箭头函数
TypeScript入门教程 之 箭头函数 亲切地称为粗箭头(因为->是细箭头并且=>是粗箭头),也被称为lambda函数(由于其他语言).另一个常用功能是胖箭头功能()=>some ...
- TypeScript入门教程 之 生成器函数
TypeScript入门教程 之 生成器函数 生成器函数 function *是用于创建生成器函数的语法.调用generator函数将返回一个generator对象.发电机对象如下刚刚所述迭代器接口( ...
- 【数据分析】Numpy入门教程(超详细)
Numpy 教程 什么是Numpy?Numpy(Numerical Python)是一个Python扩展库,支持大量的维度数组和矩阵运算,此外也针对数组运算提供大量的数学函数库. 其主要用于数组计算, ...
- c# 定位内存快速增长_CTF丨Linux Pwn入门教程:针对函数重定位流程的相关测试(下)...
Linux Pwn入门教程系列分享已到尾声,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...
- C#,入门教程(04)——Visual Studio 2022 数据编程实例:随机数与组合
上一篇: C#,入门教程(03)--Visual Studio 2022编写彩色Hello World与动画效果https://blog.csdn.net/beijinghorn/article/de ...
最新文章
- QAQorz的训练记录
- Android之ActivityLifecycleCallbacks的得到当前的activity的状态
- 2018蓝桥杯省赛---java---C---4( 第几个幸运数)
- spring学习(16):使用接口
- ubuntu18 安装mysql8安装包_Ubuntu18.04深度学习环境配置
- python异常处理知识点_一文掌握 Python 异常处理的所有知识点
- 这只拒绝内卷的 AI 狼火了!高智商却自暴自弃,不想抓羊只想躺
- minecraft我的世界服务器自动备份脚本
- 001 【Chrome】浏览器自带取色器
- 【python】【selenium】Selenium 自动化如何进行文件上传?
- Web运维之安全配置指导手册
- python图片截取斜四边形_python shapely.geometry.polygon任意两个四边形的IOU计算实例...
- ArcMap关联图层并发布为服务
- java基于Springboot+vue的球鞋销售商城网站 elementui
- Vlog 是短视频发展的新催化剂?
- TKDragView_iOS开发常用第三方开源框架 持续更新中...
- 使用朴素贝叶斯过滤垃圾邮件
- 有能力的人从不抱怨大环境!
- 使用Web前端技术实现的梦幻VR虚拟现实效果
- 阿里云分布式关系型数据库(DRDS)