Complete the code to add a new axis to the array using np.expand_dims.
import numpy as np arr = np.array([1, 2, 3]) new_arr = np.expand_dims(arr, axis=[1]) print(new_arr.shape)
Using axis=1 adds a new axis as the second dimension, changing shape from (3,) to (3, 1).
Complete the code to remove single-dimensional entries from the shape using np.squeeze.
import numpy as np arr = np.array([[[1], [2], [3]]]) squeezed_arr = np.squeeze(arr, axis=[1]) print(squeezed_arr.shape)
Axis 0 has size 1, so squeezing it removes that dimension, changing shape from (1, 3, 1) to (3, 1).
Fix the error in the code by choosing the correct axis to expand.
import numpy as np arr = np.array([[1, 2], [3, 4]]) new_arr = np.expand_dims(arr, axis=[1]) print(new_arr.shape)
For a 2D array with shape (2, 2), axis=2 adds a new axis at the end, resulting in shape (2, 2, 1). Axis 3 is invalid.
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]
The dictionary maps each word to its length using len(word). The condition len(word) > 3 filters words longer than 3 letters.
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 dictionary keys are uppercase words using word.upper(). Values are lengths with len(word). The condition filters words longer than 3.