天天看點

numpy中dot, multiply, *差別

1.dot

首先看下dot源碼中的注釋部分

def dot(a, b, out=None):
    """
    dot(a, b, out=None)

    Dot product of two arrays. Specifically,

    - If both `a` and `b` are 1-D arrays, it is inner product of vectors
      (without complex conjugation).

    - If both `a` and `b` are 2-D arrays, it is matrix multiplication,
      but using :func:`matmul` or ``a @ b`` is preferred.

    - If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply`
      and using ``numpy.multiply(a, b)`` or ``a * b`` is preferred.

    - If `a` is an N-D array and `b` is a 1-D array, it is a sum product over
      the last axis of `a` and `b`.

    - 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])
 .....
           

關注一下最常用的兩種情況:

If both

a

and

b

are 1-D arrays, it is inner product of vectors

這就是兩個向量dot,最後得到的兩個向量的内積。

If both

a

and

b

are 2-D arrays, it is matrix multiplication, but using :func:

matmul

or ``a @ b`` is preferred.

2-D arrays指的就是矩陣了。根據上面的解釋不難看出,如果是兩個矩陣dot,執行的就是矩陣相乘運算。

寫段代碼測試下

def demo2():
    a1 = np.arange(1, 5)
    a2 = a1[::-1]
    print(a1)
    print(a2)
    # 兩個向量dot為内積
    print(a1.dot(a2))
    print(np.dot(a1, a2))
    print("\n\n")

    b1 = np.arange(1, 5).reshape(2, 2)
    b2 = np.arange(5, 9).reshape(2, 2)
    b3 = np.arange(9, 15).reshape(3, 2)
    print(b1)
    print(b2)
    print(b3)
    print(np.dot(b1, b2))
    # 會報錯, 不滿足矩陣相乘條件
    # print(np.dot(b1, b3))
           

代碼執行的結果

[1 2 3 4]
[4 3 2 1]
20
20



[[1 2]
 [3 4]]
[[5 6]
 [7 8]]
[[ 9 10]
 [11 12]
 [13 14]]
[[19 22]
 [43 50]]
           

2.multiply

同樣的看一下multiply對應源碼的注釋部分。

def multiply(x1, x2, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
    """
    multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
    
    Multiply arguments element-wise.
    
    Parameters
    ----------
    x1, x2 : array_like
        Input arrays to be multiplied. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output).
    out : ndarray, None, or tuple of ndarray and None, optional
    .....
           

明白multiply方法的關鍵就是上面的一句注釋:

Multiply arguments element-wise.
           

說人話就是:按對應的元素相乘。

def demo3():
    a1 = np.arange(1, 5)
    a2 = a1[::-1]
    print(a1)
    print(a2)
    print(np.multiply(a1, a2))
    print("\n\n")


    b1 = np.arange(1, 5).reshape(2, 2)
    b2 = np.arange(5, 9).reshape(2, 2)
    print(b1)
    print(b2)
    print(np.multiply(b1, b2))
           

運作得到結果

[1 2 3 4]
[4 3 2 1]
[4 6 6 4]



[[1 2]
 [3 4]]
[[5 6]
 [7 8]]
[[ 5 12]
 [21 32]]
           

參考對應的代碼,應該就很容易了解了。

3. *運算符

乘法運算符,最後得到的結果,跟multiply方法得到的結果是一樣的。

def demo4():
    a1 = np.arange(1, 5)
    a2 = a1[::-1]
    print(a1)
    print(a2)
    print(a1 * a2)
    print("\n\n")

    b1 = np.arange(1, 5).reshape(2, 2)
    b2 = np.arange(5, 9).reshape(2, 2)
    print(b1)
    print(b2)
    print(b1 * b2)
           

最終結果

[1 2 3 4]
[4 3 2 1]
[4 6 6 4]



[[1 2]
 [3 4]]
[[5 6]
 [7 8]]
[[ 5 12]
 [21 32]]