【发布时间】:2015-08-25 22:45:39
【问题描述】:
我最近在学习 python,我遇到了一个名为 scikit learn 的包,我们可以在其中使用 python 库和自定义代码来生成各种绘图。我已经安装了所有依赖项,然后我下载并安装了 scikit learn 但是当我尝试运行示例代码时,我在生成绘图时遇到错误。
代码
from sklearn import datasets
from sklearn.cross_validation import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt
lr = linear_model.LinearRegression()
boston = datasets.load_boston()
y = boston.target
# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validated:
predicted = cross_val_predict(lr, boston.data, y, cv=10)
fig,ax = plt.subplots() ## error comes from calling the function plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()
错误
fig,ax = plt.subplots()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 866, in subplots
fig = figure(**fig_kw)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
这与设置环境变量有关吗?我正在使用python 2.7.3,这个包应该可以使用它还是我必须放置路径或其他东西?抱歉,我对 python 很陌生,只是想很快弄清楚我是否可以使用这个包来快速生成绘图。感谢您为摆脱此错误提供任何帮助。
【问题讨论】:
-
这似乎不是
scikit-learn问题。从matplotlib创建图时存在问题。你确定安装正确吗? -
是的,这是 matplotlib 的问题,而不是 scikit-learn 的问题。我已经通过以下命令 {pip install --user --install-option="--prefix=" -U scikit-learn} 在 linux 中安装了 scikit-learn,安装没有任何问题。然后我直接使用示例中的脚本在 python2.7.3 上运行它们。我在脚本中导入 matplotlib 应该没有问题吧?
标签: python python-2.7 scikit-learn