0
0
Data Analysis Pythondata~20 mins

Heatmaps for correlation in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heatmap Correlation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of correlation heatmap data
What is the shape of the correlation matrix produced by this code?
Data Analysis Python
import pandas as pd
import numpy as np

data = pd.DataFrame(np.random.rand(10, 5), columns=list('ABCDE'))
corr_matrix = data.corr()
print(corr_matrix.shape)
A(5, 10)
B(10, 5)
C(5, 5)
D(10, 10)
Attempts:
2 left
💡 Hint
The correlation matrix compares each column with every other column.
data_output
intermediate
2:00remaining
Correlation values from heatmap data
What is the correlation value between columns 'A' and 'B' in this dataset?
Data Analysis Python
import pandas as pd
import numpy as np
np.random.seed(0)
data = pd.DataFrame({
    'A': np.arange(5),
    'B': np.arange(5) * 2,
    'C': np.random.rand(5)
})
corr = data.corr()
print(round(corr.loc['A', 'B'], 2))
A1.00
B0.50
C-1.00
D0.00
Attempts:
2 left
💡 Hint
Columns 'A' and 'B' have a perfect linear relationship.
visualization
advanced
2:00remaining
Identify the correct heatmap color scale
Which heatmap color scale correctly represents correlations from -1 (blue) to 1 (red) with 0 as white?
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(1)
data = pd.DataFrame(np.random.randn(10, 4), columns=list('WXYZ'))
corr = data.corr()

sns.heatmap(corr, cmap='coolwarm')
plt.show()
A'viridis'
B'Greens'
C'Blues'
D'coolwarm'
Attempts:
2 left
💡 Hint
The color scale should show blue for negative, white for zero, and red for positive.
🔧 Debug
advanced
2:00remaining
Error in heatmap correlation calculation
What error does this code raise when trying to plot a heatmap of correlations?
Data Analysis Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.DataFrame({'A': [1, 2, 3], 'B': ['x', 'y', 'z']})
corr = data.corr()
sns.heatmap(corr)
plt.show()
ANo error, heatmap displays correctly
BKeyError: 'B'
CValueError: No numeric types to aggregate
DTypeError: unsupported operand type(s) for -: 'str' and 'int'
Attempts:
2 left
💡 Hint
data.corr() ignores non-numeric columns.
🚀 Application
expert
3:00remaining
Interpreting correlation heatmap for feature selection
Given this correlation heatmap matrix, which feature should be dropped to reduce multicollinearity?
Data Analysis Python
import pandas as pd
import numpy as np

np.random.seed(42)
data = pd.DataFrame({
    'X1': np.random.rand(100),
    'X2': np.random.rand(100),
    'X3': np.random.rand(100) * 0.5 + np.random.rand(100) * 0.5,
    'X4': np.random.rand(100) * 0.9 + np.random.rand(100) * 0.1
})
data['X3'] = data['X1'] * 0.95 + np.random.rand(100) * 0.05
corr = data.corr()
print(corr.round(2))
ADrop X2 because it has low correlation with others
BDrop X3 because it is highly correlated with X1
CDrop X4 because it has the highest variance
DDrop X1 because it has the lowest mean
Attempts:
2 left
💡 Hint
Look for pairs with correlation close to 1 or -1.