python - Multiply Numpy arrays by distributing terms -
i have 2 one-d numpy arrays in files.
'test1'=(2,3)
'test2'=(5,6,7)
i multiply them get
t=(10, 12, 14, 15, 18, 21)
i using program
import numpy np a=open('test1') b=open('test2') c=open('test3','w+') t1=np.loadtxt(a) t2=np.loadtxt(b) t=t1*t2 print >> c, t
when run program, following error..
valueerror: operands not broadcast shapes (2) (3)
what should desired result?
using numpy.outer , numpy.ravel
>>> import numpy np >>> = np.array((2,3)) >>> b = np.array((5,6,7)) >>> np.outer(a,b).ravel() array([10, 12, 14, 15, 18, 21])
edit:
subraction: can't use numpy.outer
, can use numpy.newaxis
:
>>> (a[:, np.newaxis] - b).ravel() array([-3, -4, -5, -2, -3, -4])
Comments
Post a Comment