Jobs - 1 year ago 67

Python Question

a = np.asarray([1,2,3])

b = np.asarray([2,3,4,5])

a.shape

(3,)

b.shape

(4,)

I want a 3 by 4 matrix that's the product of a and b

`1`

2 * 2 3 4 5

3

np.dot(a, b.transpose())

`Traceback (most recent call last):`

File "<stdin>", line 1, in <module>

ValueError: objects are not aligned

dot product is only equivalent to matrix multiplication when the array is 2d, so np.dot doesn't work.

Answer

This is `np.outer(a, b)`

:

```
In [2]: np.outer([1, 2, 3], [2, 3, 4, 5])
Out[2]:
array([[ 2, 3, 4, 5],
[ 4, 6, 8, 10],
[ 6, 9, 12, 15]])
```

Source (Stackoverflow)