Medical data visualization


  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'] = np.where(df['weight'].div((df['height']/100).pow(2))>25,1,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['cholesterol'] = np.where(df['cholesterol']>1,1,0)
  df['gluc'] = np.where(df['gluc']>1,1,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 = df.melt(id_vars=['cardio'],value_vars=sorted(['cholesterol', 'gluc', 'smoke', 'alco', 'active', '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.
    # Draw the catplot with 'sns.catplot()'
    fig = sns.catplot(data=df_cat, kind='count', x='variable', hue='value',col="cardio")
    # 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.975))&(df['weight']>=df['weight'].quantile(0.025)) & (df['weight']<=df['weight'].quantile(0.975))]
    # Calculate the correlation matrix
    corr = df_heat.corr().round(1)
    # Generate a mask for the upper triangle
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True
    # Set up the matplotlib figure
    fig = plt.figure()
    # Draw the heatmap with 'sns.heatmap()'
    sns.heatmap(data=corr,mask=mask,annot=True,fmt=".1f")
    # Do not modify the next two lines
    fig.savefig('heatmap.png')
    return fig
  medical_data_visualizer.draw_cat_plot()
  medical_data_visualizer.draw_heat_map()