You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Statsmodels OLS回归报错TypeError:需指定轴(a与weights形状不同)

OLS回归报错:TypeError: Axis must be specified when shapes of a and weights differ.

问题场景

尝试用statsmodels执行OLS回归,代码如下:

# 创建数据框格式的设计矩阵:
y, X = dmatrices("GR ~ Im + Ct + Op + Ap + Mt", data=df, return_type='dataframe')
# 定义并拟合OLS模型:
model = sm.OLS(y, X)
results = model.fit()
# 查看结果摘要:
print(results.summary())

运行后触发报错:TypeError: Axis must be specified when shapes of a and weights differ.

完整报错栈

Cell In[106], line 2
      1 #INSPECT summary of results
----> 2 print(results.summary())

File ~/anaconda3/lib/python3.10/site-packages/statsmodels/regression/linear_model.py:2739, in RegressionResults.summary(self, yname, xname, title, alpha, slim)
   2735     top_left.append(('Covariance Type:', [self.cov_type]))
   2737 rsquared_type = '' if self.k_constant else ' (uncentered)'
   2738 top_right = [('R-squared' + rsquared_type + ':',
-> 2739               ["%#8.3f" % self.rsquared]),
   2740              ('Adj. R-squared' + rsquared_type + ':',
   2741               ["%#8.3f" % self.rsquared_adj]),
   2742              ('F-statistic:', ["%#8.4g" % self.fvalue]),
   2743              ('Prob (F-statistic):', ["%#6.3g" % self.f_pvalue]),
   2744              ('Log-Likelihood:', None),
   2745              ('AIC:', ["%#8.4g" % self.aic]),
   2746              ('BIC:', ["%#8.4g" % self.bic])
   2747              ]
   2749 if slim:
   2750     slimlist = ['Dep. Variable:', 'Model:', 'No. Observations:',
   2751                 'Covariance Type:', 'R-squared:', 'Adj. R-squared:',
   2752                 'F-statistic:', 'Prob (F-statistic):']

File ~/anaconda3/lib/python3.10/site-packages/pandas/_libs/properties.pyx:36, in pandas._libs.properties.CachedProperty.__get__()

File ~/anaconda3/lib/python3.10/site-packages/statsmodels/regression/linear_model.py:1752, in RegressionResults.rsquared(self)
   1744 """
   1745 R-squared of the model.
   1746 
   (...)
   1749 omitted.
   1750 """
   1751 if self.k_constant:
-> 1752     return 1 - self.ssr/self.centered_tss
   1753 else:
   1754     return 1 - self.ssr/self.uncentered_tss

File ~/anaconda3/lib/python3.10/site-packages/pandas/_libs/properties.pyx:36, in pandas._libs.properties.CachedProperty.__get__()

File ~/anaconda3/lib/python3.10/site-packages/statsmodels/regression/linear_model.py:1702, in RegressionResults.centered_tss(self)
   1700 sigma = getattr(model, 'sigma', None)
   1701 if weights is not None:
-> 1702     mean = np.average(model.endog, weights=weights)
   1703     return np.sum(weights * (model.endog - mean)**2)
   1704 elif sigma is not None:
   1705     # Exactly matches WLS when sigma is diagonal

File <__array_function__ internals>:180, in average(*args, **kwargs)

File ~/anaconda3/lib/python3.10/site-packages/numpy/lib/function_base.py:531, in average(a, axis, weights, returned, keepdims)
    529 if a.shape != wgt.shape:
    530     if axis is None:
-> 531         raise TypeError(
    532             "Axis must be specified when shapes of a and weights "
    533             "differ.")
    534     if wgt.ndim != 1:
    535         raise TypeError(
    536             "1D weights expected when shapes of a and weights differ.")

TypeError: Axis must be specified when shapes of a and weights differ.

解决思路

1. 调整因变量y的维度

报错根源是y为DataFrame格式时,内部计算中心化总平方和时维度不匹配。可以将y转为一维结构:

# 方法1:转为numpy一维数组
y = y.values.ravel()
model = sm.OLS(y, X)

# 方法2:转为pandas Series
model = sm.OLS(y.squeeze(), X)

2. 修改dmatrices返回类型

直接让dmatrices返回numpy数组而非DataFrame,避免维度问题:

y, X = dmatrices("GR ~ Im + Ct + Op + Ap + Mt", data=df, return_type='numpy')
model = sm.OLS(y, X)

3. 清理数据中的缺失值

数据存在缺失值可能导致生成的矩阵形状异常,先清理数据:

df = df.dropna()
# 再执行原回归流程

内容的提问来源于stack exchange,提问作者matthins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:32:44