[Python] How to compute elementwise dot product of two 2D NumPy arrays [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    This script:

    import numpy as np

    a = np.array([[0, 1], [1, 1], [1, 0]])
    b = np.array([[1, 0], [1, 1], [1, -1]])
    dot = a[:, 0]*b[:, 0] + a[:, 1]*b[:, 1]
    print(dot)


    produces elementwise dot product of a and b:

    [0 2 1]


    There must be a NumPy magic, which can do the same with one function call. I tried np.dot, np.tensordot and np.inner with no success. Any suggestions?

    Continue reading...

Compartilhe esta Página