Hello.
I am doing the MDV exercise and when I run it, it says it failed the last test, with the heatmap. It says the values differ. I read some posts in the forum and tried, but none work. I think it’s a problem with the test as I copied and printed the heatmap in Jupyter Notebook and is exactly the same as in the sample. Can you have a look and tell me what’s wrong? Thanks.
The test:
class HeatMapTestCase(unittest.TestCase):
def setUp(self):
self.fig = medical_data_visualizer.draw_heat_map()
self.ax = self.fig.axes[0]
def test_heat_map_labels(self):
actual = []
for label in self.ax.get_xticklabels():
actual.append(label.get_text())
expected = ['id', 'age', 'gender', 'height', 'weight', 'ap_hi', 'ap_lo', 'cholesterol', 'gluc', 'smoke', 'alco', 'active', 'cardio', 'overweight']
self.assertEqual(actual, expected, "Expected bar plot legend labels to be months of the year.")
def test_heat_map_values(self):
actual = [text.get_text() for text in self.ax.get_default_bbox_extra_artists() if isinstance(text, mpl.text.Text)]
print(actual)
expected = ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.1', '0.0', '0.2', '0.1', '0.0', '0.1', '-0.0', '-0.1', '0.1', '0.0', '0.2', '0.0', '0.1', '-0.0', '-0.0', '0.1', '0.0', '0.1', '0.4', '-0.0', '-0.0', '0.3', '0.2', '0.1', '-0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.2', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.0', '-0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '-0.0', '0.2', '0.1', '0.3', '0.2', '0.1', '-0.0', '-0.0', '-0.0', '-0.0', '0.1', '-0.1', '-0.1', '0.7', '0.0', '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1']
self.assertEqual(actual, expected, "Expected different values in heat map.")
My code:
def draw_heat_map():
# Clean the data
df_heat = df[ (df[‘ap_lo’]<= df[‘ap_hi’]) &
(df[‘height’]>=df[‘height’].quantile(0.025)) &
(df[‘height’]<=df[‘height’].quantile(0.0975)) &
(df[‘weight’]>=df[‘weight’].quantile(0.025)) &
(df[‘weight’]<=df[‘weight’].quantile(0.0975))]
# Calculate the correlation matrix
corr = df_heat.corr(method = 'pearson')
# Generate a mask for the upper triangle
mask = np.triu(corr)
# Set up the matplotlib figure
fig,ax = plt.subplots(figsize = (12,12))
# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr, linewidths = 1, annot = True, square = True, mask = mask, fmt=".1f", center = 0.08, cbar_kws={'shrink':0.5})
# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56
Challenge: Medical Data Visualizer
Link to the challenge: