Attribute error during testing

Tell us what’s happening:
The test module fails giving the error 'numpy.ndarray' object has no attribute 'get_xlabel'. A very similar error comes up in the second test. Additionally for some reasons the values in my correlation matrix are wrong.

This is the console output:

python main.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-x5crtemw because the default path (/config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
EE          id    age  gender  height  ...  alco  active  cardio  overweight
0          0  18393       2     168  ...     0       1       0         0.0
1          1  20228       1     156  ...     0       1       1         1.0
2          2  18857       1     165  ...     0       0       1         0.0
3          3  17623       2     169  ...     0       1       1         1.0
4          4  17474       1     156  ...     0       0       0         0.0
...      ...    ...     ...     ...  ...   ...     ...     ...         ...
69993  99991  19699       1     172  ...     0       1       1         0.0
69994  99992  21074       1     165  ...     0       1       1         1.0
69995  99993  19240       2     168  ...     0       1       0         1.0
69998  99998  22431       1     163  ...     0       0       1         1.0
69999  99999  20540       1     170  ...     0       1       0         0.0

[63259 rows x 14 columns]
.          id    age  gender  height  ...  alco  active  cardio  overweight
0          0  18393       2     168  ...     0       1       0         0.0
1          1  20228       1     156  ...     0       1       1         1.0
2          2  18857       1     165  ...     0       0       1         0.0
3          3  17623       2     169  ...     0       1       1         1.0
4          4  17474       1     156  ...     0       0       0         0.0
...      ...    ...     ...     ...  ...   ...     ...     ...         ...
69993  99991  19699       1     172  ...     0       1       1         0.0
69994  99992  21074       1     165  ...     0       1       1         1.0
69995  99993  19240       2     168  ...     0       1       0         1.0
69998  99998  22431       1     163  ...     0       0       1         1.0
69999  99999  20540       1     170  ...     0       1       0         0.0

[63259 rows x 14 columns]
[]
F
======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-1/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-1/test_module.py", line 13, in test_line_plot_labels
    actual = self.ax.get_xlabel()
AttributeError: 'numpy.ndarray' object has no attribute 'get_xlabel'

======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer-1/test_module.py", line 47, in test_heat_map_values
    self.assertEqual(actual, expected, "Expected different values in heat map.")
AssertionError: Lists differ: [] != ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5[616 chars]0.1']

Second list contains 91 additional elements.
First extra element 0:
'0.0'

Diff is 941 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map.

----------------------------------------------------------------------
Ran 4 tests in 9.978s

FAILED (failures=1, errors=2)

Your code so far

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


# Import data
data = pd.read_csv("medical_examination.csv")


#Add 'overweight' column
data['overweight'] = None

for index, row in data.iterrows():
  if (row["weight"]/((row["height"]/100)**2))> 25: 
    data.loc[index, "overweight"] = int(1)
  else: 
    data.loc[index, "overweight"] = int(0)
data = data.astype({"overweight": float})
# Normalize data bindex, "y 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.
for index, row in data.iterrows(): 
  if data.loc[index, "cholesterol"] > 1: 
    data.loc[index,"cholesterol"] = 1 
  else:
    data.loc[index,"cholesterol"] = 0
  if data.loc[index, "gluc"] > 1: 
    data.loc[index,"gluc"] = 1 
  else:
    data.loc[index,"gluc"] = 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'.
    attr_ls = ['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke']
    df_cat = pd.melt(data, "cardio",attr_ls)
    
    # 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.
    
    #return df_cat_cardio0

    fig = sns.catplot(data=df_cat, x = "variable", col = "cardio", kind = "count", hue = "value")
       

    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig


# Draw Heat Map
def draw_heat_map():
    # Clean the data
    
    list_to_plot = ["id","age","gender","height","weight","ap_hi","ap_lo","cholesterol","gluc","smoke","alco","active","cardio","overweight"]
    data_heatmap = data[list_to_plot]
    df_heat = data_heatmap.loc[(data['ap_lo'] <= data['ap_hi'])&(data['height'] >= data['height'].quantile(0.025))&(data['height'] <= data['height'].quantile(0.975))&(data['weight'] >= data['weight'].quantile(0.025))&(data['weight'] <= data['weight'].quantile(0.975))]
    
    

    # Calculate the correlation matrix
    corr = df_heat.corr()
    #print(corr)
      

    # Generate a mask for the upper triangle

    mask = np.triu(np.ones_like(corr, dtype=bool))
    fig = None
    fig, ax = plt.subplots()
    ax = sns.heatmap(corr, mask=mask)
    # Draw the heatmap with the mask and correct aspect ratio
    # 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/94.0.4606.71 Safari/537.36 Edg/94.0.992.38

Challenge: Medical Data Visualizer

Link to the challenge:

Please don’t loop over the rows >.<
The whole point of using pandas and other libraries is so you can apply transformations to tens of thousands of entries with a single command in a fraction of the time Python would need…

Also it would help if you could provide the error messages or even link us to your project.

As of now, the error is basically saying you are returning the wrong object in one of your functions. Because you are returning a numpy.ndarray, but it’s expecting another object that has the .get_xlabel attribute. The test just includes calling the attribute, not checking the class itself, hence the error not plainly saying “wrong class”.

1 Like

I fixed the looping over rows thing, the code now runs a lot faster. I however do not know how I can change the object class produced by seaborns catplot. Also for some reason my correlation matrix looks diffrent and their appears to be a diffrence in values also.
boilerplate-medical-data-visualizer-1 - Replit
Here the link to the project.

Thanks, the link makes it easier ^^

Yeah the catplot one is weird and I’ll just give you the solution: fig = fig.fig
I just picked it up from another solution here in the forum because I was out of ideas ^^°

For the second one, your correlation-matrix looks different because you have a different color scheme - that alone is not an issue.
The issue is that it doesn’t have any numbers on the fields. The “annotations” need to be “formatted” as floats with one decimal. I hope this helps finding the arguments needed :wink:

Yes found it! Thank you very much I finally figured it out !