0
0
NumPydata~10 mins

Correlation coefficient with np.corrcoef() 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 correlation coefficient matrix of two arrays using numpy.

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)
Drag options to blanks, or click blank then click option'
Acov
Bcorrelate
Ccorrcoef
Dcorr
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.correlate() which computes cross-correlation, not correlation coefficient.
Using np.cov() which computes covariance, not correlation coefficient.
2fill in blank
medium

Complete the code to extract the correlation coefficient between x and y from the correlation matrix.

NumPy
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)
Drag options to blanks, or click blank then click option'
A[]
B()
C{}
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses which is for function calls.
Using curly braces which are for dictionaries or sets.
3fill in blank
hard

Fix the error in the code to correctly calculate the correlation coefficient between two lists.

NumPy
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])
Drag options to blanks, or click blank then click option'
Ax
Bnp.array(x)
Clist(x)
Dnp.tolist(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Python lists directly to np.corrcoef which expects numpy arrays.
Using incorrect conversion functions like np.tolist().
4fill in blank
hard

Fill both blanks to create a dictionary of correlation coefficients for words with length greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 2 characters.

NumPy
words = ['cat', 'dog', 'a', 'bird']
result = [1]: [2] for w in words if len(w) [3] 2}
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Bw
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase keys instead of uppercase.
Using wrong comparison operator or filtering condition.