2

I'm unable to plot the histogram in Jupyter notebook. Here's the code below and the error message in response to it.

import pandas as pd import numpy as np from sklearn.datasets import load_boston import matplotlib.pyplot as plt housing_data = load_boston() %matplotlib inline housing_data.hist(bins = 50, figsize = (20, 15)) plt.show() KeyError Traceback (most recent call last) /anaconda3/lib/python3.6/site-packages/sklearn/utils/__init__.py in __getattr__(self, key) 60 try: ---> 61 return self[key] 62 except KeyError: KeyError: 'hist' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) <ipython-input-17-570a88b85d5d> in <module>() ----> 1 housing_data.hist(bins = 50, figsize = (20, 15)) 2 plt.show(); /anaconda3/lib/python3.6/site-packages/sklearn/utils/__init__.py in __getattr__(self, key) 61 return self[key] 62 except KeyError: 

---> 63 raise AttributeError(key) 64 65 def setstate(self, state):

AttributeError: hist 

I'm new to this and please help me with this.

3
  • You're missing plot in your call: housing_data.plot.hist(...) should do it Commented Jul 2, 2018 at 13:45
  • @busybear, I tried using housing_data.plot.hist() and I get AttributeError: plot Commented Jul 2, 2018 at 13:47
  • 1
    Ah my bad. I just assumed housing_data was a pandas dataframe. The plot.hist method is specific for pandas dataframes. housing_data is a dictionary, more or less; you need to figure out how you want to deal with that or what data from there you want to plot. Commented Jul 2, 2018 at 13:52

2 Answers 2

6
import pandas as pd import numpy as np from sklearn.datasets import load_boston import matplotlib.pyplot as plt housing_data = load_boston() %matplotlib inline pd.DataFrame(housing_data['data']).hist(bins = 50, figsize = (20, 15)) 

you have to access dictionary of numpy array that contains data and than convert it to pandas dataframe in order to use .hist

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. It really helped me
great! Glad that I helped! If you have question feel free to ask. If the question is answered please check the tick under the answer =)
1

You didn't specify so I assumed you wanted to plot 'target'?

import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_boston housing_data = load_boston() housing_data_2 = ({'target' : list(housing_data['target'])}) df = pd.DataFrame(data=housing_data_2) df.plot.hist(bins = 50) 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.