Complete the code to calculate the correlation coefficient matrix of two arrays using numpy.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([5, 4, 3, 2, 1]) result = np.[1](x, y) print(result)
The function np.corrcoef() calculates the correlation coefficient matrix between arrays.
Complete the code to extract the correlation coefficient between x and y from the correlation matrix.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([5, 4, 3, 2, 1]) corr_matrix = np.corrcoef(x, y) corr_xy = corr_matrix[1][0, 1] print(corr_xy)
The correlation matrix is a 2D numpy array, so we use square brackets [] to index elements.
Fix the error in the code to correctly calculate the correlation coefficient between two lists.
import numpy as np x = [1, 2, 3, 4, 5] y = [5, 4, 3, 2, 1] corr = np.corrcoef([1], y) print(corr[0, 1])
np.corrcoef requires numpy arrays, so convert the list x to a numpy array first.
Fill both blanks to create a dictionary of correlation coefficients for words with length greater than 3.
words = ['data', 'science', 'ai', 'ml', 'python'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
We want the length of each word as the value, and only include words with length greater than 3.
Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 2 characters.
words = ['cat', 'dog', 'a', 'bird'] result = [1]: [2] for w in words if len(w) [3] 2} print(result)
The keys are uppercase words, values are original words, and we filter words longer than 2 characters.