0
0
NumPydata~20 mins

Correlation coefficient with np.corrcoef() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.corrcoef() with simple arrays
What is the output of this code snippet?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.corrcoef(x, y)
print(result)
NumPy
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.corrcoef(x, y)
print(result)
A
[[1. 1.]
 [1. 1.]]
B
[[1. 0.]
 [0. 1.]]
C
[[0. 1.]
 [1. 0.]]
D
[[1. -1.]
 [-1. 1.]]
Attempts:
2 left
💡 Hint
Think about how correlation measures linear relationship between two arrays.
data_output
intermediate
2:00remaining
Correlation matrix shape for multiple variables
Given three variables stored as arrays, what is the shape of the correlation matrix returned by np.corrcoef() when called as np.corrcoef(a, b, c)?
NumPy
import numpy as np
a = np.array([1, 2, 3])
b = np.array([3, 2, 1])
c = np.array([2, 2, 2])
result = np.corrcoef(a, b, c)
print(result.shape)
A(3,)
B(3, 3)
C(1, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
The correlation matrix compares each variable with every other variable.
🔧 Debug
advanced
2:00remaining
Identify the error in np.corrcoef usage
What error will this code raise?
import numpy as np
x = [1, 2, 3]
y = [4, 5]
np.corrcoef(x, y)
NumPy
import numpy as np
x = [1, 2, 3]
y = [4, 5]
np.corrcoef(x, y)
ATypeError: unsupported operand type(s) for -: 'list' and 'list'
BNo error, returns correlation matrix
CIndexError: index out of range
DValueError: all the input arrays must have same length
Attempts:
2 left
💡 Hint
Think about the length of arrays passed to np.corrcoef.
🚀 Application
advanced
2:00remaining
Using np.corrcoef to find strongest correlation
You have three arrays representing different features. Which pair has the strongest positive correlation?
import numpy as np
f1 = np.array([1, 2, 3, 4])
f2 = np.array([4, 3, 2, 1])
f3 = np.array([2, 4, 6, 8])
cor_matrix = np.corrcoef(f1, f2, f3)
print(cor_matrix)
NumPy
import numpy as np
f1 = np.array([1, 2, 3, 4])
f2 = np.array([4, 3, 2, 1])
f3 = np.array([2, 4, 6, 8])
cor_matrix = np.corrcoef(f1, f2, f3)
print(cor_matrix)
Af1 and f3
BAll pairs have equal correlation
Cf2 and f3
Df1 and f2
Attempts:
2 left
💡 Hint
Look for pairs with correlation close to 1.
🧠 Conceptual
expert
2:00remaining
Interpretation of np.corrcoef output matrix
Given the correlation matrix below from np.corrcoef for variables A, B, and C:
[[ 1.   0.5 -0.3]
 [ 0.5  1.   0.1]
 [-0.3  0.1  1. ]]
Which statement is true?
AVariable B and C have a strong negative correlation.
BVariable A and C have a strong positive correlation.
CVariable A and B have a moderate positive correlation, and A and C have a weak negative correlation.
DAll variables are uncorrelated.
Attempts:
2 left
💡 Hint
Correlation values near 0 mean weak correlation; near 1 or -1 mean strong.