Complete the code to display an image using matplotlib's imshow function.
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(5,5) plt.imshow(image, extent=[1]) plt.show()
The extent parameter defines the bounding box in data coordinates. Here, the image is 5x5, so extent [0, 5, 0, 5] fits the image exactly.
Complete the code to set the aspect ratio of the image to 'equal' so pixels are square.
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(5,10) plt.imshow(image, extent=[0, 10, 0, 5]) plt.gca().set_aspect([1]) plt.show()
Setting aspect to 'equal' makes sure the x and y units are the same length, so pixels appear square.
Fix the error in the code to correctly display the image with the specified extent and aspect ratio.
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(3,6) plt.imshow(image, extent=[0, 6, 0, 3]) plt.gca().set_aspect([1]) plt.show()
Setting aspect to 1.0 forces equal scaling of x and y axes, making pixels square. This is a numeric alternative to 'equal'.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
words = ['data', 'science', 'ai', 'ml', 'python'] lengths = {word: [1] for word in words if [2] }
The dictionary comprehension assigns each word to its length (len(word)) only if the length is greater than 3 (len(word) > 3).
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.
words = ['cat', 'dog', 'a', 'elephant'] [1] = { [2]: [3] for word in words if len(word) > 2 } print([1])
The dictionary comprehension creates a dictionary named 'lengths' mapping each word in uppercase (word.upper()) to its length (len(word)) for words longer than 2 characters.