Bird
Raised Fist0
ML Pythonml~20 mins

Correlation analysis in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Pearson Correlation Coefficient

Which statement correctly describes the Pearson correlation coefficient between two variables?

AIt measures the difference in means between two groups.
BIt measures the probability that two variables are independent.
CIt measures the strength and direction of a linear relationship between two continuous variables.
DIt measures the total variance explained by a regression model.
Attempts:
2 left
💡 Hint

Think about what correlation tells us about two variables' relationship.

❓ Predict Output
intermediate
2:00remaining
Output of Correlation Calculation in Python

What is the output of the following Python code?

ML Python
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
correlation = np.corrcoef(x, y)[0, 1]
print(round(correlation, 2))
A-1.00
B0.80
C0.00
D1.00
Attempts:
2 left
💡 Hint

Check if y is a perfect linear multiple of x.

❓ Model Choice
advanced
2:00remaining
Choosing Correlation Method for Nonlinear Data

You have two variables with a clear nonlinear but monotonic relationship. Which correlation method is best to measure their association?

ASpearman rank correlation coefficient
BChi-square test
CMean squared error
DPearson correlation coefficient
Attempts:
2 left
💡 Hint

Consider a method that uses ranks instead of raw values.

❓ Metrics
advanced
2:00remaining
Interpreting Correlation Matrix Output

Given the correlation matrix below, how many pairs of variables have a strong positive correlation (above 0.7)?

   A    B    C    D
A  1.0  0.8  0.3  0.6
B  0.8  1.0  0.5  0.9
C  0.3  0.5  1.0  0.2
D  0.6  0.9  0.2  1.0
A3
B2
C4
D1
Attempts:
2 left
💡 Hint

Count pairs ignoring the diagonal and duplicates.

🔧 Debug
expert
2:00remaining
Debugging Correlation Calculation Code

What error does the following Python code raise?

import numpy as np
x = [1, 2, 3]
y = [4, 5]
correlation = np.corrcoef(x, y)[0, 1]
print(correlation)
AValueError due to different lengths of x and y
BIndexError due to accessing [0, 1] in a 1D array
CTypeError because lists cannot be used with np.corrcoef
DNo error, prints correlation value
Attempts:
2 left
💡 Hint

Check if input arrays must have the same length.

Practice

(1/5)
1. What does a correlation value of 0 indicate between two variables?
easy
A. There is no linear relationship between the variables
B. The variables move exactly together in the same direction
C. One variable increases while the other decreases perfectly
D. The variables have a perfect nonlinear relationship

Solution

  1. Step 1: Understand correlation meaning

    Correlation measures linear relationship strength between two variables.
  2. Step 2: Interpret zero correlation

    A correlation of 0 means no linear relationship exists between the variables.
  3. Final Answer:

    There is no linear relationship between the variables -> Option A
  4. Quick Check:

    Correlation = 0 means no linear link [OK]
Hint: Zero correlation means no linear link between variables [OK]
Common Mistakes:
  • Thinking zero means variables are unrelated in any way
  • Confusing zero correlation with negative correlation
  • Assuming zero means perfect nonlinear relationship
2. Which of the following is the correct Python syntax to calculate Pearson correlation between two lists x and y using NumPy?
easy
A. np.correlation(x, y)
B. np.corrcoef(x, y)[0, 1]
C. np.pearson(x, y)
D. np.corr(x, y)

Solution

  1. Step 1: Recall NumPy correlation function

    NumPy uses np.corrcoef() to compute correlation matrix.
  2. Step 2: Extract Pearson correlation value

    The correlation between x and y is at position [0, 1] in the matrix.
  3. Final Answer:

    np.corrcoef(x, y)[0, 1] -> Option B
  4. Quick Check:

    Use np.corrcoef and index [0,1] for Pearson [OK]
Hint: Use np.corrcoef(x, y)[0,1] to get Pearson correlation [OK]
Common Mistakes:
  • Using non-existent functions like np.correlation or np.pearson
  • Forgetting to index the correlation matrix
  • Confusing correlation with covariance functions
3. Given the code below, what is the output?
import numpy as np
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
correlation = np.corrcoef(x, y)[0, 1]
print("{:.2f}".format(round(correlation, 2)))
medium
A. 1.00
B. -1.00
C. 0.00
D. 0.50

Solution

  1. Step 1: Analyze the data relationship

    List y is exactly twice x, so they have a perfect positive linear relationship.
  2. Step 2: Calculate correlation

    Perfect positive linear relationship means correlation = 1.0.
  3. Final Answer:

    1.00 -> Option A
  4. Quick Check:

    Perfect positive linear relation = 1.00 [OK]
Hint: Perfect multiples have correlation 1.00 [OK]
Common Mistakes:
  • Thinking correlation is 0 because values differ
  • Confusing correlation with covariance magnitude
  • Rounding errors causing wrong output
4. Identify the error in the following code snippet for calculating correlation:
import numpy as np
x = [1, 2, 3]
y = [4, 5]
correlation = np.corrcoef(x, y)[0, 1]
print(correlation)
medium
A. Missing import statement for correlation function
B. np.corrcoef requires pandas Series, not lists
C. Lists x and y have different lengths causing an error
D. Indexing [0, 1] is incorrect for correlation matrix

Solution

  1. Step 1: Check input data lengths

    Lists x and y have lengths 3 and 2, which must be equal for correlation.
  2. Step 2: Understand np.corrcoef requirements

    np.corrcoef requires inputs of equal length arrays to compute correlation matrix.
  3. Final Answer:

    Lists x and y have different lengths causing an error -> Option C
  4. Quick Check:

    Input arrays must be same length [OK]
Hint: Check input lengths match before correlation [OK]
Common Mistakes:
  • Assuming np.corrcoef works with unequal length lists
  • Thinking pandas Series are required for np.corrcoef
  • Misindexing the correlation matrix
5. You have two features: hours_studied and exam_score. You calculate their correlation and get -0.85. What does this imply and what should you consider next?
hard
A. Perfect negative nonlinear relationship; use polynomial regression
B. Strong positive linear relationship; increase study hours to improve score
C. No relationship; ignore this feature in modeling
D. Strong negative linear relationship; consider if data or labels are reversed

Solution

  1. Step 1: Interpret correlation sign and magnitude

    A correlation of -0.85 means a strong negative linear relationship between the two features.
  2. Step 2: Consider real-world meaning

    Since more study usually means higher scores, a strong negative correlation suggests possible data issues or reversed labels.
  3. Final Answer:

    Strong negative linear relationship; consider if data or labels are reversed -> Option D
  4. Quick Check:

    Negative strong correlation may indicate reversed data [OK]
Hint: Strong negative correlation may mean reversed data [OK]
Common Mistakes:
  • Assuming negative correlation means no relationship
  • Ignoring domain knowledge about expected relationships
  • Confusing negative linear with nonlinear relationships
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard