Instagram
youtube
Facebook
Twitter

Compute Dot Product of 2D and 1D NumPy Arrays

A Compute Dot Product of 2D and 1D NumPy Arrays?
Code Explanation:

Library Import: numpy is imported as np to perform mathematical operations.
Create 2D Array: a is defined as a 2D array with shape (2, 2).
Create 1D Array: b is defined as a 1D array with 2 elements.
Dot Product: np.dot(a, b) computes the dot product between each row of a and array b.
Mathematical Operation: Each row of a is multiplied element-wise with b, then summed:

  • Row 1: (1×5 + 2×6)
     
  • Row 2: (3×5 + 4×6)
    Store Result: The result of the dot product is stored in result.
    Display Output: print(result) displays the resulting 1D array.

 

Program:

import numpy as np

a = np.array([[1, 2], [3, 4]])

b = np.array([5, 6])

result = np.dot(a, b)

print(result)