Trying to fix cv_pls

This commit is contained in:
2007-07-30 09:46:43 +00:00
parent 9ccdf97d07
commit b39e71ca2b
3 changed files with 139 additions and 59 deletions
+11 -12
View File
@@ -61,7 +61,6 @@ def w_pls_cv_val(X, Y, amax, n_blocks=None, algo='simpls'):
V = w_pls_gen(XXt, Y, n_blocks=n_blocks, center=True)
for Din, Doi, Yin, Yout in V:
ym = -sum(Yout, 0)[newaxis]/(1.0*Yin.shape[0])
Yin = Yin - ym
PRESS[:,0] = PRESS[:,0] + ((Yout - ym)**2).sum(0)
if algo=='simpls':
dat = w_simpls(Din, Yin, amax)
@@ -74,15 +73,14 @@ def w_pls_cv_val(X, Y, amax, n_blocks=None, algo='simpls'):
for j in range(l):
TQ = dot(That, triu(dot(Q[j,:][:,newaxis], ones((1,amax)))) )
E = Yout[:,j][:,newaxis] - TQ
E = E + sum(E, 0)/Din.shape[0]
E = E + sum(E, 0)/Din.shape[0]
PRESS[j,1:] = PRESS[j,1:] + sum(E**2, 0)
Yhat = Y - dot(That,Q.T)
rmsep = sqrt(PRESS/Y.shape[0])
aopt = find_aopt_from_sep(rmsep)
return rmsep, Yhat, aopt
#Yhat = Yin - dot(That,Q.T)
msep = PRESS/(Y.shape[0])
aopt = find_aopt_from_sep(msep)
return sqrt(msep)
def pls_val(X, Y, amax=2, n_blocks=10, algo='pls', metric=None):
k, l = m_shape(Y)
PRESS = zeros((l, amax+1), dtype='<f8')
EE = zeros((amax, k, l), dtype='<f8')
@@ -105,9 +103,10 @@ def pls_val(X, Y, amax=2, n_blocks=10, algo='pls', metric=None):
EE[a,out,:] = E
PRESS[:,a+1] = PRESS[:,a+1] + sum(E**2,0)
rmsep = sqrt(PRESS/(k-1.))
aopt = find_aopt_from_sep(rmsep)
return rmsep, Yhat, aopt
#rmsep = sqrt(PRESS/(k-1.))
msep = PRESS
aopt = find_aopt_from_sep(msep)
return msep, Yhat, aopt
def lpls_val(X, Y, Z, a_max=2, nsets=None,alpha=.5):
"""Performs crossvalidation to get generalisation error in lpls"""
@@ -270,7 +269,7 @@ def lpls_jk(X, Y, Z, a_max, nsets=None, alpha=.5):
def find_aopt_from_sep(sep, method='75perc'):
"""Returns an estimate of optimal number of components from rmsecv.
"""
sep = sep.copy()
if method=='vanilla':
# min rmsep
rmsecv = sqrt(sep.mean(0))
@@ -282,7 +281,7 @@ def find_aopt_from_sep(sep, method='75perc'):
med = median(sep)
prc_75 = []
for col in sep.T:
col.sort()
col.sort() #this is inplace -> ruins sep, so we are doing a copy
prc_75.append(col[int(ind)])
prc_75 = array(prc_75)
for i in range(1, sep.shape[1], 1):