【问题标题】:Statsmodel multivariate OLS error "matrices are not aligned"Statsmodel 多元 OLS 错误“矩阵未对齐”
【发布时间】:2015-01-24 06:03:45
【问题描述】:

我正在尝试解决多元回归。这是回归的附加代码。模型构建良好,但是当我尝试检索摘要时,它会出现以下错误

ValueError:矩阵未对齐

这是回溯:

Traceback (most recent call last):
File "/Users/mikhilraj/Desktop/try2.py", line 23, in <module>
    print mod.summary()
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1967, in summary
    top_right = [('R-squared:', ["%#8.3f" % self.rsquared]),
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/tools/decorators.py", line 97, in __get__
    _cachedval = self.fget(obj)
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1181, in rsquared
    return 1 - self.ssr/self.centered_tss
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/tools/decorators.py", line 97, in __get__
    _cachedval = self.fget(obj)
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1153, in ssr
    return np.dot(wresid, wresid)
ValueError: matrices are not aligned

代码:

import numpy as np
import statsmodels.api as sm


np.random.seed(12345)

N = 30

X = np.random.uniform(-20, 20, size=(N,10))
beta = np.random.randn(11)
X = sm.add_constant(X)


weights = np.random.uniform(1, 20, size=(N,))
weights = weights/weights.sum()

y = np.dot(X, beta) + weights*np.random.uniform(-100, 100, size=(N,))

Y = np.c_[y,y,y]

mod = sm.OLS(Y, X).fit()

print mod.summary()

【问题讨论】:

    标签: python pandas linear-regression statsmodels


    【解决方案1】:

    endog 参数应该是因变量的一维向量。将模型中的参数Y 更改为y(例如)允许代码无错误地运行。

    http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.OLS.html

    【讨论】:

    猜你喜欢
    • 2017-01-12
    • 2020-11-30
    • 2012-02-03
    • 2021-05-15
    • 2020-07-26
    • 2012-11-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多