Complete the code to calculate the covariance matrix of data using numpy.
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6]]) cov_matrix = np.cov([1]) print(cov_matrix)
The np.cov() function calculates the covariance matrix of the input data array. Here, we pass the variable data to it.
Complete the code to calculate covariance matrix with rows as variables.
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6]]) cov_matrix = np.cov(data, rowvar=[1]) print(cov_matrix)
By default, rowvar=True means each row represents a variable. Here, data rows are variables, so we set rowvar=True.
Fix the error in the code to get covariance matrix of columns as variables.
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6]]) cov_matrix = np.cov(data, rowvar=[1]) print(cov_matrix)
When columns represent variables, set rowvar=False so covariance is calculated between columns.
Fill both blanks to create a covariance matrix from data with variables in columns and normalize by N (not N-1).
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6]]) cov_matrix = np.cov(data, rowvar=[1], bias=[2]) print(cov_matrix)
Set rowvar=False because variables are in columns. Set bias=True to normalize by N instead of N-1.
Fill the blanks to compute covariance matrix of variables in rows, with bias normalization, and print shape.
import numpy as np data = np.array([[7, 8, 9], [10, 11, 12]]) cov_matrix = np.cov(data, rowvar=[1], bias=[2]) print(cov_matrix.shape, cov_matrix)
Set rowvar=True because variables are rows. Set bias=True to normalize by N. The shape printed shows the covariance matrix size.