# [Numpy-discussion] For broadcasting, can m by n by k matrix be multiplied with n by k matrix?

Stephan Hoyer shoyer at gmail.com
Fri Apr 19 19:06:02 EDT 2019

```You may find np.einsum() more intuitive than np.dot() for aligning axes --
it's certainly more explicit.

On Fri, Apr 19, 2019 at 3:59 PM C W <tmrsg11 at gmail.com> wrote:

> Thanks, you are right. I overlooked it's for addition.
>
> The original problem was that I have matrix X (RBG image, 3 layers), and
> vector y.
>
> I wanted to do np(X, y.T).
> >>> X.shape   # 100 of 28 x 28 matrix
> (100, 28, 28)
> >>> y.shape   # Just one 28 x 28 matrix
> (1, 28, 28)
>
> But, np.dot() gives me four axis shown below,
> >>> z = np.dot(X, y.T)
> >>> z.shape
> (100, 28, 28, 1)
>
> The fourth axis is unexpected. Should y.shape be (28, 28), not (1, 28, 28)?
>
> Thanks again!
>
> On Fri, Apr 19, 2019 at 6:39 PM Andras Deak <deak.andris at gmail.com> wrote:
>
>> On Sat, Apr 20, 2019 at 12:24 AM C W <tmrsg11 at gmail.com> wrote:
>> >
>>
>> Hey,
>>
>> You are missing that the broadcasting rules typically apply to
>> arithmetic operations and methods that are specified explicitly to
>> broadcast. There is no mention of broadcasting in the docs of np.dot
>> , and its behaviour is a bit more complicated.
>> Specifically for multidimensional arrays (which you have), the doc says
>>
>> If a is an N-D array and b is an M-D array (where M>=2), it is a sum
>> product over the last axis of a and the second-to-last axis of b:
>> dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
>>
>> So your (3,4,5) @ (3,5) would want to collapse the 4-length axis of
>> `a` with the 3-length axis of `b`; this won't work. If you want
>> elementwise multiplication according to the broadcasting rules, just
>> use `a * b`:
>>
>> >>> a = np.arange(3*4*5).reshape(3,4,5)
>> ... b = np.arange(4*5).reshape(4,5)
>> ... (a * b).shape
>> (3, 4, 5)
>>
>>
>> : https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at python.org
>> https://mail.python.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190419/9858d7b6/attachment.html>
```