-
红颜莎娜
import numpy as np m = np.array([0.2, 0.4, 1.2])x = 5y = 3X = m*x+y这被称为numpy广播(既简便又快速;)
-
守着一只汪
当X和Y是数组时使用爱因斯坦求和In [70]: YOut[76]: array([5, 6, 7, 8, 9])In [71]: XOut[71]: array([0, 1, 2, 3, 4])In [72]: mOut[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]])
-
牧羊人nacy
如果您同时提供了示例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 + yOut[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]