Medical Data Visualizer error in test_module.py (It says)

Tell us what’s happening:
So yeah, I did it. I created my own plot and heatmap in the Medical Data Visualizer Challenge. I know the code is not pretty but I did it at the end lmao.

But I keep getting Failed (error = 2) and it says it’s an error in test_module.py
How does one get through this problem? If you can, tell me what to fix and not how to fix it. Because I want to learn BABY!

Anyway, thank you before hand :smiley:

Your code so far

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

df[‘overweight’] = “”

mask0 = df.weight / (df.height/100)**2 <= 25
mask1 = df.weight / (df.height/100)**2 > 25

df.overweight[mask0] = 0
df.overweight[mask1] = 1
df = df.astype({“overweight”: int})

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.

maskC0 = df.cholesterol <= 1
maskC1 = df.cholesterol > 1
df.cholesterol[maskC0] = 0
df.cholesterol[maskC1] = 1

maskC0 = df.gluc <= 1
maskC1 = df.gluc > 1

df.gluc[maskC0] = 0
df.gluc[maskC1] = 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’.
df_cat = pd.melt(df, id_vars=‘cardio’,value_vars=[‘active’,‘alco’,‘cholesterol’,‘gluc’,‘overweight’,‘smoke’])

# 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.
df_cat = pd.melt(df, id_vars='cardio',value_vars=['active','alco','cholesterol','gluc','overweight','smoke'])

# Draw the catplot with 'sns.catplot()'
fig = sns.catplot(x="variable", hue="value", col="cardio", data=df_cat, kind="count")
fig.set_axis_labels(y_var="Total")

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

Draw Heat Map

def draw_heat_map():
# Clean the data
df_mask = (df.ap_hi >= df.ap_lo) & (df.height.quantile(0.025) <= df.height) & (df.weight.quantile(0.025) <= df.weight) & (df.weight.quantile(0.975) >= df.weight) & (df.height.quantile(0.975) >= df.height)

df_heat = df[df_mask]

# Calculate the correlation matrix
corr = df_heat.corr(method="pearson")

# Generate a mask for the upper triangle
mask = np.zeros_like(corr)
Triangle = np.triu_indices_from(mask)
mask[Triangle] = True

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

# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr,mask=mask,annot=True,fmt='.1f')

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

The Error

ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-5/test_module.py”, line 26, in test_bar_plot_number_of_bars
actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rectangle)])
AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_children’

======================================================================
ERROR: test_line_plot_labels (test_module.CatPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-5/test_module.py”, line 13, in test_line_plot_labels
actual = self.ax.get_xlabel()
AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_xlabel’

Your browser information:

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

Challenge: Medical Data Visualizer

Link to the challenge:

Use code blocks or post a link to a repl (repl.it) or something. I’m getting too lazy to reformat.

Those errors indicate you are not returning the correct object, so inspect fig before you return it. It should be matplotlib Figure object but the one for the catplot is a seaborn FacetGrid object since that’s what sns.catplot() returns. The heatmap one is either not set or the wrong object; it’s difficult to tell without running the code.

There are plenty of posts about this error in the forums.

1 Like

Ohh shieee you’re right! After I put .fig at the end to make it a FacetGrid, it works like a charm!

And the second error is because I didn’t read the instruction clearly (I work around it lmao)

Anyway, thank you kind stranger! Sorry I didn’t put the replit link and or the code block, it’s my first time posting in a code forum and I don’t even know what a code block is :sweat_smile:

Cheers :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.