gpt4 book ai didi

python - 为什么 OLS 引发 LinAlgError : SVD did not converge?

转载 作者:太空狗 更新时间:2023-10-30 01:28:40 26 4
gpt4 key购买 nike

我有一个数组:

Num Col2 Col3  Col4  
1 6 1 1
2 60 0 2
3 60 0 1
4 6 0 1
5 60 1 1

还有代码:

y = df.loc[:,'Col3']  # response
X = df.loc[:,['Col2','Col4']] # predictor
X = sm.add_constant(X) #add constant
est = sm.OLS(y, X) #build regression
est = est.fit() #full model

当它到达 .fit() 时,它会引发一个错误:

Traceback (most recent call last):
File "D:\Users\Anna\workspace\mob1\mobols.py", line 36, in <module>
est = est.fit() #full model
File "C:\Python27\lib\site-packages\statsmodels\regression\linear_model.py", line 174, in fit
self.pinv_wexog, singular_values = pinv_extended(self.wexog)
File "C:\Python27\lib\site-packages\statsmodels\tools\tools.py", line 392, in pinv_extended
u, s, vt = np.linalg.svd(X, 0)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 1327, in svd
u, s, vt = gufunc(a, signature=signature, extobj=extobj)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 99, in _raise_linalgerror_svd_nonconvergence
raise LinAlgError("SVD did not converge")
numpy.linalg.linalg.LinAlgError: SVD did not converge

有什么问题?我该如何解决?

谢谢

最佳答案

看起来您正在使用 Pandas 和 statsmodels。我运行了您的代码片段,但没有得到“raise LinAlgError("SVD did not converge")”异常。这是我运行的:

import numpy as np
import pandas
import statsmodels.api as sm
d = {'col2': [6, 60, 60, 6, 60], 'col3': [1, 0, 0, 0, 1], 'col4': [1, 2, 1, 1, 1]}
df = pandas.DataFrame(data=d, index=np.arange(1, 6))
print df

打印:

   col2  col3  col4
1 6 1 1
2 60 0 2
3 60 0 1
4 6 0 1
5 60 1 1

y = df.loc[:, 'col3']
X = df.loc[:, ['col2', 'col4']]
X = sm.add_constant(X)
est = sm.OLS(y, X)
est = est.fit()
print est.summary()

这打印:

                            OLS Regression Results                            
==============================================================================
Dep. Variable: col3 R-squared: 0.167
Model: OLS Adj. R-squared: -0.667
Method: Least Squares F-statistic: 0.2000
Date: Sat, 28 Mar 2015 Prob (F-statistic): 0.833
Time: 16:43:02 Log-Likelihood: -3.0711
No. Observations: 5 AIC: 12.14
Df Residuals: 2 BIC: 10.97
Df Model: 2
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
const 1.0000 1.003 0.997 0.424 -3.316 5.316
col2 -8.674e-18 0.013 -6.62e-16 1.000 -0.056 0.056
col4 -0.5000 0.866 -0.577 0.622 -4.226 3.226
==============================================================================
Omnibus: nan Durbin-Watson: 1.500
Prob(Omnibus): nan Jarque-Bera (JB): 0.638
Skew: -0.000 Prob(JB): 0.727
Kurtosis: 1.250 Cond. No. 187.
==============================================================================

所以这似乎可行,所以没问题,至少对于这段代码而言。会不会是您将错误的矩阵称为 df?

关于python - 为什么 OLS 引发 LinAlgError : SVD did not converge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29307670/

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