猿问

类型错误:“DesignMatrix”对象不可调用

我正在尝试在 JupyterLab 上的 ipynb 笔记本上使用 patsy 包创建 B 样条线:


from patsy import dmatrix


bs = dmatrix("bs(x, df=50, degree=1) - 1", {"x": x})

axes[0].plot(x, bs)

axes[0].set_title("Basis functions")


plt.show()

我第一次运行它时效果很好。但是当我尝试再次重新运行此单元时,它失败并出现以下错误:


-----------------------------------------------------

TypeError                                 Traceback (most recent call last)

/opt/conda/lib/python3.8/site-packages/patsy/compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs)

     35     try:

---> 36         return f(*args, **kwargs)

     37     except Exception as e:


/opt/conda/lib/python3.8/site-packages/patsy/eval.py in eval(self, expr, source_name, inner_namespace)

    16         code = compile(expr, source_name, "eval", self.flags, False)

--> 165         return eval(code, {}, VarLookupDict([inner_namespace]

    166                                             + self._namespaces))


<string> in <module>


TypeError: 'DesignMatrix' object is not callable


The above exception was the direct cause of the following exception:


PatsyError                                Traceback (most recent call last)

<ipython-input-6-6ed4ba95a384> in <module>

      2 

      3 _, axes = plt.subplots(2, figsize=(16, 16))

----> 4 bs = dmatrix("bs(x, df=50, degree=1) - 1", {"x": x})

      5 axes[0].plot(x, bs)

      6 axes[0].set_title("Basis functions")


/opt/conda/lib/python3.8/site-packages/patsy/highlevel.py in dmatrix(formula_like, data, eval_env, NA_action, return_type)

    288     """

    289     eval_env = EvalEnvironment.capture(eval_env, reference=1)

--> 290     (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,

    291                                       NA_action, return_type)

    292     if lhs.shape[1] != 0:

大话西游666
浏览 98回答 1
1回答

FFIVE

最终这是因为我自己覆盖了变量bs,因此覆盖了bspatsy 字符串内的函数。这就是为什么 eval 像往常一样是反模式......
随时随地看视频慕课网APP

相关分类

Python
我要回答