0
我有一個線性迴歸問題(Ax=b)
。我最初的方法有助於解決我的一些問題,使用SVD並獲得我感興趣的卡方和其他一些值,但在某些情況下,例如,如果我的迴歸問題如下所示,它正在分解:當第一個引發線性迴歸的錯誤信息時,選擇不同的方法
>>> coff=
array([[ 1., 0., 0., 0.],
[ 0., 0., -1., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 0., -1.],
[ 1., 0., -1., 0.],
[ 0., 0., 1., -1.],
[ 0., 0., -1., 0.],
[ 0., 0., 1., -1.]])
coff
矩陣實際上是在迴歸問題A
矩陣和b
是如下:
>>> b
array([-0.56168673, 0.8943901 , -0.56168673, 1.20952994, 0.33270337,
0.31513984, 0.8943901 , 0.31513984])
使用奇異值分解方法:
print "==============================SVD calculation============================"
U, s, Vh = linalg.svd(coff, full_matrices=False)
print U.shape, Vh.shape, s.shape
print s
S = scipy.linalg.diagsvd(s, 4, 4)
print allclose(coff, dot(U, dot(S, Vh)))
Sh=scipy.linalg.inv(S)
for i in range(Sh.shape[0]):
if Sh[i,i]>1.0e+04 :
Sh[i,i]=0
Uh = scipy.transpose(U)
V= scipy.transpose(Vh)
aa=dot(V, dot(Sh, Uh))
aah= scipy.transpose(aa)
S_sq=dot(Sh,Sh)
V_sq=dot(V,Vh)
covar=dot(S_sq,V_sq)
#The least square problem results
res=dot(aa,b)
wt=zeros(s.shape[0],float)
for i in range(s.shape[0]):
wt[i]=0
if math.fabs(s[i])>1.0e-04:
wt[i]=1./(s[i]*s[i])
cvm=zeros((s.shape[0],s.shape[0]),float)
for i in range(s.shape[0]):
j=0
while j<=i:
cum=0.0
for k in range(s.shape[0]):
cum=cum+Vh[i,k]*Vh[j,k]*wt[k]
cvm[i,j]=cum
cvm[j,i]=cum
j+=1
print "SVD results for seventeen filters:\n",res
print "SVD's covariance matrix:\n",cvm
sig=zeros(cvm.shape[0],float)
for i in range(cvm.shape[0]):
for j in range(cvm.shape[1]):
if i==j:
sig[i]=math.sqrt(cvm[i,j])
print 'Variance:\n',sig
chi_square=0
v=dot(coff,res)
for i in range(b.shape[0]):
chi_square += (b[i]-v[i])**2
print "chi_square:\n",chi_square
reduce_chi=chi_square/(coff.shape[0]-coff.shape[1]-1)
print "Reduced-Chisquare:\n",reduce_chi
那麼我的做法是不是優化的做法,但我需要看到比如什麼是Reduced-Chisquare
或covariance
值,但它提出了奇異矩陣錯誤當我嘗試逆S
矩陣,但如果我使用下面的過程:
Least_squares,residuals,rank,Singular_values=np.linalg.lstsq(coff, b)
它不給我任何錯誤和計算迴歸問題。 我的問題:
首先:爲什麼使用SVD
會出現這個問題呢?
第二個(非常編程問題):如何保持第一種方法並使用第二種方法,以防萬一第一種方法中出現一個錯誤?
奇怪的是,我看到的文件,但我不知道我怎麼能實現它,在第一個人會在任何階段失敗,然後再次嘗試第二個人CH。 – Dalek 2014-09-05 14:44:32