Tell us what’s happening:
In this project of medical data visualizeer I’m meeting lots of errors; I don’t know what to do because I think my code is correct, but the problems are with the tests_module. I understood also that there are problems with the values expected in the correlation matrix and in fact comparing my corr plot and the one provided in figure 2 there are some differences, but I cannot understand what I did wrong. Also I’m sure my first image it’s equal to the figure 1 they provided but when I try to modify the test module, I get that there are 70 bars instead of 13.
the errors are:
ERROR: test_bar_plot_number_of_bars (__main__.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/boilerplate-medical-data-visualizer/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 (__main__.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/boilerplate-medical-data-visualizer/test_module.py", line 13, in test_line_plot_labels
actual = self.ax.get_xlabel()
AttributeError: 'numpy.ndarray' object has no attribute 'get_xlabel'
======================================================================
FAIL: test_heat_map_values (__main__.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/boilerplate-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[14 chars]0', '-0.0', '-0.1', '0.5', '-0.0', '0.1', '0.2[594 chars]0.2'] != ['0.0[14 chars]0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1',[593 chars]0.1']
First differing element 3:
'-0.0'
'0.0'
Diff is 1287 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map.
----------------------------------------------------------------------
Ran 4 tests in 2.525s
FAILED (failures=1, errors=2)
Your code so far
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
weight= df['weight']
height= df['height']/100 #to change in meters from cm
BMI= weight/(height)**2
df['overweight'] = np.where(BMI>25 , 1 , 0)
print(df.head(10))
# 3
df['cholesterol'] = np.where(df['cholesterol'] > 1, 1, 0)
df['gluc'] = np.where(df['gluc'] > 1, 1, 0)
print(df.head(10))
# 4
def draw_cat_plot():
# 5.
categorical_features = ['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight']
df_cat = pd.melt(df, id_vars=['cardio'], value_vars=categorical_features,
var_name='variable', value_name='value')
# 7
df_cat_counts = df_cat.groupby(['cardio', 'variable', 'value']).size().reset_index(name='total')
# 8
fig = sns.catplot(data=df_cat_counts, x='variable', y='total', hue='value', col='cardio',
kind='bar', height=4, aspect=1.5, legend_out=True)
fig.set_axis_labels("Variable", "Total")
fig.set_titles("Cardio = {col_name}")
fig.add_legend(title='Value')
# 9
fig.savefig('catplot.png')
return fig
draw_cat_plot()
# 10
def draw_heat_map():
# 11
df_heat = df.copy()
df_heat_ = df_heat[
(df_heat['height'] >= df_heat['height'].quantile(0.025)) &
(df_heat['height'] <= df_heat['height'].quantile(0.975)) &
(df_heat['weight'] >= df_heat['weight'].quantile(0.025)) &
(df_heat['weight'] <= df_heat['weight'].quantile(0.975))&
(df_heat['ap_lo'] <= df_heat['ap_hi'])
]
# 12
corr = df_heat.corr()
mask = np.triu(np.ones_like(corr, dtype=bool))
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(corr, annot=True, fmt=".1f", cmap='inferno', square=True, linewidths=.5, ax=ax, mask=mask)
fig.savefig('heatmap.png')
return fig
draw_heat_map()
Your browser information:
Lo user agent è: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Progetti di Analisi dei dati con Python - Visualizzatore di Dati Medici