‘’’
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*(df["height"])/100)
df.loc[df["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "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["overweight"]>25, "overweight"]=1
df.loc[df["overweight"]<=25, "overweight"]=0
# 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.value_counts().reset_index(name="total")
# Draw the catplot with 'sns.catplot()'
var = ["active", "alco", "cholesterol", "gluc", "overweight", "smoke"]
fig = sns.catplot(x ="variable", y= "total", hue ="value", data = "df_cat", kind = "bar", order = var, col = "cardio").fig
# 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[(df["ap_lo"]<=df["ap_hi"])&(df["height"]>=df["height"].quantile(0.025))&(df["height"]<=df["height"].quantile(0.0975))&(df["weight"]>=df["weight"].quantile(0.025))&(df["weight"]<=df["weight"].quantile(0.0975))]
‘’’
Output
‘’’
python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-ztkaysjt because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Traceback (most recent call last):
File "main.py", line 2, in <module>
import medical_data_visualizer
File "/home/runner/boilerplate-medical-data-visualizer-3/medical_data_visualizer.py", line 7, in <module>
df =pd.read_csv('medical_data_visualizer.csv')
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 586, in read_csv
return _read(filepath_or_buffer, kwds)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 482, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 811, in __init__
self._engine = self._make_engine(self.engine)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/parsers/readers.py", line 1040, in _make_engine
return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-1t3e315s because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Traceback (most recent call last):
File "main.py", line 6, in <module>
medical_data_visualizer.draw_cat_plot()
File "/home/runner/boilerplate-medical-data-visualizer-3/medical_data_visualizer.py", line 28, in draw_cat_plot
fig = sns.catplot(x ="variable", y= "total", hue ="value", data = "df_cat", kind = "bar", order = var, col = "cardio").fig
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/_decorators.py", line 46, in inner_f
return f(**kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/categorical.py", line 3792, in catplot
p.establish_variables(x_, y_, hue, data, orient, order, hue_order)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/seaborn/categorical.py", line 144, in establish_variables
x = data.get(x, x)
AttributeError: 'str' object has no attribute 'get'
‘’’