Numpy Matrix Subtraction Confusion

前端 未结 2 1356
耶瑟儿~
耶瑟儿~ 2021-02-14 08:08

I have a question about the result of an operation I accidentally performed with two numpy matrices (and later fixed).

Let\'s say that I have a column vector, A = [1,2,

2条回答
  •  温柔的废话
    2021-02-14 09:06

    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 np
    In [2]: A = np.matrix([1,2,3]).T
    In [3]: B = np.matrix([1,1,1])
    
    In [4]: A
    Out[4]: 
    matrix([[1],
            [2],
            [3]])
    
    In [5]: B
    Out[5]: matrix([[1, 1, 1]])
    
    In [6]: A+B
    Out[6]: 
    matrix([[2, 2, 2],
            [3, 3, 3],
            [4, 4, 4]])
    
    In [7]: A*B
    Out[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]: a
    Out[11]: 
    array([[0],
           [1],
           [2]])
    
    In [12]: b
    Out[12]: array([0, 1, 2])
    
    In [13]: a+b
    Out[13]: 
    array([[0, 1, 2],
           [1, 2, 3],
           [2, 3, 4]])
    

提交回复
热议问题