Python 3.x 在statsmodels中添加_常量()不起作用
我尝试将add_constant()函数用于数据集数组。在索引59处,它可以工作(创建列),但在索引60处,它没有创建。最初,testmat[59]返回(24,54)的形状,testmat[60]返回(9,54)的形状。下面是我运行add_constant函数时得到的结果:Python 3.x 在statsmodels中添加_常量()不起作用,python-3.x,statsmodels,Python 3.x,Statsmodels,我尝试将add_constant()函数用于数据集数组。在索引59处,它可以工作(创建列),但在索引60处,它没有创建。最初,testmat[59]返回(24,54)的形状,testmat[60]返回(9,54)的形状。下面是我运行add_constant函数时得到的结果: In: Xnew = sm.add_constant(testmat[59]) Out: const TRYSIL_PO TRYSIL_TA 6142 1 985.7 271.65 61
In: Xnew = sm.add_constant(testmat[59])
Out:
const TRYSIL_PO TRYSIL_TA
6142 1 985.7 271.65
6143 1 984.9 271.85
我已经查过其他消息来源,但这一条似乎还没有得到解决。 您能解释一下吗?如果已经存在方差为0的列,即具有所有相同值的列,则add_constant()似乎不起作用。我的数据集中就是这样(在上面的示例(54列)中没有提到) 解决方案是在add_constant()函数中添加has_constant选项,如下所示:
sm.add_constant(testmat[60], has_constant='add')
更多信息:
sm.add_constant(testmat[60], has_constant='add')