Challenge - 5 Problems
Correlation Matrix Visualization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of correlation matrix heatmap code
What will be the output of the following Python code that uses matplotlib and pandas to visualize a correlation matrix?
Matplotlib
import pandas as pd import matplotlib.pyplot as plt import numpy as np data = pd.DataFrame({ 'A': [1, 2, 3, 4, 5], 'B': [5, 4, 3, 2, 1], 'C': [2, 3, 2, 3, 2] }) corr = data.corr() plt.imshow(corr, cmap='coolwarm', interpolation='none') plt.colorbar() plt.xticks(range(len(corr)), corr.columns) plt.yticks(range(len(corr)), corr.columns) plt.title('Correlation Matrix Heatmap') plt.show()
Attempts:
2 left
💡 Hint
Think about what plt.imshow does with a correlation matrix and the color map used.
✗ Incorrect
The code calculates the correlation matrix and uses plt.imshow to display it as a heatmap with colors representing correlation values. The axes are labeled with column names and a colorbar shows the scale.
❓ data_output
intermediate1:30remaining
Number of cells in correlation matrix heatmap
Given a DataFrame with 4 columns, how many cells will the correlation matrix heatmap display?
Matplotlib
import pandas as pd import numpy as np data = pd.DataFrame(np.random.rand(10,4), columns=['W', 'X', 'Y', 'Z']) corr = data.corr() print(corr.shape)
Attempts:
2 left
💡 Hint
Correlation matrix is square with size equal to number of columns squared.
✗ Incorrect
The correlation matrix shape is (4,4), so it has 16 cells representing pairwise correlations.
🔧 Debug
advanced2:00remaining
Identify the error in correlation heatmap code
What error will this code raise when trying to plot a correlation matrix heatmap?
Matplotlib
import pandas as pd import matplotlib.pyplot as plt data = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) corr = data.corr() plt.imshow(corr, cmap='viridis') plt.xticks(corr.columns) plt.yticks(corr.columns) plt.show()
Attempts:
2 left
💡 Hint
Check the arguments passed to plt.xticks and plt.yticks.
✗ Incorrect
plt.xticks and plt.yticks expect positions (integers) and labels, but corr.columns is an Index object of strings without positions, causing a TypeError.
🚀 Application
advanced2:30remaining
Best way to add correlation values on heatmap cells
Which code snippet correctly adds correlation coefficient numbers on each cell of a matplotlib heatmap of a correlation matrix?
Matplotlib
import pandas as pd import matplotlib.pyplot as plt import numpy as np data = pd.DataFrame(np.random.rand(5,3), columns=['A','B','C']) corr = data.corr() fig, ax = plt.subplots() cax = ax.matshow(corr, cmap='coolwarm') fig.colorbar(cax) # Add correlation values here plt.show()
Attempts:
2 left
💡 Hint
Use np.ndenumerate to get row and column indices and values.
✗ Incorrect
Option D correctly iterates over all matrix positions and places text at (j, i) with horizontal and vertical center alignment and visible color.
🧠 Conceptual
expert1:30remaining
Interpretation of correlation matrix heatmap colors
In a correlation matrix heatmap using the 'coolwarm' colormap, what does a deep blue color in a cell represent?
Attempts:
2 left
💡 Hint
Recall the color scheme of 'coolwarm' where blue and red represent opposite ends.
✗ Incorrect
In 'coolwarm', blue colors represent negative values, so deep blue means strong negative correlation near -1.