def test_leave_pair_out(self): #compares holdout and leave-pair-out start = [0, 2, 3, 5] end = [1, 3, 6, 8] for X in [self.Xtrain1, self.Xtrain2]: for Y in [self.Ytrain1, self.Ytrain2]: #LPO with linear kernel rls1 = GlobalRankRLS(X, Y, regparam = 7.0, bias=3.0) lpo_start, lpo_end = rls1.leave_pair_out(start, end) ho_start, ho_end = [], [] for i in range(len(start)): P = rls1.holdout([start[i], end[i]]) ho_start.append(P[0]) ho_end.append(P[1]) ho_start = np.array(ho_start) ho_end = np.array(ho_end) assert_allclose(ho_start, lpo_start) assert_allclose(ho_end, lpo_end) #LPO Gaussian kernel rls1 = GlobalRankRLS(X, Y, regparam = 11.0, kenerl="PolynomialKernel", coef0=1, degree=3) lpo_start, lpo_end = rls1.leave_pair_out(start, end) ho_start, ho_end = [], [] for i in range(len(start)): P = rls1.holdout([start[i], end[i]]) ho_start.append(P[0]) ho_end.append(P[1]) ho_start = np.array(ho_start) ho_end = np.array(ho_end) assert_allclose(ho_start, lpo_start) assert_allclose(ho_end, lpo_end)
def test_leave_pair_out(self): #compares holdout and leave-pair-out start = [0, 2, 3, 5] end = [1, 3, 6, 8] for X in [self.Xtrain1, self.Xtrain2]: for Y in [self.Ytrain1, self.Ytrain2]: #LPO with linear kernel rls1 = GlobalRankRLS(X, Y, regparam=7.0, bias=3.0) lpo_start, lpo_end = rls1.leave_pair_out(start, end) ho_start, ho_end = [], [] for i in range(len(start)): P = rls1.holdout([start[i], end[i]]) ho_start.append(P[0]) ho_end.append(P[1]) ho_start = np.array(ho_start) ho_end = np.array(ho_end) assert_allclose(ho_start, lpo_start) assert_allclose(ho_end, lpo_end) #LPO Gaussian kernel rls1 = GlobalRankRLS(X, Y, regparam=11.0, kenerl="PolynomialKernel", coef0=1, degree=3) lpo_start, lpo_end = rls1.leave_pair_out(start, end) ho_start, ho_end = [], [] for i in range(len(start)): P = rls1.holdout([start[i], end[i]]) ho_start.append(P[0]) ho_end.append(P[1]) ho_start = np.array(ho_start) ho_end = np.array(ho_end) assert_allclose(ho_start, lpo_start) assert_allclose(ho_end, lpo_end)
def test_holdout(self): for X in [self.Xtrain1, self.Xtrain2]: for Y in [self.Ytrain1, self.Ytrain2]: m = X.shape[0] hoindices = [3, 5, 8, 10, 17, 21] hocompl = list(set(range(m)) - set(hoindices)) #Holdout with linear kernel rls1 = GlobalRankRLS(X, Y) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl]) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Holdout with bias rls1 = GlobalRankRLS(X, Y, bias=3.0) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl], bias=3.0) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Fast regularization for i in range(-5, 5): rls1.solve(2**i) rls2.solve(2**i) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Kernel holdout rls1 = GlobalRankRLS(X, Y, kernel="GaussianKernel", gamma=0.01) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl], kernel="GaussianKernel", gamma=0.01) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) for i in range(-15, 15): rls1.solve(2**i) rls2.solve(2**i) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Incorrect indices I = [0, 3, 100] self.assertRaises(IndexError, rls1.holdout, I) I = [-1, 0, 2] self.assertRaises(IndexError, rls1.holdout, I) I = [1, 1, 2] self.assertRaises(IndexError, rls1.holdout, I)
def test_holdout(self): for X in [self.Xtrain1, self.Xtrain2]: for Y in [self.Ytrain1, self.Ytrain2]: m = X.shape[0] hoindices = [3, 5, 8, 10, 17, 21] hocompl = list(set(range(m)) - set(hoindices)) #Holdout with linear kernel rls1 = GlobalRankRLS(X, Y) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl]) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Holdout with bias rls1 = GlobalRankRLS(X, Y, bias = 3.0) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl], bias = 3.0) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Fast regularization for i in range(-5, 5): rls1.solve(2**i) rls2.solve(2**i) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Kernel holdout rls1 = GlobalRankRLS(X, Y, kernel = "GaussianKernel", gamma = 0.01) rls2 = GlobalRankRLS(X[hocompl], Y[hocompl], kernel = "GaussianKernel", gamma = 0.01) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) for i in range(-15, 15): rls1.solve(2**i) rls2.solve(2**i) P1 = rls1.holdout(hoindices) P2 = rls2.predict(X[hoindices]) assert_allclose(P1, P2) #Incorrect indices I = [0, 3, 100] self.assertRaises(IndexError, rls1.holdout, I) I = [-1, 0, 2] self.assertRaises(IndexError, rls1.holdout, I) I = [1,1,2] self.assertRaises(IndexError, rls1.holdout, I)
def train_rls(): #Trains RLS with default parameters (regparam=1.0, kernel='LinearKernel') X_train, Y_train, X_test, Y_test = load_housing() #generate fold partition, arguments: train_size, k, random_seed folds = random_folds(len(Y_train), 5, 10) learner = GlobalRankRLS(X_train, Y_train) perfs = [] for fold in folds: P = learner.holdout(fold) c = cindex(Y_train[fold], P) perfs.append(c) perf = np.mean(perfs) print("5-fold cross-validation cindex %f" % perf) P_test = learner.predict(X_test) print("test cindex %f" % cindex(Y_test, P_test))
def train_rls(): #Trains RLS with default parameters (regparam=1.0, kernel='LinearKernel') X_train, Y_train, X_test, Y_test = load_housing() #generate fold partition, arguments: train_size, k, random_seed folds = random_folds(len(Y_train), 5, 10) learner = GlobalRankRLS(X_train, Y_train) perfs = [] for fold in folds: P = learner.holdout(fold) c = cindex(Y_train[fold], P) perfs.append(c) perf = np.mean(perfs) print("5-fold cross-validation cindex %f" %perf) P_test = learner.predict(X_test) print("test cindex %f" %cindex(Y_test, P_test))
def testAllPairsRankRLS(self): print( "Testing the cross-validation routines of the GlobalRankRLS module.\n\n" ) np.random.seed(100) floattype = np.float64 m, n, h = 30, 200, 10 Xtrain = np.random.rand(m, n) trainlabels = np.random.rand(m, h) trainkm = np.dot(Xtrain, Xtrain.T) ylen = 1 L = np.mat(m * np.eye(m) - np.ones((m, m), dtype=floattype)) hoindices = [5, 7] hoindices3 = [5, 7, 9] hocompl = list(set(range(m)) - set(hoindices)) hocompl3 = list(set(range(m)) - set(hoindices3)) loglambdas = range(-5, 5) for j in range(0, len(loglambdas)): regparam = 2.**loglambdas[j] print("\nRegparam 2^%1d" % loglambdas[j]) Kcv = trainkm[np.ix_(hocompl, hocompl)] Ycv = trainlabels[hocompl] Ktest = trainkm[np.ix_(hocompl, hoindices)] Xcv = Xtrain[hocompl] Xtest = Xtrain[hoindices] Lcv = np.mat((m - 2) * np.eye(m - 2) - np.ones((m - 2, m - 2), dtype=floattype)) oind = 1 rpool = {} rpool['Y'] = Ycv rpool['X'] = Xcv rpool['regparam'] = regparam naivedualrls = GlobalRankRLS(**rpool) naivedualrls.solve(regparam) hopreds = [] hopred = naivedualrls.predictor.predict(Xtest) print(str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' Naive') hopreds.append((hopred[0, oind], hopred[1, oind])) rpool = {} rpool['Y'] = trainlabels rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out( np.array([hoindices[0], 1, 1, 2]), np.array([hoindices[1], 2, 3, 3])) print( str(hopred_start[0][1]) + ' ' + str(hopred_end[0][1]) + ' Fast') hopreds.append((hopred_start[0][1], hopred_end[0][1])) self.assertAlmostEqual(hopreds[0][0], hopreds[1][0]) self.assertAlmostEqual(hopreds[0][1], hopreds[1][1]) #Test with single output just in case. rpool = {} rpool['Y'] = trainlabels[:, 1] rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out( np.array([hoindices[0], 1, 1, 2]), np.array([hoindices[1], 2, 3, 3])) #print(str(hopred_start[0]) + ' ' + str(hopred_end[0]) + ' Fast') #Test with single output just in case. rpool = {} rpool['Y'] = trainlabels[:, 1] rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out( np.array([hoindices[0]]), np.array([hoindices[1]])) #print(str(hopred_start) + ' ' + str(hopred_end) + ' Fast') hopreds = [] rpool = {} rpool['Y'] = trainlabels rpool['X'] = Xtrain rpool['regparam'] = regparam hoprimalrls = GlobalRankRLS(**rpool) hoprimalrls.solve(regparam) hopred = hoprimalrls.holdout(hoindices) print(str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' HO') hopreds.append((hopred[0, oind], hopred[1, oind])) hopred = Xtest * la.inv(Xcv.T * Lcv * Xcv + regparam * np.mat(np.eye(n))) * Xcv.T * Lcv * Ycv print( str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' Dumb (primal)') hopreds.append((hopred[0, oind], hopred[1, oind])) hopred0 = hopreds.pop(0) for hopred in hopreds: self.assertAlmostEqual(hopred0[0], hopred[0]) self.assertAlmostEqual(hopred0[1], hopred[1])
def testAllPairsRankRLS(self): print("Testing the cross-validation routines of the GlobalRankRLS module.\n\n") np.random.seed(100) floattype = np.float64 m, n, h = 30, 200, 10 Xtrain = np.random.rand(m, n) trainlabels = np.random.rand(m, h) trainkm = np.dot(Xtrain, Xtrain.T) ylen = 1 L = np.mat(m * np.eye(m) - np.ones((m, m), dtype=floattype)) hoindices = [5, 7] hoindices3 = [5, 7, 9] hocompl = list(set(range(m)) - set(hoindices)) hocompl3 = list(set(range(m)) - set(hoindices3)) loglambdas = range(-5, 5) for j in range(0, len(loglambdas)): regparam = 2. ** loglambdas[j] print("\nRegparam 2^%1d" % loglambdas[j]) Kcv = trainkm[np.ix_(hocompl, hocompl)] Ycv = trainlabels[hocompl] Ktest = trainkm[np.ix_(hocompl, hoindices)] Xcv = Xtrain[hocompl] Xtest = Xtrain[hoindices] Lcv = np.mat((m - 2) * np.eye(m - 2) - np.ones((m - 2, m - 2), dtype=floattype)) oind = 1 rpool = {} rpool['Y'] = Ycv rpool['X'] = Xcv rpool['regparam'] = regparam naivedualrls = GlobalRankRLS(**rpool) naivedualrls.solve(regparam) hopreds = [] hopred = naivedualrls.predictor.predict(Xtest) print(str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' Naive') hopreds.append((hopred[0, oind], hopred[1, oind])) rpool = {} rpool['Y'] = trainlabels rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out(np.array([hoindices[0], 1, 1, 2]), np.array([hoindices[1], 2, 3, 3])) print(str(hopred_start[0][1]) + ' ' + str(hopred_end[0][1]) + ' Fast') hopreds.append((hopred_start[0][1], hopred_end[0][1])) self.assertAlmostEqual(hopreds[0][0], hopreds[1][0]) self.assertAlmostEqual(hopreds[0][1], hopreds[1][1]) #Test with single output just in case. rpool = {} rpool['Y'] = trainlabels[:, 1] rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out(np.array([hoindices[0], 1, 1, 2]), np.array([hoindices[1], 2, 3, 3])) #print(str(hopred_start[0]) + ' ' + str(hopred_end[0]) + ' Fast') #Test with single output just in case. rpool = {} rpool['Y'] = trainlabels[:, 1] rpool['X'] = Xtrain rpool['regparam'] = regparam hodualrls = GlobalRankRLS(**rpool) hodualrls.solve(regparam) hopred_start, hopred_end = hodualrls.leave_pair_out(np.array([hoindices[0]]), np.array([hoindices[1]])) #print(str(hopred_start) + ' ' + str(hopred_end) + ' Fast') hopreds = [] rpool = {} rpool['Y'] = trainlabels rpool['X'] = Xtrain rpool['regparam'] = regparam hoprimalrls = GlobalRankRLS(**rpool) hoprimalrls.solve(regparam) hopred = hoprimalrls.holdout(hoindices) print(str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' HO') hopreds.append((hopred[0, oind], hopred[1, oind])) hopred = Xtest * la.inv(Xcv.T * Lcv * Xcv + regparam * np.mat(np.eye(n))) * Xcv.T * Lcv * Ycv print(str(hopred[0, oind]) + ' ' + str(hopred[1, oind]) + ' Dumb (primal)') hopreds.append((hopred[0, oind], hopred[1, oind])) hopred0 = hopreds.pop(0) for hopred in hopreds: self.assertAlmostEqual(hopred0[0],hopred[0]) self.assertAlmostEqual(hopred0[1],hopred[1])