我在conda环境snowflakes中安装了scikit-learn和其他依赖项。
我输入了以下启动代码
这导致了以下错误
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/sridhar/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile execfile(filename, namespace) File "/home/sridhar/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 89, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/sridhar/anaconda3/envs/snowflakes/Test/test.py", line 6, in <module> Ridge(alpha=0.5, copy_X=True, fit_intercept=True, max_iter=None,NameError: name 'Ridge' is not defined
这意味着什么?我已经安装了所有依赖项,conda list显示它们都存在。
回答:
对我来说,运行得很好:
In [4]: import numpy as npIn [5]: import sklearnIn [6]: from sklearn import linear_modelIn [7]: clf = linear_model.Ridge (alpha = .5)In [8]: clf.fit ([[0, 0], [0, 0], [1, 1]], [0, .1, 1])Out[8]: Ridge(alpha=0.5, copy_X=True, fit_intercept=True, max_iter=None, normalize=False, random_state=None, solver='auto', tol=0.001)In [9]: clf.predict([[1,1]])Out[9]: [ 0.82727273]
看起来你是从Ipython笔记本中复制了这段代码,那里会自动打印输出结果如果存在的话。
因此,它会抛出错误,因为Ridge
在导入语句中并未定义。
如果你真的想在Spyder中运行这段代码,我建议使用print(clf.fit ([[0, 0], [0, 0], [1, 1]], [0, .1, 1]))
,并完全删除下面的行。