Data Analysis with Python Projects - Medical Data Visualizer

Tell us what’s happening:

I have issues with the medical data visualizer. After running my code, 3 out of 4 tests were passed. The last one is about differing values. Here’s the error:
self.assertEqual(actual, expected, “Expected different values in heat map.”)
AssertionError: Lists differ:

How do I solve this? I have attached a link to my code:

https://freecodecam-boilerplate-1g0ekwhvb5b.ws-eu116.gitpod.io/

Thank you!

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Data Analysis with Python Projects - Medical Data Visualizer

1 Like

I can’t access the link, but it seems that the error indicates a discrepancy between the expected output and the output you’re getting. Try adding print statements in your code to help with debugging and see the actual output. Sometimes, the data types of the values can differ, which may cause issues. If you can update the link or paste your code here, I’d be happy to take a look!

You can paste your code here or I believe you need to enable sharing for your gitpod.

1 Like

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

# 1
df = pd.read_csv("medical_examination.csv")

# 2
df['overweight'] = (df['weight']/((df['height']/100)**2) >25).astype(int)

# 3
df['cholesterol'] = df['cholesterol'].apply(lambda x: 0 if x == 1 else 1)
df['gluc'] = df['gluc'].apply(lambda x: 0 if x == 1 else 1)

# 4
def draw_cat_plot():

    # 5
    df_cat = pd.melt(df, id_vars=['cardio'], value_vars=['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight'],
                     var_name='variable', value_name='value')


    # 6
    df_cat = df_cat.groupby(['cardio', 'variable', 'value']).size().reset_index(name='total')
    
    # 7
    catplot = sns.catplot(x='variable', y='total', hue='value', col='cardio', kind='bar', data=df_cat)

    # 8
    fig = catplot.fig

    # 9
    fig.savefig('catplot.png')
    return fig


# 10
def draw_heat_map():
    # Filter the data to remove outliers
    df_heat = df[
        (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()

    # Ensure that -0.0 is displayed as 0.0
    corr = corr.applymap(lambda x: 0.0 if x == -0.0 else round(x, 1))

    # Generate a mask for the upper triangle
    mask = np.triu(np.ones_like(corr, dtype=bool))

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

    # Draw the heatmap with correct formatting
    sns.heatmap(
        corr,
        mask=mask,
        annot=True,  # Add the values inside the heatmap cells
        fmt='.1f',   # Use one decimal place for formatting
        cmap='coolwarm',  # Colormap
        # vmax=0.3,  # Maximum correlation value
        center=0,  # Center the colormap around 0
        square=True,  # Keep cells square-shaped
        linewidths=0.5,  # Add lines between cells
        cbar_kws={"shrink": 0.5}  # Shrink colorbar size
    )

    # Save the figure
    fig.savefig('heatmap.png')

    return fig

I have inserted the code here. Thank you so much

Thank you.

I have pasted the code.

1 Like

I took a brief look at the code. So maybe try chaing the rounding to 2 decimal places. so

corr = corr.applymap(lambda x: 0.0 if x == -0.0 else round(x, 1))

TO

code removed

and If that doesn’t solve the problem, then try removing rounding completely then just use if the test cases need more than 2 decimal places:

code removed

Let me know if it works.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Same error, same problem: https://forum.freecodecamp.org/t/data-analysis-with-python-projects-medical-data-visualizer/714175

You should search the forum first for this error.

I would remove this since you were trying to fix the error by manipulating the data:

You are missing this instruction:

Clean the data. Filter out the following patient segments that represent incorrect data:

  • diastolic pressure is higher than systolic (Keep the correct data with (df[‘ap_lo’] <= df[‘ap_hi’]))

I see what happened, this seems to be missing from the numbered instructions…

also when going with rounding to 2 decimal places. also change the

fmt=‘.2f’

and if you’re removing rounding by just using the below

corr = df_heat.corr()

then you can select any decimal places for fmt=‘.2f’

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Thank you for the feedback! I appreciate the guidance and will focus on providing hints and suggestions.

1 Like

Thank you so much for helping to make the code readable. The code works now and passes all tests. The line I missed was part of the tasks, but not in the step-by-step instructions. I did try going through previous questions but didn’t notice that.

Once more, thank you so much!

1 Like

Thank you! I got it sorted with all your help.

1 Like