Python 3.x 无法在jupyter笔记本中绘制直方图
我无法在Jupyter笔记本中绘制直方图。下面是代码和相应的错误消息Python 3.x 无法在jupyter笔记本中绘制直方图,python-3.x,matplotlib,jupyter-notebook,Python 3.x,Matplotlib,Jupyter Notebook,我无法在Jupyter笔记本中绘制直方图。下面是代码和相应的错误消息 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.s
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:
我是新手,请帮我解决这个问题
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))
为了使用.hist
您必须访问包含数据的numpy数组字典,然后将其转换为pandas dataframe,以便使用
.hist
您没有指定,所以我假设您想要绘制“目标”
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)
您没有指定,所以我假设您想要绘制“目标”
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)
您的呼叫中缺少
plot
:housing\u data.plot.hist(…)
应该可以it@busybear,我尝试使用housing_data.plot.hist()得到AttributeError:plotAh my bad。我只是假设housing\u data
是一个数据帧。plot.hist
方法特定于数据帧<代码>住房数据或多或少是一本字典;您需要弄清楚如何处理这些数据,或者要从中打印哪些数据。您的调用中缺少plot
:housing\u data.plot.hist(…)
应该这样做it@busybear,我尝试使用housing_data.plot.hist()得到AttributeError:plotAh my bad。我只是假设housing\u data
是一个数据帧。plot.hist
方法特定于数据帧<代码>住房数据或多或少是一本字典;你需要弄清楚你想如何处理这些数据,或者你想从中绘制什么样的数据。非常感谢。这真的帮了我大忙!很高兴我帮了忙!如果你有问题,请随时提问。如果问题得到回答,请勾选答案下方的勾号=)非常感谢。这真的帮了我大忙!很高兴我帮了忙!如果你有问题,请随时提问。如果问题已回答,请勾选答案下方的勾号=)