python中numpy.array_Python中的NumPy(三)——阵列数学(Array Math)

阵列数学

1、基础数学

# Basic math

x = np.array([[1,2], [3,4]], dtype=np.float64)

y = np.array([[1,2], [3,4]], dtype=np.float64)

print("x:\n",x)

print("y:\n",y)

print ("x + y:\n", np.add(x, y)) # or x + y

print ("x - y:\n", np.subtract(x, y)) # or x - y

print ("x * y:\n", np.multiply(x, y)) # or x * y

输出结果:

x:

[[1. 2.]

[3. 4.]]

y:

[[1. 2.]

[3. 4.]]

x + y:

[[2. 4.]

[6. 8.]]

x - y:

[[0. 0.]

[0. 0.]]

x * y:

[[ 1. 4.]

[ 9. 16.]]

注:这里的乘法是各自位置上的数字相乘*

2、点乘

# Dot product

a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # we can specify dtype

b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)

print("a:\n",a)

print("b:\n",b)

print (a.dot(b))

输出结果:

[[ 58. 64.]

[139. 154.]]

3、维度和

# Sum across a dimension

x = np.array([[1,2],[3,4]])

print (x)

print ("sum all: ", np.sum(x)) # adds all elements

print ("sum by col: ", np.sum(x, axis=0)) # add numbers in each column

print ("sum by row: ", np.sum(x, axis=1)) # add numbers in each row

输出结果:

[[1 2]

[3 4]]

sum all: 10

sum by col: [4 6]

sum by row: [3 7]

4、转置

# Transposing

x = np.array([[1,2],[3,4]])

print ("x:\n", x)

print ("x.T:\n", x.T)

输出结果:

x:

[[1 2]

[3 4]]

x.T:

[[1 3]

[2 4]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值