def crossvalidate_delta(self, folds): import utils cv_scheme = utils.crossValidationScheme(folds, self.nTrain) ldeltas = SP.arange(-3, -1.5, .01) Ss = [] Us = [] Uys = [] UCs = [] err = 0.0 errs = [] for ldelta in ldeltas: for test_set in cv_scheme: train_set = ~test_set K_sub = self.kernel[SP.ix_(train_set, train_set)] K_cross = self.kernel[SP.ix_(~train_set, train_set)] # print LA.inv((K_sub + SP.eye(train_set.sum())*self.delta)) Core = SP.dot( K_cross, LA.inv((K_sub + SP.eye(train_set.sum()) * SP.exp(ldelta)))) diff = self.yTrain[test_set] -\ SP.dot(Core, self.yTrain[train_set]) err += (diff**2).sum() / diff.size S, U = LA.eigh(self.kernel[SP.ix_(train_set, train_set)]) Ss.append(S) Us.append(U) Uys.append(SP.dot(U.T, self.yTrain[train_set])) UCs.append(SP.dot(U.T, SP.ones_like(self.yTrain[train_set]))) errs.append(err / len(cv_scheme)) err = 0.0 nll_scores = [] for ldelta in ldeltas: # print 'ldelta equals', ldelta score = 0.0 for i in xrange(len(cv_scheme)): score += lmm_fast.nLLeval(ldelta, (Uys[i])[:, 0], UCs[i], Ss[i]) nll_scores.append(score / len(cv_scheme)) print 'best ldelta found ll', ldeltas[SP.argmin(nll_scores)] return ldeltas[SP.argmin(errs)]
def crossvalidate_delta(self, folds): import utils cv_scheme = utils.crossValidationScheme(folds, self.nTrain) ldeltas = SP.arange(-3, -1.5, .01) Ss = [] Us = [] Uys = [] UCs = [] err = 0.0 errs = [] for ldelta in ldeltas: for test_set in cv_scheme: train_set = ~test_set K_sub = self.kernel[SP.ix_(train_set, train_set)] K_cross = self.kernel[SP.ix_(~train_set, train_set)] # print LA.inv((K_sub + SP.eye(train_set.sum())*self.delta)) Core = SP.dot(K_cross, LA.inv((K_sub + SP.eye(train_set.sum()) * SP.exp(ldelta)))) diff = self.yTrain[test_set] -\ SP.dot(Core, self.yTrain[train_set]) err += (diff**2).sum()/diff.size S, U = LA.eigh(self.kernel[SP.ix_(train_set, train_set)]) Ss.append(S) Us.append(U) Uys.append(SP.dot(U.T, self.yTrain[train_set])) UCs.append(SP.dot(U.T, SP.ones_like(self.yTrain[train_set]))) errs.append(err/len(cv_scheme)) err = 0.0 nll_scores = [] for ldelta in ldeltas: # print 'ldelta equals', ldelta score = 0.0 for i in xrange(len(cv_scheme)): score += lmm_fast.nLLeval(ldelta, (Uys[i])[:, 0], UCs[i], Ss[i]) nll_scores.append(score/len(cv_scheme)) print 'best ldelta found ll', ldeltas[SP.argmin(nll_scores)] return ldeltas[SP.argmin(errs)]