Medical Data Visualizer (AttributeError: numpy.ndarray)

I am having this error and can’t find the solution. Does anyone knows how to fix it? Thanks in advance.

EE.['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.1', '0.0', '0.2', '0.1', '0.0', '0.1', '-0.0', '-0.1', '0.1', '0.0', '0.2', '0.0', '0.1', '-0.0', '-0.0', '0.1', '0.0', '0.1', '0.4', '-0.0', '-0.0', '0.3', '0.2', '0.1', '-0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.2', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.0', '-0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '-0.0', '0.2', '0.1', '0.3', '0.2', '0.1', '-0.0', '-0.0', '-0.0', '-0.0', '0.1', '-0.1', '-0.1', '0.7', '0.0', '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1', '', '', '']
.
======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 26, in test_bar_plot_number_of_bars
    actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rectangle)])
AttributeError: 'numpy.ndarray' object has no attribute 'get_children'

======================================================================
ERROR: test_line_plot_labels (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-3/test_module.py", line 13, in test_line_plot_labels
    actual = self.ax.get_xlabel()
AttributeError: 'numpy.ndarray' object has no attribute 'get_xlabel'

----------------------------------------------------------------------
Ran 4 tests in 22.332s

FAILED (errors=2)

CODE:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Import data
df = df = pd.read_csv('medical_examination.csv')

# 'overweight' column
bmi = df['weight'] / pow((df['height'] / 100), 2)
df['overweight'] = bmi

df.loc[bmi <= 25, 'overweight'] = 0
df.loc[bmi > 25, 'overweight'] = 1

# Normalize data
df.loc[df['cholesterol'] == 1, 'cholesterol'] = 0
df.loc[df['cholesterol'] > 1, 'cholesterol'] = 1

df.loc[df['gluc'] == 1, 'gluc'] = 0
df.loc[df['gluc'] > 1, 'gluc'] = 1

# Draw Categorical Plot
def draw_cat_plot():
    # Create DataFrame for cat plot using 'pd.melt'.
    df_cat = pd.melt(
        df,
        id_vars='cardio',
        value_vars=[
            'active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'
        ])

    sns.set(font_scale=3)
    sns.set_style("white")
    fig = sns.catplot(
        x='variable',
        hue='value',
        kind='count',
        palette='colorblind',
        col='cardio',
        edgecolor='.01',
        data=df_cat,
        height=10,
        aspect=2)

    fig.savefig('catplot.png')
    return fig


# Heat Map
def draw_heat_map():
    
    df_heat = 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)))]

    corr = df_heat.corr()

    mask = np.triu(corr)

    fig, ax = plt.subplots(figsize=(15, 10))
    sns.set(font_scale=1.4)

    sns.heatmap(
        corr,
        vmax=.30,
        center=0.08,
        annot=True,
        fmt='.1f',
        cbar=True,
        square=True,
        mask=mask)

    fig.savefig('heatmap.png')
    return fig
1 Like

See this.

That is at least a partial solution.

1 Like

Me too stuck on same error .

The main issue is that in test module the method used for checking labels is applicable for a Matplotlib figure : :

but as we are using seaborn.catplot() the figure that is returned from catplot function is a FacetGrid and it does not have a get_xlabel() method.

Reference Documentation : : https://seaborn.pydata.org/generated/seaborn.catplot.html

For this method get_xlabel() that is used in test_module.py to work the figure should be a Matplotlib.axes.Axes() object .
where we can use Matplotlib.axes.Axes.set_xlabel(self , "string") to set change value of x_label.

I have tried almost everything . It would be a pleasure if anyone could help or mentor me further.

Thank you.

hey there found the solution to covert catplot to figure

fig = sns.catplot(x="variable", y = 'total', col="cardio", hue = 'value' , data=df_cat , kind="bar").fig

12 Likes

This works perfectly fine!