gpt4 book ai didi

python - Statsmodel 多元 OLS 错误 "matrices are not aligned"

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:28 27 4
gpt4 key购买 nike

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

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()

最佳答案

endog 参数应该是因变量的一维向量。将模型中的参数 Y 更改为 y(例如)可以让代码运行而不会出现错误。

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

关于python - Statsmodel 多元 OLS 错误 "matrices are not aligned",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28122820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com