Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
    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])
Beispiel #4
0
 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])