Solution
This commit is contained in:
+26
-19
@@ -4,31 +4,31 @@ import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
# Import data
|
||||
df = None
|
||||
df = pd.read_csv("medical_examination.csv")
|
||||
|
||||
# Add 'overweight' column
|
||||
df['overweight'] = None
|
||||
df['overweight'] = (df['weight']/((df['height']/100)**2) > 25).astype('int8')
|
||||
|
||||
# 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['cholesterol'] = (df['cholesterol'] != 1).astype('int8')
|
||||
df['gluc'] = (df['gluc'] != 1).astype('int8')
|
||||
|
||||
|
||||
# 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 = None
|
||||
|
||||
|
||||
# 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 = None
|
||||
df0 = df[df['cardio'] == 0]
|
||||
df0long = pd.melt(df0, value_vars=['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'])
|
||||
df1 = df[df['cardio'] == 1]
|
||||
df1long = pd.melt(df1, value_vars=['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'])
|
||||
|
||||
|
||||
# Draw the catplot with 'sns.catplot()'
|
||||
|
||||
|
||||
|
||||
# Get the figure for the output
|
||||
fig = None
|
||||
|
||||
fig, axs = plt.subplots(ncols=2, figsize=(15, 5))
|
||||
plot0 = sns.countplot(data=df0long, x='variable', hue='value', ax=axs[0]).set(title='cadio = 0', ylabel="total")
|
||||
axs[0].legend([],[], frameon=False)
|
||||
plot1 = sns.countplot(data=df1long, x='variable', hue='value', ax=axs[1]).set(title='cadio = 1', ylabel="total")
|
||||
sns.move_legend(axs[1], "right", bbox_to_anchor=(1.15, 0.5))
|
||||
|
||||
# Do not modify the next two lines
|
||||
fig.savefig('catplot.png')
|
||||
@@ -38,21 +38,28 @@ def draw_cat_plot():
|
||||
# Draw Heat Map
|
||||
def draw_heat_map():
|
||||
# Clean the data
|
||||
df_heat = None
|
||||
global df
|
||||
df = df[df['ap_lo'] <= df['ap_hi']]
|
||||
df = df[(df['height'] >= df['height'].quantile(0.025)) & (df['height'] <= df['height'].quantile(0.975))]
|
||||
df = df[(df['weight'] >= df['weight'].quantile(0.025)) & (df['weight'] <= df['weight'].quantile(0.975))]
|
||||
|
||||
# Calculate the correlation matrix
|
||||
corr = None
|
||||
def abs_zero(x):
|
||||
for i in range(len(x)):
|
||||
if x[i] == 0:
|
||||
x[i] = abs(x[i])
|
||||
return x
|
||||
corr = df.corr().round(decimals=1).transform(abs_zero)
|
||||
|
||||
# Generate a mask for the upper triangle
|
||||
mask = None
|
||||
|
||||
mask = np.triu(np.ones_like(corr, dtype=bool))
|
||||
|
||||
|
||||
# Set up the matplotlib figure
|
||||
fig, ax = None
|
||||
fig, axs = plt.subplots(ncols=1, figsize=(10, 10))
|
||||
|
||||
# Draw the heatmap with 'sns.heatmap()'
|
||||
|
||||
sns.heatmap(corr, mask=mask, annot=True, fmt=".1f")
|
||||
|
||||
|
||||
# Do not modify the next two lines
|
||||
|
||||
Reference in New Issue
Block a user