Heatmap Error in Medical Data Visualizer

Hi,

I’m having problems with the heatmap in my medical data visualizer.
Seems like my code is generating three empty cells at the end which is not present in the rest case. (I saw that the reverse was a problem before and I tried changing matplotlib versions and everything but they aren’t working.)

Any help would be appreciated!

Here’s my code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Import data
df = pd.read_csv("medical_examination.csv")

# Add 'overweight' column
bmi = df.weight/((df.height/100) ** 2)

df["overweight"] = bmi.apply(lambda classifier:1 if classifier > 25 else 0)

# Normalize data by making 0 always good and 1 always bad. If the value of 'cholesterol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df.cholesterol = df.cholesterol.apply(lambda classifier:0 if classifier == 1 else 1)

df.gluc = df.gluc.apply(lambda classifier:0 if classifier == 1 else 1)

# Draw Categorical Plot
def draw_cat_plot():
    # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
    cols = ["cholesterol", "gluc" , "smoke" , "alco", "active", "overweight"]
    df_cat = pd.melt(df, id_vars = ["cardio"], value_vars = cols)


    # Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the columns for the catplot to work correctly.
    col_groups = ["cardio", "variable", "value"]
    df_cat = df_cat.reset_index().groupby(col_groups).agg("count").rename(columns = {"index": "total"}).reset_index()
    

    # Draw the catplot with 'sns.catplot()'
    


    # Get the figure for the output
    fig = sns.catplot(
      data = df_cat,
      x = "variable",
      y = "total",
      col = "cardio",
      hue = "value",
      kind = "bar"
    ).fig


    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig


# Draw Heat Map
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.975))
                 & (df.weight >= df.weight.quantile(0.025)) 
                 & (df.weight <= df.weight.quantile(0.975))
                ]

    # Calculate the correlation matrix
    corr = df_heat.corr()

    # Generate a mask for the upper triangle
    mask = np.triu(corr)



    # Set up the matplotlib figure
    fig, ax = plt.subplots(figsize = (50, 20))

    # Draw the heatmap with 'sns.heatmap()'
    sns.heatmap(corr, mask = mask,
                annot = True, square = True, 
                linewidths = 1, fmt = ".1f",
                center = 0)



    # Do not modify the next two lines
    fig.savefig('heatmap.png')
    return fig

And here’s the error:

../home/runner/shirshodasgupta-medical-data-visualizer/venv/lib/python3.8/site-packages/seaborn/matrix.py:268: PendingDeprecationWarning: 
The label function will be deprecated in a future version. Use Tick.label1 instead.
  fontsize = tick.label.get_size()
.['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', '', '', '']
F
======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/shirshodasgupta-medical-data-visualizer/test_module.py", line 47, in test_heat_map_values
    self.assertEqual(actual, expected, "Expected different values in heat map.")
AssertionError: Lists differ: ['0.0[607 chars] '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1', '', '', ''] != ['0.0[607 chars] '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1']

First list contains 3 additional elements.
First extra element 91:
''

Diff is 989 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map.

----------------------------------------------------------------------
Ran 4 tests in 13.662s

FAILED (failures=1)
 

I tested your code and it is the (a? There may be more than one…) version problem (tests pass locally with up to date dependencies). Remove the installed dependencies in replit, fix the pyproject.toml file, and reinstall as in the other posts to fix the dependencies for the data analysis projects and re-run the tests.

Post a link to your repl if this doesn’t work.

1 Like

It worked! Thanks and a Happy New Year!

Hi Jeremy!
I have the same problem but I don’t know how to implement your solution.
I’ve tried to remove the package but doesn’t work.
Can you help me with this?
Thanks!