Tell us what’s happening:
Describe your issue in detail here.
Something wrong with test module. In the class CatPlotTestCase
at the line 10, the self.fig.axes
is a (1, 2) Numpy array, and self.ax
is another Numpy array. This cause type errors on line 13 and line 26.
And, there are just two numbers of my correlation plot fail to match the answer, may you have a look at it as well?
Your code so far
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’] = df[“weight”] / (df[“height”] / 100)**2
df[“overweight”] = (df[“overweight”] > 25)*1
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”] = (df[“cholesterol”] > 1) * 1
df[“gluc”] = (df[“gluc”] > 1) * 1
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= [“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.
df_cat = df_cat.groupby(["cardio", "variable"]).count().sort_index().reset_index()
df_cat.rename({"value":"total"}, axis=1, inplace=True)
# Draw the catplot with 'sns.catplot()'
# Get the figure for the output
fig = sns.catplot(data = df_cat, x = "variable", y = "total" , hue = "cardio", col = "cardio", kind = "bar")
# 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.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.02)) &
(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))
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(11, 9))
# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr,
mask = mask,
# vmin = -0.16,
# vmax = 0.3,
# center = 0,
annot = True,
fmt = ".1f",
# cbar_kws = {"shrink": 0.5, "ticks":[-0.08, 0.00, 0.08, 0.16, 0.24]}
)
# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0
Challenge: Data Analysis with Python Projects - Medical Data Visualizer
Link to the challenge: