Tell us what’s happening:
Describe your issue in detail here.
My solution for the medical data visualizer project has three numbers wrong in the heatmap and fails the test. The numbers are associated with weight/height, cholesterol/gluc and overweight/height and are each off by .1. I suspect I have made a mistake cleaning or normalizing the data, but I cannot find any mistakes. I have tried cleaning several different ways and continue to get the same heatmap errors. Any ideas or suggestions about why my solution fails the heatmap test would be appreciated.
I am also getting two errors that I do not understand:
ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
Traceback (most recent call last):
File “/home/gary/Documents/a_Learning/DataScience/DataAnalysisProjects/3Medical_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 (test_module.CatPlotTestCase)
Traceback (most recent call last):
File “/home/gary/Documents/a_Learning/DataScience/DataAnalysisProjects/3Medical_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’
The catplot that my solution creates is identical to the example catplot provided by fCC, so I don’t understand why the errors occur.
I have very little experience with python testing, so any helpful suggestions would be appreciated.
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’] = df[‘weight’]/(df[‘height’]/100)**2
df.loc[df[‘overweight’] > 25, ‘overweight’] = 1
df.loc[df[‘overweight’] > 1, ‘overweight’] = 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.loc[df[‘cholesterol’] == 1, ‘cholesterol’] = 0
df.loc[df[‘cholesterol’] > 1, ‘cholesterol’] = 1
df.loc[df[‘gluc’] == 1, ‘gluc’] = 0
df.loc[df[‘gluc’] > 1, ‘gluc’] = 1
df_clean = df.loc[(df[‘height’] >= df[‘height’].quantile(0.025)) & (df[‘height’] <= df[‘height’].quantile(0.975))]
df_clean1 = df_clean.loc[(df_clean[‘weight’] >= df_clean[‘weight’].quantile(0.025)) & (df_clean[‘weight’] <= df_clean[‘weight’].quantile(0.975))]
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=[‘cholesterol’, ‘gluc’, ‘alco’, ‘active’, ‘smoke’, ‘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 columns for the catplot to work correctly.
df_cat = df_cat.groupby(['cardio','variable','value']).size().reset_index(name='total')
# Draw the catplot with 'sns.catplot()'
fig = sns.catplot(x='variable',
y='total',
col='cardio',
hue='value',
legend=True,
data=df_cat,
kind='bar',
ci=None
)
# 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_clean1.copy()
# Calculate the correlation matrix
corr = df_heat.corr()
# Generate a mask for the upper triangle
mask = np.triu(corr)
# Set up the matplotlib figure
sns.set_style('white')
#sns.color_palette('rocket', as_cmap=True)
sns.set(rc={'figure.figsize':(12, 12)})
sns.set(rc={'figure.facecolor':'white'})
# Draw the heatmap with 'sns.heatmap()'
fig, ax = plt.subplots(figsize=(10,10))
ax = sns.heatmap(corr,
annot=True,
mask=mask,
fmt='.1f',
linewidths=.5,
)
# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0
Challenge: Medical Data Visualizer
Link to the challenge: