0
0
NumPydata~20 mins

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

Choose your learning style9 modes available
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.