python numpy数学计算基础

时间:2026-02-16 02:04:02

1、使用numpy对矩阵进行加减计算

import numpy as nx = np.array([[9,2],[9,3]], dtype=np.float64)y = np.array([[1,3],[5,7]], dtype=np.float64)print(x + y)print(np.add(x, y)) #进行加法计算

输出结果:

[[10.  5.]

 [14. 10.]]

[[10.  5.]

 [14. 10.]]

print(x - y)print(np.subtract(x, y)) #进行减法计算 

输出结果:

[[ 8. -1.]

 [ 4. -4.]]

[[ 8. -1.]

 [ 4. -4.]]

python numpy数学计算基础

python numpy数学计算基础

2、使用numpy对矩阵进行乘除计算

import numpy as n

x = np.array([[9,2],[9,3]], dtype=np.float64)y = np.array([[1,3],[5,7]], dtype=np.float64)print(x / y)print(np.divide(x, y)) #除法计算结果

输出结果:

[[9.         0.66666667]

 [1.8        0.42857143]]

[[9.         0.66666667]

 [1.8        0.42857143]]

print(x * y)print(np.multiply(x, y))#乘法计算结果

输出结果:

[[ 9.  6.]

 [45. 21.]]

[[ 9.  6.]

 [45. 21.]]

python numpy数学计算基础

python numpy数学计算基础

3、使用numpy也可以进行其它运算,包括平方,乘方,三角函数(sin,tan,cos,ctg等等)

import numpy as npx = np.array([[9,2],[9,3]], dtype=np.float64)y = np.array([[1,3],[5,7]], dtype=np.float64print(np.sqrt(x)) #平方根

输出结果:

[[3.         1.41421356]

 [3.         1.73205081]]

print(np.sin(x/y))

输出结果:

[[0.41211849 0.6183698 ]

 [0.97384763 0.41557185]]

print(np.tan(x*y))

输出结果:

[[-0.45231566 -0.29100619]

 [ 1.61977519 -1.52749853]]

python numpy数学计算基础

python numpy数学计算基础

4、numpy常用的一个数学方法,sum

sum可以计算nump中的指定和

import numpy as npx = np.array([[9,2],[9,3]], dtype=np.float64)y = np.array([[1,3],[5,7]], dtype=np.float64)print(np.sum(x))  # 计算所有元素和;

输出:23.0

print(np.sum(x, axis=0))  # 计算列的和;

输出:[18.  5.]

print(np.sum(x, axis=1))  # 计算行的和;

输出:[11. 12.]

python numpy数学计算基础

python numpy数学计算基础

© 2026 一点知道
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com