statsmodels TypeError:当 a 的形状和权重不同时必须指定轴

问题描述 投票:0回答:0

我正在尝试使用 statsmodels 库执行 OLS 回归。

#create 设计矩阵作为数据框:

y, X = dmatrices("GR ~ Im + Ct + Op + Ap + Mt", data=df, return_type='dataframe') 

#describe 和 fit ols 模型:

model = sm.OLS(y, X)

results = model.fit()

#inspect 汇总结果:

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.
python typeerror axis statsmodels
© www.soinside.com 2019 - 2024. All rights reserved.