Bird
Raised Fist0
NumPydata~20 mins

np.dot() for dot product in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dot Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
1:30remaining
Output of np.dot() with 1D arrays
What is the output of this code using np.dot() on two 1D arrays?
NumPy
import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.dot(x, y)
print(result)
ATypeError
B[4 10 18]
C32
D[24 30 36]
Attempts:
2 left
๐Ÿ’ก Hint
Recall that np.dot() between two 1D arrays calculates the sum of products of corresponding elements.
โ“ Predict Output
intermediate
1:30remaining
np.dot() with 2D arrays (matrix multiplication)
What is the output of this code using np.dot() on two 2D arrays?
NumPy
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = np.dot(A, B)
print(result)
AValueError
B
[[12 16]
 [28 40]]
C
[[5 12]
 [21 32]]
D
[[19 22]
 [43 50]]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that np.dot() on 2D arrays performs matrix multiplication.
โ“ data_output
advanced
1:30remaining
Shape of np.dot() result with mixed dimensions
Given these arrays, what is the shape of the result from np.dot(A, B)?
NumPy
import numpy as np

A = np.array([[1, 2, 3], [4, 5, 6]])  # shape (2, 3)
B = np.array([7, 8, 9])                # shape (3,)
result = np.dot(A, B)
print(result.shape)
A(3,)
B(2,)
C(2, 3)
DValueError
Attempts:
2 left
๐Ÿ’ก Hint
When dot product involves a 2D array and a 1D array, the result shape depends on the 2D array's rows.
๐Ÿ”ง Debug
advanced
1:30remaining
Error raised by np.dot() with incompatible shapes
What error does this code raise when using np.dot() with incompatible shapes?
NumPy
import numpy as np

A = np.array([1, 2])  # shape (2,)
B = np.array([[3, 4], [5, 6], [7, 8]])  # shape (3, 2)
result = np.dot(A, B)
print(result)
AValueError: shapes (2,) and (3,2) not aligned: 2 (dim 0) != 3 (dim 0)
BTypeError: unsupported operand type(s) for *: 'int' and 'list'
CIndexError: index out of bounds
DNo error, output is [17 20]
Attempts:
2 left
๐Ÿ’ก Hint
Check the dimensions of arrays to see if they can be multiplied.
๐Ÿš€ Application
expert
2:00remaining
Using np.dot() to compute cosine similarity
You want to compute the cosine similarity between two vectors a and b using np.dot(). Which option correctly computes it?
NumPy
import numpy as np

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

# Choose the correct formula for cosine similarity
Anp.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
Bnp.dot(a, b) * (np.linalg.norm(a) + np.linalg.norm(b))
Cnp.dot(a, b) / (np.linalg.norm(a) + np.linalg.norm(b))
Dnp.dot(a, b) - (np.linalg.norm(a) * np.linalg.norm(b))
Attempts:
2 left
๐Ÿ’ก Hint
Cosine similarity is the dot product divided by the product of vector lengths.

Practice

(1/5)
1. What does the np.dot() function do when applied to two 1D arrays (vectors)?
easy
A. Multiplies each element of the first array by the second array as a whole.
B. Adds the two arrays element-wise.
C. Returns the cross product of the two vectors.
D. Calculates the sum of products of corresponding elements (dot product).

Solution

  1. Step 1: Understand np.dot() with 1D arrays

    When given two 1D arrays, np.dot() multiplies each pair of elements and sums them up.
  2. Step 2: Compare with other operations

    Adding element-wise or cross product are different operations; np.dot() specifically does the sum of products.
  3. Final Answer:

    Calculates the sum of products of corresponding elements (dot product). -> Option D
  4. Quick Check:

    np.dot(vector1, vector2) = sum of element-wise products [OK]
Hint: Dot product sums element-wise multiplications [OK]
Common Mistakes:
  • Confusing dot product with element-wise addition
  • Thinking np.dot() returns cross product for 1D arrays
  • Assuming np.dot() multiplies arrays element-wise without summing
2. Which of the following is the correct syntax for the np.dot() function to compute the dot product of two numpy arrays a and b?
easy
A. np.dot(a, b)
B. a.dot(b)
C. np.dot(a + b)
D. np.dot(a * b)

Solution

  1. Step 1: Recall np.dot() syntax

    The function np.dot() takes two arguments: the first and second arrays to multiply.
  2. Step 2: Check each option

    np.dot(a, b) correctly calls np.dot(a, b). a.dot(b) is valid but uses method syntax, not the function. Options A and C misuse the function by passing one argument or element-wise multiplication.
  3. Final Answer:

    np.dot(a, b) -> Option A
  4. Quick Check:

    np.dot(array1, array2) is correct syntax [OK]
Hint: Use np.dot(a, b) with two arguments [OK]
Common Mistakes:
  • Passing only one argument to np.dot()
  • Using addition or multiplication inside np.dot() incorrectly
  • Confusing method call with function call
3. What is the output of the following code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.dot(x, y)
print(result)
medium
A. [4 10 18]
B. 32
C. 15
D. Error

Solution

  1. Step 1: Calculate element-wise products

    Multiply corresponding elements: 1*4=4, 2*5=10, 3*6=18.
  2. Step 2: Sum the products

    Sum: 4 + 10 + 18 = 32.
  3. Final Answer:

    32 -> Option B
  4. Quick Check:

    Sum of products = 32 [OK]
Hint: Multiply and sum elements for dot product [OK]
Common Mistakes:
  • Printing element-wise multiplication instead of sum
  • Confusing dot product with addition
  • Expecting a vector output instead of a scalar
4. Identify the error in this code snippet:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([5, 6, 7])
result = np.dot(A, B)
print(result)
medium
A. Syntax error in np.dot() call.
B. np.dot() requires three arguments.
C. Shape mismatch: cannot multiply 2x2 matrix with length 3 vector.
D. No error; output is [17 39].

Solution

  1. Step 1: Check shapes of arrays

    Matrix A is 2x2, vector B has length 3. For dot product, inner dimensions must match.
  2. Step 2: Identify mismatch

    2 (columns of A) does not equal 3 (length of B), so multiplication is invalid.
  3. Final Answer:

    Shape mismatch: cannot multiply 2x2 matrix with length 3 vector. -> Option C
  4. Quick Check:

    Matrix columns must match vector length [OK]
Hint: Check matrix columns match vector length [OK]
Common Mistakes:
  • Ignoring shape mismatch and expecting output
  • Thinking np.dot() can auto-adjust shapes
  • Confusing syntax error with shape error
5. Given two matrices:
A = np.array([[1, 0, 2], [3, 1, 0]])
B = np.array([[2, 1], [0, 3], [1, 4]])

What is the result of np.dot(A, B)?
hard
A. [[4 9] [6 6]]
B. [[2 1 8] [6 3 0]]
C. [[2 1] [0 3] [1 4]]
D. Error due to shape mismatch

Solution

  1. Step 1: Verify shapes for multiplication

    A is 2x3, B is 3x2, so multiplication is valid (3 matches 3).
  2. Step 2: Calculate dot product manually

    Row 1 of A and column 1 of B: 1*2 + 0*0 + 2*1 = 2 + 0 + 2 = 4
    Row 1 of A and column 2 of B: 1*1 + 0*3 + 2*4 = 1 + 0 + 8 = 9
    Row 2 of A and column 1 of B: 3*2 + 1*0 + 0*1 = 6 + 0 + 0 = 6
    Row 2 of A and column 2 of B: 3*1 + 1*3 + 0*4 = 3 + 3 + 0 = 6
  3. Final Answer:

    [[4 9] [6 6]] -> Option A
  4. Quick Check:

    Matrix multiplication sums products of rows and columns [OK]
Hint: Multiply rows of A by columns of B and sum [OK]
Common Mistakes:
  • Mixing up rows and columns during multiplication
  • Expecting element-wise multiplication output
  • Ignoring shape compatibility rules