def train_rls(): #Trains RLS with default parameters (regparam=1.0, kernel='LinearKernel') X_train, Y_train, X_test, Y_test = load_housing() learner = GlobalRankRLS(X_train, Y_train) #Test set predictions P_test = learner.predict(X_test) print("test cindex %f" %cindex(Y_test, P_test))
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))