NumPy 提供了广泛的数组操作,包括算术运算。
NumPy 的算术运算因其能够对数组进行简单高效的计算而得到广泛应用。
在本教程中,我们将探讨 NumPy 中一些常用的算术运算,并学习如何使用它们来操作数组。
算术运算列表
以下是各种算术运算及其关联运算符和内置函数的列表
| 元素级操作 | 运算符 | 函数 |
|---|---|---|
| 加法 | + |
add() |
| 减法 | - |
subtract() |
| 乘法 | * |
multiply() |
| 除法 | / |
divide() |
| 乘方 | ** |
power() |
| 模 | % |
mod() |
要执行每种运算,我们可以使用关联的运算符或内置函数。例如,**要执行加法**,我们可以使用 + 运算符或 add() 内置函数。
接下来,我们将看到每种运算的示例。
NumPy 数组元素级加法
如前所述,我们可以使用 + 运算符和 add() 内置函数来执行两个 NumPy 数组之间的元素级加法。例如,
import numpy as np
first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])
# using the + operator
result1 = first_array + second_array
print("Using the + operator:",result1)
# using the add() function
result2 = np.add(first_array, second_array)
print("Using the add() function:",result2)
输出
Using the + operator: [ 3 7 11 15] Using the add() function: [ 3 7 11 15]
在上面的示例中,我们首先创建了两个名为:first_array 和 second_array 的数组。
然后,我们分别使用 + 运算符和 add() 函数执行元素级加法。
正如我们所见,+ 和 add() 均产生相同的结果。
NumPy 数组元素级减法
在 NumPy 中,我们可以使用 - 运算符或 subtract() 函数来执行两个 NumPy 数组之间的元素级减法。例如,
import numpy as np
first_array = np.array([3, 9, 27, 81])
second_array = np.array([2, 4, 8, 16])
# using the - operator
result1 = first_array - second_array
print("Using the - operator:",result1)
# using the subtract() function
result2 = np.subtract(first_array, second_array)
print("Using the subtract() function:",result2)
输出
Using the - operator: [ 1 5 19 65] Using the subtract() function: [ 1 5 19 65]
在这里,我们使用 - 运算符和 subtract() 函数对 first_array 和 second_array 进行了减法运算。
NumPy 数组元素级乘法
对于元素级乘法,我们可以使用 * 运算符或 multiply() 函数。例如,
import numpy as np
first_array = np.array([1, 3, 5, 7])
second_array = np.array([2, 4, 6, 8])
# using the * operator
result1 = first_array * second_array
print("Using the * operator:",result1)
# using the multiply() function
result2 = np.multiply(first_array, second_array)
print("Using the multiply() function:",result2)
输出
Using the * operator: [ 2 12 30 56] Using the multiply() function: [ 2 12 30 56]
在这里,我们使用 * 和 multiply() 来演示两种对数组进行元素级乘法的方法。
NumPy 数组元素级除法
我们可以使用 / 运算符或 divide() 函数来执行两个 NumPy 数组之间的元素级除法。例如,
import numpy as np
first_array = np.array([1, 2, 3])
second_array = np.array([4, 5, 6])
# using the / operator
result1 = first_array / second_array
print("Using the / operator:",result1)
# using the divide() function
result2 = np.divide(first_array, second_array)
print("Using the divide() function:",result2)
输出
Using the / operator: [0.25 0.4 0.5 ] Using the divide() function: [0.25 0.4 0.5 ]
在这里,我们可以看到 / 和 divide() 均产生相同的结果。
NumPy 数组元素级指数运算
数组指数运算是指将数组的每个元素提高到给定幂。
在 NumPy 中,我们可以使用 ** 运算符或 power() 函数来执行元素级指数运算。例如,
import numpy as np
array1 = np.array([1, 2, 3])
# using the ** operator
result1 = array1 ** 2
print("Using the ** operator:",result1)
# using the power() function
result2 = np.power(array1, 2)
print("Using the power() function:",result2)
输出
Using the ** operator: [1 4 9] Using the power() function: [1 4 9]
在上面的示例中,我们分别使用了 ** 运算符和 power() 函数来执行指数运算。
在这里,** 和 power() 都将 array1 的每个元素提高到 **2** 的幂。
NumPy 数组元素级取模
我们可以使用 % 运算符或 mod() 函数在 NumPy 数组中执行取模运算。
此运算计算两个数组之间元素级除法的余数。
让我们看一个例子。
import numpy as np
first_array = np.array([9, 10, 20])
second_array = np.array([2, 5, 7])
# using the % operator
result1 = first_array % second_array
print("Using the % operator:",result1)
# using the mod() function
result2 = np.mod(first_array, second_array)
print("Using the mod() function:",result2)
输出
Using the % operator: [1 0 6] Using the mod() function: [1 0 6]
在这里,我们使用 % 运算符和 mod() 函数执行了元素级取模运算。
