예제 #1
0
    def testStandardiseArray(self):
        numExamples = 10
        numFeatures = 3

        preprocessor = Standardiser()

        # Test an everyday matrix
        X = numpy.random.rand(numExamples, numFeatures)
        Xs = preprocessor.standardiseArray(X)

        self.assertAlmostEquals(numpy.sum(Xs), 0, places=3)
        self.assertAlmostEquals(numpy.sum(Xs * Xs), numFeatures, places=3)

        # Now, test on a portion of a matrix
        Xss = preprocessor.standardiseArray(X[1:5, :])
        self.assertTrue((Xss == Xs[1:5, :]).all())
예제 #2
0
    def testUnstandardiseArray(self):
        numExamples = 10
        numFeatures = 3

        tol = 10 ** -6
        preprocessor = Standardiser()

        # Test an everyday matrix
        X = numpy.random.rand(numExamples, numFeatures)
        Xs = preprocessor.standardiseArray(X)
        X2 = preprocessor.unstandardiseArray(Xs)

        self.assertTrue(numpy.linalg.norm(X2 - X) < tol)
    def testLearnModel(self):
        numExamples = 50
        numFeatures = 200
        preprocessor = Standardiser()
        X = numpy.random.randn(numExamples, numFeatures)
        X = preprocessor.standardiseArray(X)
        c = numpy.random.rand(numFeatures)
        y = numpy.dot(X, c)

        tol = 0.05
        kernel = LinearKernel()
        lmbda = 0.0001
        predictor = KernelShiftRegression(kernel, lmbda)

        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        self.assertTrue(Evaluator.rootMeanSqError(y, predY) < tol)

        #Try increasing y
        y = y + 5
        lmbda = 0.2
        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        self.assertTrue(numpy.abs(b - 5) < 0.1)
        self.assertTrue(Evaluator.rootMeanSqError(y, predY) < 0.1)

        #Try making prediction for multilabel Y
        C = numpy.random.rand(numFeatures, numFeatures)
        Y = numpy.dot(X, C)

        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, Y)
        predY = predictor.predict(X)

        self.assertTrue(Evaluator.rootMeanSqError(Y, predY) < 0.1)

        #Now, shift the data
        s = numpy.random.rand(numFeatures)
        Y = Y + s

        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, Y)
        predY = predictor.predict(X)

        self.assertTrue(numpy.linalg.norm(b - s) < 0.1)
        self.assertTrue(Evaluator.rootMeanSqError(Y, predY) < 0.1)
    def testLearnModel(self):
        numExamples = 50
        numFeatures = 200
        preprocessor = Standardiser()
        X = numpy.random.randn(numExamples, numFeatures)
        X = preprocessor.standardiseArray(X)
        c = numpy.random.rand(numFeatures)
        y = numpy.dot(X, c)

        tol = 0.05
        kernel = LinearKernel()
        lmbda = 0.0001
        predictor = KernelShiftRegression(kernel, lmbda)

        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        self.assertTrue(Evaluator.rootMeanSqError(y, predY) < tol)

        #Try increasing y
        y = y + 5
        lmbda = 0.2
        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        self.assertTrue(numpy.abs(b - 5) < 0.1)
        self.assertTrue(Evaluator.rootMeanSqError(y, predY) < 0.1)

        #Try making prediction for multilabel Y
        C = numpy.random.rand(numFeatures, numFeatures)
        Y = numpy.dot(X, C)

        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, Y)
        predY = predictor.predict(X)

        self.assertTrue(Evaluator.rootMeanSqError(Y, predY) < 0.1)

        #Now, shift the data 
        s = numpy.random.rand(numFeatures)
        Y = Y + s

        predictor = KernelShiftRegression(kernel, lmbda)
        alpha, b = predictor.learnModel(X, Y)
        predY = predictor.predict(X)

        self.assertTrue(numpy.linalg.norm(b - s) < 0.1)
        self.assertTrue(Evaluator.rootMeanSqError(Y, predY) < 0.1)
예제 #5
0
    def testClassify(self):
        numExamples = 10
        numFeatures = 20

        X = numpy.random.randn(numExamples, numFeatures)
        y = numpy.sign(numpy.random.randn(numExamples))
        logging.debug(y)

        preprocessor = Standardiser()
        X = preprocessor.standardiseArray(X)

        tol = 10**-5
        lmbda = 1.0
        kernel = LinearKernel()

        predictor = KernelRidgeRegression(kernel, lmbda)
        predictor.learnModel(X, y)
        classY, predY = predictor.classify(X)

        self.assertTrue(numpy.logical_or(classY == 1, classY == -1).all())
    def testLearnModel2(self):
        numExamples = 200
        numFeatures = 100

        X = numpy.random.randn(numExamples, numFeatures)
        y = numpy.random.randn(numExamples)

        preprocessor = Standardiser()
        X = preprocessor.standardiseArray(X)

        tol = 10**-3
        kernel = LinearKernel()

        #Try using a low-rank matrix
        lmbda = 0.001
        predictor = KernelShiftRegression(kernel, lmbda)

        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        logging.debug((numpy.linalg.norm(y)))
        logging.debug((numpy.linalg.norm(predY - y)))
    def testLearnModel2(self):
        numExamples = 200
        numFeatures = 100

        X = numpy.random.randn(numExamples, numFeatures)
        y = numpy.random.randn(numExamples)

        preprocessor = Standardiser()
        X = preprocessor.standardiseArray(X)

        tol = 10**-3
        kernel = LinearKernel()

        #Try using a low-rank matrix 
        lmbda = 0.001
        predictor = KernelShiftRegression(kernel, lmbda)

        alpha, b = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        logging.debug((numpy.linalg.norm(y)))
        logging.debug((numpy.linalg.norm(predY - y)))
예제 #8
0
    def testLearnModel(self):
        numExamples = 50
        numFeatures = 200

        X = numpy.random.randn(numExamples, numFeatures)
        y = numpy.random.randn(numExamples)

        preprocessor = Standardiser()
        X = preprocessor.standardiseArray(X)

        tol = 10**-3
        kernel = LinearKernel()

        #Compare Linear kernel with linear ridge regression
        lmbda = 0.1
        predictor = KernelRidgeRegression(kernel, lmbda)

        alpha = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        K = numpy.dot(X, X.T)
        alpha2 = numpy.dot(
            numpy.linalg.inv(K + lmbda * numpy.eye(numExamples)), y)
        predY2 = X.dot(
            numpy.linalg.inv(
                numpy.dot(X.T, X) + lmbda * numpy.eye(numFeatures))).dot(
                    X.T).dot(y)

        #logging.debug(numpy.linalg.norm(alpha - alpha2))

        self.assertTrue(numpy.linalg.norm(alpha - alpha2) < tol)
        self.assertTrue(numpy.linalg.norm(predY - predY2) < tol)

        lmbda = 0.5
        predictor = KernelRidgeRegression(kernel, lmbda)

        alpha = predictor.learnModel(X, y)
        predY = predictor.predict(X)

        K = numpy.dot(X, X.T)
        alpha2 = numpy.dot(
            numpy.linalg.inv(K + lmbda * numpy.eye(numExamples)), y)
        predY2 = X.dot(
            numpy.linalg.inv(
                numpy.dot(X.T, X) + lmbda * numpy.eye(numFeatures))).dot(
                    X.T).dot(y)

        self.assertTrue(numpy.linalg.norm(alpha - alpha2) < tol)
        self.assertTrue(numpy.linalg.norm(predY - predY2) < tol)

        #Now test on an alternative test set
        numTestExamples = 50
        testX = numpy.random.randn(numTestExamples, numFeatures)
        predictor = KernelRidgeRegression(kernel, lmbda)

        alpha = predictor.learnModel(X, y)
        predY = predictor.predict(testX)

        K = numpy.dot(X, X.T)
        alpha2 = numpy.dot(
            numpy.linalg.inv(K + lmbda * numpy.eye(numExamples)), y)
        predY2 = testX.dot(
            numpy.linalg.inv(
                numpy.dot(X.T, X) + lmbda * numpy.eye(numFeatures))).dot(
                    X.T).dot(y)

        self.assertTrue(numpy.linalg.norm(alpha - alpha2) < tol)
        self.assertTrue(numpy.linalg.norm(predY - predY2) < tol)

        #Use the method against a multi-label example
        Y = numpy.random.randn(numExamples, numFeatures)

        alpha = predictor.learnModel(X, Y)

        self.assertTrue(alpha.shape == (numExamples, numFeatures))