3 回答
TA贡献1842条经验 获得超12个赞
import numpy as np
m = np.array([0.2, 0.4, 1.2])
x = 5
y = 3
X = m*x+y
这被称为numpy广播(既简便又快速;)
TA贡献1872条经验 获得超3个赞
当X和Y是数组时使用爱因斯坦求和
In [70]: Y
Out[76]: array([5, 6, 7, 8, 9])
In [71]: X
Out[71]: array([0, 1, 2, 3, 4])
In [72]: m
Out[72]: [0.2, 0.4, 1.2]
In [73]: np.einsum('i,j', X, m)
Out[73]:
array([[0. , 0. , 0. ],
[0.2, 0.4, 1.2],
[0.4, 0.8, 2.4],
[0.6, 1.2, 3.6],
[0.8, 1.6, 4.8]])
In [74]: Y[...,np.newaxis] + np.einsum('i,j', X, m)
Out[74]:
array([[ 5. , 5. , 5. ],
[ 6.2, 6.4, 7.2],
[ 7.4, 7.8, 9.4],
[ 8.6, 9.2, 11.6],
[ 9.8, 10.6, 13.8]])
TA贡献1862条经验 获得超7个赞
如果您同时提供了示例x和y,那么它也会有所帮助m,但是:
In [435]: x,y = np.array([1,2,3,4]), np.array([.1,.2,.3,.4])
In [436]: m = [.2,.4,1.2]
因此,结果为(3,N):
In [437]: np.array([i*x+y for i in m])
Out[437]:
array([[0.3, 0.6, 0.9, 1.2],
[0.5, 1. , 1.5, 2. ],
[1.3, 2.6, 3.9, 5.2]])
播放m:
In [438]: np.array(m)[:,None]*x + y
Out[438]:
array([[0.3, 0.6, 0.9, 1.2],
[0.5, 1. , 1.5, 2. ],
[1.3, 2.6, 3.9, 5.2]])
哎呀,我想念你的换位,
In [440]: np.array(m)*x[:,None] + y[:,None]
Out[440]:
array([[0.3, 0.5, 1.3],
[0.6, 1. , 2.6],
[0.9, 1.5, 3.9],
[1.2, 2. , 5.2]])
我会继续将移调应用于[438]
添加回答
举报