0
0
NumPydata~10 mins

Covariance with np.cov() in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the covariance matrix of data using numpy.

NumPy
import numpy as np

data = np.array([[1, 2, 3], [4, 5, 6]])
cov_matrix = np.cov([1])
print(cov_matrix)
Drag options to blanks, or click blank then click option'
Anp.mean
Bnp.array
Cnp.cov
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function np.cov instead of the data array.
Passing np.mean which calculates mean, not covariance.
2fill in blank
medium

Complete the code to calculate covariance matrix with rows as variables.

NumPy
import numpy as np

data = np.array([[1, 2, 3], [4, 5, 6]])
cov_matrix = np.cov(data, rowvar=[1])
print(cov_matrix)
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting rowvar=False when rows are variables.
Passing None which is invalid for rowvar.
3fill in blank
hard

Fix the error in the code to get covariance matrix of columns as variables.

NumPy
import numpy as np

data = np.array([[1, 2, 3], [4, 5, 6]])
cov_matrix = np.cov(data, rowvar=[1])
print(cov_matrix)
Drag options to blanks, or click blank then click option'
ATrue
BFalse
C1
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using rowvar=True when columns are variables.
Passing invalid values like None or 1.
4fill in blank
hard

Fill both blanks to create a covariance matrix from data with variables in columns and normalize by N (not N-1).

NumPy
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)
Drag options to blanks, or click blank then click option'
AFalse
BTrue
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using rowvar=True when variables are columns.
Setting bias=False which normalizes by N-1.
5fill in blank
hard

Fill the blanks to compute covariance matrix of variables in rows, with bias normalization, and print shape.

NumPy
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)
Drag options to blanks, or click blank then click option'
ATrue
BFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using rowvar=False when variables are rows.
Setting bias=False which normalizes by N-1.