Challenge - 5 Problems
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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)
Attempts:
2 left
💡 Hint
Think about how correlation measures linear relationship between two arrays.
✗ Incorrect
The arrays x and y increase together perfectly, so their correlation coefficient is 1. The matrix shows correlation of each array with itself (1) and with the other (1).
❓ data_output
intermediateCorrelation 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)
Attempts:
2 left
💡 Hint
The correlation matrix compares each variable with every other variable.
✗ Incorrect
np.corrcoef returns a square matrix with size equal to the number of input variables, so 3 variables produce a 3x3 matrix.
🔧 Debug
advancedIdentify 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)
Attempts:
2 left
💡 Hint
Think about the length of arrays passed to np.corrcoef.
✗ Incorrect
np.corrcoef requires input arrays to have the same length to compute pairwise correlation. Different lengths cause a ValueError.
🚀 Application
advancedUsing 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)
Attempts:
2 left
💡 Hint
Look for pairs with correlation close to 1.
✗ Incorrect
f1 and f3 increase together perfectly, so their correlation is 1. f1 and f2 are negatively correlated (-1).
🧠 Conceptual
expertInterpretation 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?
Attempts:
2 left
💡 Hint
Correlation values near 0 mean weak correlation; near 1 or -1 mean strong.
✗ Incorrect
0.5 is moderate positive correlation between A and B; -0.3 is weak negative correlation between A and C; 0.1 is very weak positive correlation between B and C.
