我已经在conda环境雪花中安装了scikit learn和其他依赖项。
我键入以下启动代码
import numpy as np
import sklearn
from sklearn import linear_model
clf = linear_model.Ridge (alpha = .5)
clf.fit ([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
Ridge(alpha=0.5, copy_X=True, fit_intercept=True, max_iter=None,
normalize=False, random_state=None, solver='auto', tol=0.001)
clf.predict([1,1])
这会产生错误
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 列表显示了它们。
对我来说
,它工作正常:
In [4]: import numpy as np
In [5]: import sklearn
In [6]: from sklearn import linear_model
In [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]))
并完全删除以下行。