Loading Nix environment:
I am facing problem with the Nix environment as its never load on the console. I tried all the possibilities but it didn’t work. Please help me
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
df = pd.read_csv(‘myfile.csv’)
df[‘overweight’] = (df[‘weight’] / (df[‘height’]/100)**2).apply(lambda x: 1 if x > 25 else 0)
df[‘cholesterol’] = df[‘cholesterol’].apply(lambda x: 0 if x == 1 else 1)
df[‘gluc’] = df[‘gluc’].apply(lambda x: 0 if x == 1 else 1)
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 = pd.melt(df, id_vars = ‘cardio’, var_name = ‘variable’, value_vars = [‘alco’, ‘active’,‘cholesterol’, ‘gluc’, ‘overweight’,‘smoke’])
# Group and reformat the data to split it by 'cardio'. Show the counts of each feature.
#You will have to rename one of the collumns for the catplot to work correctly.
df_cat = pd.melt(df, var_name = 'variable', value_vars = ['active','alco','cholesterol', 'gluc','overweight','smoke'], id_vars = 'cardio')
# Draw the catplot with 'sns.catplot()'
fig = sns.catplot(data=df_cat, kind="count", x="variable",hue="value", col="cardio").set_axis_labels("variable", "total")
fig = fig.fig
# 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()
# Generate a mask for the upper triangle
mask = np.triu(corr)
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(12,12))
# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr,mask=mask, fmt='.1f',vmax=.3, linewidths=.5,square=True, cbar_kws = {'shrink':0.5},annot=True, center=0)
# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36
Challenge: Data Analysis with Python Projects - Demographic Data Analyzer
Link to the challenge: