forked from freeCodeCamp/boilerplate-medical-data-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedical_data_visualizer.py
78 lines (57 loc) · 1.67 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
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
df['overweight'] = np.where((df['weight'] / ((df['height'] / 100) ** 2) > 25), 1, 0)
# 3
df['cholesterol'] = np.where(df['cholesterol'] <= 1, 0, 1)
df['gluc'] = np.where(df['gluc'] <= 1, 0, 1)
# 4
def draw_cat_plot():
# 5
df_cat = pd.melt(
df,
['cardio'],
['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'],
['variable'],
'value'
)
#print(df_cat)
# 6
df_cat = df_cat.groupby(['cardio', 'variable', 'value'])['value'].count().reset_index(name='total')
#print(df_cat)
# 7
# 8
fig = sns.catplot(df_cat, kind='bar', x='variable', y='total', col='cardio', hue='value')
# 9
fig.savefig('catplot.png')
return fig
# 10
def draw_heat_map():
# 11
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))]
# print(df_heat)
# 12
corr = df_heat.corr()
corr = corr.round(1)
# 13
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))
# mask = df_heat.mask()
# 14
fig, ax = plt.subplots(figsize=(12, 12))
# 15
ax = sns.heatmap(corr, mask=mask, ax=ax, annot=True,
robust=True, fmt='.1f', linewidths=0.5, center=0, square=True)
fig = ax.get_figure()
# 16
fig.savefig('heatmap.png')
return fig