Python问题集锦之Python+Numpy——数组运算

(1)数组加法:array([20,31,42,53])=array([20,30,40,50])+array([0,1,2,3])

from numpy import *
	a=array([20,30,40,50])
	b=arange(4)
	a+b

(2)数组减法:array([20,29,38,47])=array([20,30,40,50])-array([0,1,2,3])

	 from numpy import *
	a=array([20,30,40,50])
	b=arange(4)
	c=a-b

(3)数组乘法:array([2,0],[0,4])=array([1,1],[0,1])*array([2,0],[3,4])

数组点积

矩阵乘积

	  from numpy import *
	 a=array([1,1],[0,1])
	 b=array([2,0],[3,4])
	 a*b	#点积

dot表示乘积;一维数组计算的是点积;二维数组计算的是矩阵乘积。

	  from numpy import *
	 a=array([1,1],[0,1])
	 b=array([2,0],[3,4])
	 dot(a,b)	#矩阵乘积

(4)数组乘方:array([0,1,2,3])的二次方=array([0,1,4,9])

	 from numpy import *
	b=arange(4)
	b**2

(5)数组除法:array([20.,15.,10.,5.])=array([20,30,40,50])/array([1,2,4,10])

	 from numpy import *
	a=array([20,30,40,50])
	b=array([1,2,4,10])
	a/b

发表评论

%d 博主赞过: