我在文件中有两个一维 numpy 数组。
'test1'=(2,3)
'test2'=(5,6,7)
我想将它们相乘得到
t=(10, 12, 14, 15, 18, 21)
我正在使用这个程序
import numpy as 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
当我运行程序时,出现以下错误..
ValueError:操作数无法与形状一起广播 (2) (3)
我应该怎么做才能得到想要的结果?
最佳答案
>>> import numpy as np
>>> a = np.array((2,3))
>>> b = np.array((5,6,7))
>>> np.outer(a,b).ravel()
array([10, 12, 14, 15, 18, 21])
编辑:
减法:我们不能使用numpy.outer
,但我们可以使用numpy.newaxis
:
>>> (a[:, np.newaxis] - b).ravel()
array([-3, -4, -5, -2, -3, -4])
关于python - 通过分布项乘以 Numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894965/