【问题标题】:"ValueError: shapes not aligned" while doing OLS regression on a DataFrame对 DataFrame 进行 OLS 回归时出现“ValueError:形状未对齐”
【发布时间】:2018-04-07 23:36:42
【问题描述】:

我试图在已有的 DataFrame 上运行 OLS 回归,但出现此错误:

ValueError:形状 (48,34) 和 (48,34) 未对齐:34 (dim 1) != 48 (dim 0)

我不确定如何修复对齐。我已经包含了我尝试运行的 OLS 代码和 DataFrame 输出的图片。整个DataFrame有4列48行。

import numpy as np
import statsmodels.formula.api as sm
result = sm.ols(formula="price ~ ownership + shipping + title", data=sold1).fit()
result.summary()

【问题讨论】:

  • @nazul 是 sold1 已经是数据框了吗?你能分享一下形状吗,用np.shape(sold1)
  • 检查数据框的数据类型。它应该是数字,但分类变量除外。我的猜测是您的价格列是字符串/对象。

标签: python pandas statsmodels


【解决方案1】:

在循环中运行线性回归以计算因子的 vif 时遇到了类似的问题。正如 Josef 在 cmets 中提到的那样,原因是,我有两列作为“对象”,而其余列是“int”。删除了这两个“对象”列并且它起作用了。

【讨论】:

  • 所以我检查了我的数据,它有这种类型 -CategoricalDtype(categories=[1, 2, 3, 4], ordered=True 。我该如何处理它?只是数字?跨度>
猜你喜欢
  • 2022-08-03
  • 2019-05-11
  • 2016-05-04
  • 2022-12-07
  • 2017-05-07
  • 1970-01-01
  • 2019-06-03
  • 2018-03-06
  • 2019-03-06
相关资源
最近更新 更多