Complete the code to calculate the correlation of two arrays using numpy.
import numpy as np x = np.array([1, 2, 3]) y = np.array([0, 1, 0]) result = np.correlate(x, y, [1]) print(result)
The mode "full" returns the full discrete linear correlation, which is the default and most complete result.
Complete the code to compute the correlation of two arrays with mode 'valid'.
import numpy as np a = np.array([1, 2, 3, 4]) b = np.array([0, 1, 0]) correlation = np.correlate(a, b, mode=[1]) print(correlation)
The mode "valid" returns only those parts of the correlation that are computed without zero-padded edges.
Fix the error in the code to correctly compute correlation with np.correlate.
import numpy as np x = np.array([1, 2, 3]) y = np.array([0, 1, 0]) result = np.correlate(x, y, [1]) print(result)
The mode argument must be a string, so it needs quotes around it.
Fill both blanks to create a dictionary comprehension that maps each number to its correlation with a fixed array.
import numpy as np fixed = np.array([1, 0, -1]) numbers = [1, 2, 3] correlations = {num: np.correlate(np.array([num, num, num]), fixed, mode=[1])[[2]] for num in numbers} print(correlations)
Using mode "full" gives the full correlation array, and index 0 accesses the first correlation value.
Fill all three blanks to create a dictionary comprehension that maps each word to the correlation of its length with a fixed array.
import numpy as np fixed = np.array([1, 2, 3]) words = ["cat", "dog", "bird"] correlation_dict = {word: np.correlate(np.array([len(word)] * 3), fixed, mode=[1])[[2]] for word in words if len(word) [3] 3} print(correlation_dict)
Use mode "full" for full correlation, index 0 for the first value, and filter words with length greater than 3.