Medical Data Visualizer Help-

**Tell us what’s happening:
I have created both Catplot and Heatmap but getting errors in both the charts. Here are the problems. I’m beyond frustrated in figuring out the cause of the errors. Any help with logic and code snippets is very helpful.

  1. Catplot() that I created(‘catplot.png’) and the (Figure_1) in the example match. Both graphs are same but why am I getting the ‘number of bars’ not equal error?

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

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-1/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’

  1. Again for Catplot I have defined the axes labels. Why is it throwing an error? Ugghh!!!
    ‘’’
    fig.set(xlabel=‘varaible’,ylabel=‘total’)
    fig.set_xticklabels([‘active’,‘alco’,‘cholesterol’,‘gluc’,‘overweight’,‘smoke’])
    ‘’’

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

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

  1. Finally, I somehow figured out creating Heatmap() but there is difference in actual values and expected values.
    Can someone point out the mistakes?

======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-1/test_module.py”, line 47, in test_heat_map_values
self.assertEqual(actual, expected, “Expected differnt values in heat map.”)
AssertionError: Lists differ: [‘0.0[36 chars]5’, ‘-0.0’, ‘0.1’, ‘0.1’, ‘0.2’, ‘-0.0’, ‘0.0’[572 chars]0.1’] != [‘0.0[36 chars]5’, ‘0.0’, ‘0.1’, ‘0.1’, ‘0.3’, ‘0.0’, ‘0.0’, [571 chars]0.1’]

First differing element 6:
‘-0.0’
‘0.0’

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


Ran 4 tests in 8.561s

FAILED (failures=1, errors=2)

**

**Here is the link to the code that I tried so far:

Import data

df = pd.read_csv(‘medical_examination.csv’)

Add ‘overweight’ column

df[‘height_in_meters’]=round(df[‘height’]/100,ndigits=1)
df[‘BMI’]=round(df[‘weight’]/(df[‘height_in_meters’]**2),ndigits=1)

df[‘overweight’] = df[‘BMI’].apply(lambda x:‘1’ if x>25 else ‘0’)
df[‘overweight’]=df[‘overweight’].astype(int)

Normalize data by making 0 always good and 1 always bad. If the value of ‘cholestorol’ 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 x:0 if x<=1 else 1)
df[‘gluc’]=df[‘gluc’].apply(lambda x:0 if x<=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’.
df_cat = df.melt(id_vars=[‘cardio’],value_vars=[‘cholesterol’,‘gluc’,‘smoke’,‘alco’,‘active’,‘overweight’])

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

# Draw the catplot with 'sns.catplot()'
fig=sns.catplot(x='variable',y='total',hue='value',col='cardio',data=df_cat,kind='bar')
fig.set(xlabel='varaible',ylabel='total')
fig.set_xticklabels(['active','alco','cholesterol','gluc','overweight','smoke'])


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

Draw Heat Map

def draw_heat_map():
# Clean the data
# Clean Pressure values
(df[‘ap_lo’] <= df[‘ap_hi’]).value_counts()
df[df[‘ap_lo’] > df[‘ap_hi’]]
pressure=df[df[‘ap_lo’] > df[‘ap_hi’]].index
df.drop(pressure,inplace=True)

# Clean Height <2.5
df[df['height'] >= df['height'].quantile(0.025)]
df[df['height'] < df['height'].quantile(0.025)]
height=df[df['height'] < df['height'].quantile(0.025)].index
df.drop(height,inplace=True)

# Clean Height>97.5
(df['height']<=df['height'].quantile(q=.975)).value_counts()
height=df[df['height']>df['height'].quantile(q=.975)].index
df.drop(height,inplace=True)

# Clean Weight<2.5
(df['weight']>=df['weight'].quantile(q=.025)).value_counts()
weight=df[df['weight']<df['weight'].quantile(q=.025)].index
df.drop(weight,inplace=True)

# Clean Weight >97.5
df[df['weight']<=df['weight'].quantile(q=.975)]
(df['weight']<=df['weight'].quantile(q=.975)).value_counts()
df[df['weight']>df['weight'].quantile(q=.975)]
weight=df[df['weight']>df['weight'].quantile(q=.975)].index
df.drop(weight,inplace=True)

df_heat = pd.DataFrame(df,columns=['id','age','gender','height','weight','ap_hi','ap_lo','cholesterol','gluc','smoke','alco','active','cardio','overweight'])

# Calculate the correlation matrix
df_heat.corr()
corr = round(df_heat.corr(),ndigits=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=(11,9))


# Draw the heatmap with 'sns.heatmap()'
fig=sns.heatmap(corr, annot=True,fmt=".1f",mask=mask,vmax=.3,center=0,square=True,linewidths=.5)
ax.set_xticklabels(['id','age','gender','height','weight','ap_hi','ap_lo','cholesterol','gluc','smoke','alco','active','cardio','overweight'])
fig=fig.get_figure()




# 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/87.0.4280.88 Safari/537.36.

Challenge: Medical Data Visualizer

Link to the challenge:

1 and 2 Solved!
here is the help/solution link posted in another post.

Need help with Heatmap.