Complete the code to compute the sum of elements in array using np.einsum.
import numpy as np arr = np.array([1, 2, 3, 4]) sum_val = np.einsum('[1]', arr) print(sum_val)
The subscript 'i->' tells np.einsum to sum over the index 'i', resulting in the sum of all elements.
Complete the code to compute the dot product of two vectors using np.einsum.
import numpy as np v1 = np.array([1, 2, 3]) v2 = np.array([4, 5, 6]) dot_product = np.einsum('[1]', v1, v2) print(dot_product)
The subscript 'i,i->' multiplies elements of v1 and v2 at the same index and sums the result, computing the dot product.
Fix the error in the code to perform matrix multiplication using np.einsum.
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) result = np.einsum('[1]', A, B) print(result)
The subscript 'ij,jk->ik' correctly performs matrix multiplication by summing over the shared index 'j'.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if [2]
We use len(word) to get the length and filter words with length greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = [1]: [2] for word in words if [3]
The key is the uppercase word, the value is its length, and the condition filters words longer than 3 characters.