0
0
NumPydata~10 mins

np.dot() for dot product in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the dot product of two 1D arrays using numpy.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.[1](arr1, arr2)
print(result)
Drag options to blanks, or click blank then click option'
Adot
Bcross
Cmultiply
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.multiply which returns element-wise multiplication, not the dot product.
Using np.cross which computes the cross product, not dot product.
2fill in blank
medium

Complete the code to calculate the dot product of two 2D arrays using numpy.

NumPy
import numpy as np

mat1 = np.array([[1, 2], [3, 4]])
mat2 = np.array([[5, 6], [7, 8]])
result = np.[1](mat1, mat2)
print(result)
Drag options to blanks, or click blank then click option'
Amultiply
Bdot
Cadd
Dtranspose
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.multiply which multiplies element-wise, not matrix multiplication.
Using np.transpose which just flips the matrix, not multiply.
3fill in blank
hard

Fix the error in the code to correctly compute the dot product of two arrays.

NumPy
import numpy as np

vec1 = np.array([1, 2, 3])
vec2 = np.array([4, 5, 6])
result = np.dot(vec1, [1])
print(result)
Drag options to blanks, or click blank then click option'
Anp.array
Bvec1
Cvec2
Dvec3
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the first vector twice instead of the second.
Passing a non-array object.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores the dot product of each vector with itself for vectors longer than 2 elements.

NumPy
import numpy as np

vectors = {'a': [1, 2, 3], 'b': [4, 5], 'c': [6, 7, 8, 9]}
dot_products = {key: np.[1](np.array(val), np.array(val)) for key, val in vectors.items() if len(val) [2] 2}
print(dot_products)
Drag options to blanks, or click blank then click option'
Adot
B>
C<=
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of np.dot for dot product.
Using wrong comparison operator like <= instead of >.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores the dot product of vectors with length less than 4, using uppercase keys.

NumPy
import numpy as np

vectors = {'x': [1, 0, 0], 'y': [0, 1, 0, 1], 'z': [1, 1]}
dot_dict = {key.[1](): np.[2](np.array(val), np.array(val)) for key, val in vectors.items() if len(val) [3] 4}
print(dot_dict)
Drag options to blanks, or click blank then click option'
Alower
Bdot
C<
Dupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper() for keys.
Using wrong comparison operator like > instead of <.