<jupyter_start><jupyter_text>Add an 'overweight' column to the data. To determine if a person is overweight, first calculate their BMI by dividing their weight in kilograms by the square of their height in meters. If that value is > 25 then the person is overweight. Use the value 0 for NOT overweight and the value 1 for overweight.<jupyter_code>df['overweight'] = df['overweight'] = df["weight"] / ((df["height"] / 100) ** 2)
df['overweight'] = pd.Series(np.where(df['overweight'] > 25, 1, 0))<jupyter_output><empty_output><jupyter_text>Normalize data by making 0 always good and 1 always bad. If the value of 'cholestorol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.<jupyter_code>df['cholesterol'] = pd.Series(np.where(df['cholesterol'] > 1, 1, 0))
df['gluc'] = pd.Series(np.where(df['gluc'] > 1, 1, 0))<jupyter_output><empty_output><jupyter_text>Convert the data into long format and create a chart that shows the value counts of the categorical features using seaborn's catplot(). The dataset should be split by 'Cardio' so there is one chart for each 'cardio' value. The chart should look like "examples/Figure_1.png".<jupyter_code>df_fig1 = pd.melt(df, id_vars=["cardio"], value_vars=[
                     "active", "alco", "cholesterol", "gluc", "smoke", "overweight"])
# Adding total seria / column to put the count result into
df_fig1["total"] = 0
df_fig1 = df_fig1.groupby(["variable", "cardio", "value"], as_index=False).count()
labels = df_fig1['variable'].unique().tolist()
x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

df_fig1_cardio_0 = df_fig1.loc[(df_fig1['cardio'] == 0)]
fig1_cardio_0_value_0 = df_fig1_cardio_0.loc[(df_fig1_cardio_0['value'] == 0), 'total'].tolist()
fig1_cardio_0_value_1 = df_fig1_cardio_0.loc[(df_fig1_cardio_0['value'] == 1), 'total'].tolist()

df_fig1_cardio_1 = df_fig1.loc[(df_fig1['cardio'] == 1)]
fig1_cardio_1_value_0 = df_fig1_cardio_1.loc[(df_fig1_cardio_1['value'] == 0), 'total'].tolist()
fig1_cardio_1_value_1 = df_fig1_cardio_1.loc[(df_fig1_cardio_1['value'] == 1), 'total'].tolist()<jupyter_output><empty_output><jupyter_text>plot it with seaborn<jupyter_code>fig = sns.catplot(x="variable", y="total", col="cardio",
                  hue="value", kind="bar", data=df_fig1).fig<jupyter_output><empty_output><jupyter_text>plot it with matplot lib<jupyter_code>plot_objects = plt.subplots(nrows=1, ncols=2, figsize=(14, 6))

fig, ((ax1, ax2)) = plot_objects

rects1 = ax1.bar(x - width/2, fig1_cardio_0_value_0, width, label='0')
rects2 = ax1.bar(x + width/2, fig1_cardio_0_value_1, width, label='1')

ax1.set_xticks(x)
ax1.set_xticklabels(labels)
ax1.set_ylabel('total')
ax1.set_xlabel('variable')
ax1.set_title('cardio = 0')
ax1.spines['top'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.spines['right'].set_visible(False)

ax2_rects1 = ax2.bar(x - width/2, fig1_cardio_1_value_0, width, label='0')
ax2_rects2 = ax2.bar(x + width/2, fig1_cardio_1_value_1, width, label='1')
ax2.set_xlabel('variable')
ax2.set_title('cardio = 1')
ax2.set_xticks(x)
ax2.set_xticklabels(labels)
ax2.set_yticklabels([])
ax2.legend(loc='center left', bbox_to_anchor=(1, 0.5), title="value")
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
fig.tight_layout()

plot_objects<jupyter_output><empty_output><jupyter_text>Clean the data. Filter out the following patient segments that represent incorrect data:* diastolic pressure is higher then systolic (Keep the correct data with df['ap_lo'] <= df['ap_hi']))* height is less than the 2.5th percentile (Keep the correct data with (df['height'] >= df['height'].quantile(0.025)))* height is more than the 97.5th percentile* weight is less then the 2.5th percentile* weight is more than the 97.5th percentile<jupyter_code>df_fig2 = df.loc[
    (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))
]
df_fig2<jupyter_output><empty_output><jupyter_text>Create a correlation matrix using the dataset. Plot the correlation matrix using seaborn's heatmap(). Mask the upper triangle. The chart should look like "examples/Figure_2.png".<jupyter_code>corr = df_fig2.corr()
mask = np.triu(np.ones_like(corr, dtype=np.bool))
fig, ax = plt.subplots(figsize=(15, 15))

ax = sns.heatmap(
    corr,
    mask=mask,
    fmt='.1f',
    annot=True,
    center=0,
    #vmax=0.32,
    square=True,
    linewidths=.5,
    cbar_kws={"shrink": .4}
)
 actual = [text.get_text() for text in fig.axes[0].get_default_bbox_extra_artists() if isinstance(text, mpl.text.Text)]
len(actual)
mask<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15683814303638646, 'fraction_numerical': 0.03785027185278126, 'mean_word_length': 5.89193083573487, 'pattern_counts': {'":': 1, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '72274', 'license': 'MIT', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1779, 'n_words': 408, 'path': 'python/medical-data-visualizer/medical_data_visualizer.ipynb', 'repo_name': 'dbgeek/freeCodeCamp', 'size': '4782'}