Numpy Matrix Subtraction Confusion Numpy Matrix Subtraction Confusion numpy numpy

Numpy Matrix Subtraction Confusion


A and B are being broadcasted together:

A = np.matrix([[1],[2],[3]])#a 3x1 vector#1#2#3B = np.matrix([[1,1,1]])#a 1x3 vector#1 1 1A-B#a 3x3 vector#0 0 0#1 1 1#2 2 2

A gets broadcasted along its size 1 dimension(columns) into

#1 1 1#2 2 2#3 3 3

B get broadcasted along its size 1 dimension(rows) into

#1 1 1#1 1 1#1 1 1

Then A-B is computed as usual for two 3x3 matrices.

If you want to know why it does this instead of reporting an error it's because np.matrix inherits from np.array. np.matrix overrides multiplication, but not addition and subtraction, so it uses the operations based on the np.array, which broadcasts when the dimensions allow it.


I cannot really explain the rationale, because I often use np.matrix instead of np.array to prevent this sort of thing. Thanks to @Jaime's link in the comments above, it's clear that np.matrix is simply a subclass from np.ndarray with redefined infix operations where there is an appropriate answer from linear algebra. Where there isn't, it falls back on the rules from np.ndarray with ndim = 2.

It seems that addition follows the matrix multiplication rules for which elements from A are paired with which elements from B:

In [1]: import numpy as npIn [2]: A = np.matrix([1,2,3]).TIn [3]: B = np.matrix([1,1,1])In [4]: AOut[4]: matrix([[1],        [2],        [3]])In [5]: BOut[5]: matrix([[1, 1, 1]])In [6]: A+BOut[6]: matrix([[2, 2, 2],        [3, 3, 3],        [4, 4, 4]])In [7]: A*BOut[7]: matrix([[1, 1, 1],        [2, 2, 2],        [3, 3, 3]])

This is the same behavior you get with np.array:

In [9]: a = np.arange(3)[...,None]In [10]: b = np.arange(3)In [11]: aOut[11]: array([[0],       [1],       [2]])In [12]: bOut[12]: array([0, 1, 2])In [13]: a+bOut[13]: array([[0, 1, 2],       [1, 2, 3],       [2, 3, 4]])