- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数组:
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/
SciPy 和 Numpy 都内置了奇异值分解 (SVD) 函数。命令基本上是 scipy.linalg.svd 和 numpy.linalg.svd。这两者有什么区别?它们中的任何一个都比另一个更好
numpy.linalg.svd 函数给出输入矩阵的完整 svd。但是我只想要第一个奇异向量。 我想知道在 numpy 中是否有任何函数用于那个或 python 中的任何其他库? 最佳答案 一种可能是
代码: import numpy from matplotlib.mlab import PCA file_name = "store1_pca_matrix.txt" ori_data = nump
我在学习SVD通过关注这个 MIT course . 矩阵构造为 C = np.matrix([[5,5],[-1,7]]) C matrix([[ 5, 5], [-1, 7]]
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我想计算 SVD ,但我没有找到合适的 java 库。现在,我在 hashmap 中存储了数据,因为矩阵不适合内存,因为大小约为 400 000 X 10 000 并且大多数为 0。我尝试了 MTJ、
运行以下代码: from sklearn.decomposition import TruncatedSVD import numpy as np X = np.matrix('1 2 3 4 5;
给定一个实数矩阵 A 使得: A 是对称的 所有非对角线项都是已知且正的 所有对角线项都缺失 排名k 我想找到 A 的最佳可能完成,称为 Ac,这样(大约)rank(Ac)=k。 矩阵 A 可能很大(
我正在寻找一个执行维基百科中描述的奇异值分解的 Java 库:从矩阵 A (m X n) 得到 A = U*S*V' 其中 U 是 m x m,S 是 m x n,V 是n x n. 谁能帮帮我? 请
我正在尝试学习用于图像处理的 SVD...例如压缩。 我的方法:使用 ImageIO 获取图像作为 BufferedImage...获取 RGB 值并使用它们获取等效的灰度值(在 0-255 范围内)
我必须在 Matlab 中使用 SVD 来获得数据的简化版本。我读到函数 svds(X,k) 执行 SVD 并返回前 k 个特征值和特征向量。如果必须规范化数据,文档中没有提及。对于归一化,我指的是减
我已经使用 SVD 找到了两组点之间的旋转矩阵。我知道 R = Transpose(U) * V 但我不明白 U 和 V 代表什么以及为什么这种乘法会产生旋转矩阵。 最佳答案 由于您的问题是理论性的并
我正在尝试在名为“LSA 简介”的论文中复制一个示例: An introduction to LSA 在示例中,它们具有以下术语-文档矩阵: 然后他们应用 SVD 并得到以下结果: 试图复制这一点,我
我正在使用带有 R 的 SVD 包,我能够通过将最低奇异值替换为 0 来降低矩阵的维数。但是当我重新组合矩阵时,我仍然拥有相同数量的特征,我找不到如何有效地删除源矩阵中最无用的特征,以减少其列数。 例
我想编写一个函数,它使用 SVD 分解来求解方程组 ax=b,其中 a 是一个方阵,b 是一个值向量。 scipy 函数 scipy.linalg.svd() 应该将 a 转换为矩阵 U W V。对于
我在 R 中有一个稀疏矩阵,它显然太大了,无法在其上运行 as.matrix()(尽管它也不是 super 大)。有问题的 as.matrix() 调用位于 svd() 函数内部,所以我想知道是否有人
我正在尝试使用 bcv 包中的 SVD 插补,但所有插补值都是相同的(按列)。 这是缺少数据的数据集 http://pastebin.com/YS9qaUPs #load data dataMiss
我有这个数组 double a[][] = {{1,1,1}, {0,1,1} , { 1,0,0} ,{0,1,0},{1,0,0},{1,0,1},{1,1,1},{1,1,1},
我们现在知道A_(m x n) = U_(m x k) * S_(k x k) * V_(k x n)^T = u_(1) * s_1 * v_(1) + u_(2) * s_2 * v_(2) +
我必须对矩阵进行 SVD,但它有一些错误,在下面的示例中 U[1][1]、U[2][1] 和 U[2][0] 应为 0。 问题是,上面的例子只是一个测试,我必须使用条件不太好的大型矩阵,我该怎么做才能
我是一名优秀的程序员,十分优秀!