📜  NumPy - 算术运算

📅  最后修改于: 2022-05-13 01:55:02.396000             🧑  作者: Mango

NumPy - 算术运算

NumPy是一个用于执行数组计算(矩阵运算)的开源Python库。它是用 C 语言实现的库的包装器,用于执行多项三角、代数和统计运算。 NumPy 对象可以轻松转换为其他类型的对象,例如 Pandas 数据框和 tensorflow 张量。 Python list 可以用于数组计算,但是比 NumPy 慢很多。 NumPy 使用矢量化来实现其快速实现。 NumPy 数组的重要特性之一是开发人员可以使用单个命令对每个元素执行相同的数学运算。

让我们了解使用 NumPy 的算术运算。

添加

Python3
import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing addition using arithmetic operator
add_ans = a+b
print(add_ans)
 
# Performing addition using numpy function
add_ans = np.add(a, b)
print(add_ans)
 
# The same functions and operations can be used for multiple matrices
c = np.array([1, 2, 3, 4])
add_ans = a+b+c
print(add_ans)
 
add_ans = np.add(a, b, c)
print(add_ans)


Python3
import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b)
print(sub_ans)


Python3
import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b-1
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b, 1)
print(sub_ans)


Python3
import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing multiplication using arithmetic operator
mul_ans = a*b
print(mul_ans)
 
# Performing multiplication using numpy function
mul_ans = np.multiply(a, b)
print(mul_ans)


Python3
import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing division using arithmetic operators
div_ans = a/b
print(div_ans)
 
# Performing division using numpy functions
div_ans = np.divide(a, b)
print(div_ans)


Python3
# Performing mod on two matrices
mod_ans = np.mod(a, b)
print(mod_ans)
 
#Performing remainder on two matrices
rem_ans=np.remainder(a,b)
print(rem_ans)
 
# Performing power of two matrices
pow_ans = np.power(a, b)
print(pow_ans)


Python3
# Getting mean of all numbers in 'a'
mean_a = np.mean(a)
print(mean_a)
 
# Getting average of all numbers in 'b'
mean_b = np.average(b)
print(mean_b)
 
# Getting sum of all numbers in 'a'
sum_a = np.sum(a)
print(sum_a)
 
# Getting variance of all number in 'b'
var_b = np.var(b)
print(var_b)


输出

[  7  77  23 130]
[  7  77  23 130]
[  8  79  26 134]
[  8  79  26 134]

正如我们所看到的,矩阵的形状相同,如果它们不同,Numpy 会在可能的情况下尝试广播。读者可以看到,同样的操作(加法)可以使用算术运算(+)以及 numpy函数(np.add)来完成。



减法

蟒蛇3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b)
print(sub_ans)

输出

[ 3 67  3 70]
[ 3 67  3 70]

用户还可以使用矩阵和常数进行广播

蟒蛇3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing subtraction using arithmetic operator
sub_ans = a-b-1
print(sub_ans)
 
# Performing subtraction using numpy function
sub_ans = np.subtract(a, b, 1)
print(sub_ans)

输出

[ 2 66  2 69]
[ 2 66  2 69]

乘法

蟒蛇3

import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing multiplication using arithmetic operator
mul_ans = a*b
print(mul_ans)
 
# Performing multiplication using numpy function
mul_ans = np.multiply(a, b)
print(mul_ans)

输出

[  10  360  130 3000]
[  10  360  130 3000]

分配

蟒蛇3



import numpy as np
 
# Defining both the matrices
a = np.array([5, 72, 13, 100])
b = np.array([2, 5, 10, 30])
 
# Performing division using arithmetic operators
div_ans = a/b
print(div_ans)
 
# Performing division using numpy functions
div_ans = np.divide(a, b)
print(div_ans)

输出

[ 2.5        14.4         1.3         3.33333333]
[ 2.5        14.4         1.3         3.33333333]

还有无数其他函数,在 NumPy 中让我们一一看到其中的一些。

mod() 和 power()函数

例子

蟒蛇3

# Performing mod on two matrices
mod_ans = np.mod(a, b)
print(mod_ans)
 
#Performing remainder on two matrices
rem_ans=np.remainder(a,b)
print(rem_ans)
 
# Performing power of two matrices
pow_ans = np.power(a, b)
print(pow_ans)

输出

[ 1  2  3 10]
[ 1  2  3 10]
[                 25          1934917632        137858491849
 1152921504606846976]

一些聚合和统计函数

例子

蟒蛇3

# Getting mean of all numbers in 'a'
mean_a = np.mean(a)
print(mean_a)
 
# Getting average of all numbers in 'b'
mean_b = np.average(b)
print(mean_b)
 
# Getting sum of all numbers in 'a'
sum_a = np.sum(a)
print(sum_a)
 
# Getting variance of all number in 'b'
var_b = np.var(b)
print(var_b)

输出

47.5
11.75
190
119.1875