forked from freeCodeCamp/boilerplate-medical-data-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedical_data_visualizer.py
82 lines (69 loc) · 1.97 KB
/
medical_data_visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Import data
df = pd.read_csv('medical_examination.csv')
# Add 'overweight' column
df['BMI'] = df['weight'] / ((df['height'] / 100) ** 2)
df['overweight'] = (df['BMI'] > 25).astype(int)
df.drop(columns=['BMI'], inplace=True)
# Normalize data by making 0 good and 1 bad
df['cholesterol'] = (df['cholesterol'] > 1).astype(int)
df['gluc'] = (df['gluc'] > 1).astype(int)
# Draw Categorical Plot
def draw_cat_plot():
# Create DataFrame for cat plot using pd.melt
df_cat = pd.melt(
df,
id_vars=['cardio'],
value_vars=['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight']
)
# Group and reformat the data to split it by 'cardio'
df_cat = (
df_cat.groupby(['cardio', 'variable', 'value'])
.size()
.reset_index(name='total')
)
# Draw the catplot
fig = sns.catplot(
x='variable',
y='total',
hue='value',
col='cardio',
kind='bar',
data=df_cat
).fig
# Return the figure
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.975)) &
(df['weight'] >= df['weight'].quantile(0.025)) &
(df['weight'] <= df['weight'].quantile(0.975))
]
# Calculate the correlation matrix
corr = df_heat.corr()
# 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=(12, 8))
# Draw the heatmap
sns.heatmap(
corr,
mask=mask,
annot=True,
fmt=".1f",
cmap='coolwarm',
vmax=0.3,
vmin=-0.1,
square=True,
linewidths=0.5,
cbar_kws={"shrink": 0.5}
)
# Return the figure
return fig