我正在实现回归。Output_variable 是我的 y 变量,而 input2、input4、Input5&1、input6-3 是我的回归方程中的 x 变量。这些基本上都是 df 中的列。
我得到错误:
PatsyError Traceback (most recent call last) <ipython-input-73-20a8ca10f7b8> in <module> 1 import statsmodels.formula.api as sm ----> 2 model = sm.ols(formula='Output_variable ~ Input2 + input4 + Input5&1 + input6-3', data=df) PatsyError: Error evaluating factor: NameError: name 'Input5' is not defined
我对 input6 未定义也得到了同样的错误。
基本上,每当我的列名包含特殊字符,比如 Input5 中有 &,input6 中有 – 号时,就会出现问题,我会得到一个 patsyerror。
如何解决这个问题?
回答:
你可以执行以下操作:
问题出在 patsy
,你不能使用像 ‘&’ 和 ‘-‘ 这样的特殊字符。幸运的是,开发者已经解决了这个问题,并允许使用 Q("full text")
绕过这一问题