Challenge - 5 Problems
Dot Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput 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)
Attempts:
2 left
๐ก Hint
Recall that np.dot() between two 1D arrays calculates the sum of products of corresponding elements.
โ Incorrect
The dot product of two 1D arrays is the sum of the products of their elements: (1*4) + (2*5) + (3*6) = 4 + 10 + 18 = 32.
โ Predict Output
intermediatenp.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)
Attempts:
2 left
๐ก Hint
Remember that np.dot() on 2D arrays performs matrix multiplication.
โ Incorrect
Matrix multiplication result is calculated as:
First row, first column: 1*5 + 2*7 = 19
First row, second column: 1*6 + 2*8 = 22
Second row, first column: 3*5 + 4*7 = 43
Second row, second column: 3*6 + 4*8 = 50
โ data_output
advancedShape 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)
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.
โ Incorrect
Dot product of (2,3) and (3,) arrays results in a 1D array with length 2, so shape is (2,).
๐ง Debug
advancedError 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)
Attempts:
2 left
๐ก Hint
Check the dimensions of arrays to see if they can be multiplied.
โ Incorrect
np.dot() requires the inner dimensions to match. Here, (2,) and (3,2) do not align because 2 != 3.
๐ Application
expertUsing 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
Attempts:
2 left
๐ก Hint
Cosine similarity is the dot product divided by the product of vector lengths.
โ Incorrect
Cosine similarity formula: (a ยท b) / (||a|| * ||b||), where ||a|| is the length (norm) of vector a.
