Exemplo n.º 1
0
 def setUp(self):
     level = 2
     dim = 2
     l = 0.00001
     self.classifier = Classifier()
     dataContainer = ARFFAdapter(
         pathlocal + "/datasets/classifier.train.arff").loadData()
     self.classifier.setDataContainer(dataContainer)
     foldingPolicy = FoldingPolicy(dataContainer)
     self.classifier.setFoldingPolicy(foldingPolicy)
     grid = Grid.createLinearGrid(dim)
     storage = grid.createGridGenerator()
     storage.regular(level)
     self.classifier.setGrid(grid)
     self.classifier.setLearnedKnowledge(LearnedKnowledge())
     spec = TrainingSpecification()
     spec.setL(l)
     spec.setCOperator(createOperationLaplace(grid))
     self.classifier.setSpecification(spec)
     stopPolicy = TrainingStopPolicy()
     stopPolicy.setAdaptiveIterationLimit(0)
     self.classifier.setStopPolicy(stopPolicy)
     solver = CGSolver()
     #solver.attachEventController(InfoToScreen())
     solver.setImax(500)
     solver.setReuse(True)
     self.classifier.setSolver(solver)
Exemplo n.º 2
0
 def setUp(self):
     level = 2
     dim = 2
     l = 0.00001
     self.classifier = Classifier()
     dataContainer = ARFFAdapter(pathlocal + "/datasets/classifier.train.arff").loadData()
     self.classifier.setDataContainer(dataContainer)
     foldingPolicy = FoldingPolicy(dataContainer)
     self.classifier.setFoldingPolicy(foldingPolicy)
     grid = Grid.createLinearGrid(dim)
     storage = grid.createGridGenerator()
     storage.regular(level)
     self.classifier.setGrid(grid)
     self.classifier.setLearnedKnowledge(LearnedKnowledge())
     spec = TrainingSpecification()
     spec.setL(l)
     spec.setCOperator(createOperationLaplace(grid))
     self.classifier.setSpecification(spec)
     stopPolicy = TrainingStopPolicy()
     stopPolicy.setAdaptiveIterationLimit(0)
     self.classifier.setStopPolicy(stopPolicy)
     solver = CGSolver()
     #solver.attachEventController(InfoToScreen())
     solver.setImax(500)
     solver.setReuse(True)
     self.classifier.setSolver(solver)
    def __loadLearner(self, iteration):
        # read data from file
        learnerFilename = self.composeName(iteration, self.fold) + ".learner.gz"

        learnerMemento = LearnerFormatter().deserializeFromFile(learnerFilename)

        # reconstruct the object
        learnerType = learnerMemento['module']
        if "bin.learner.Classifier" in learnerType:
            learner = Classifier()
        elif "bin.learner.Regressor" in learnerType:
            learner = Regressor()
        else:
            raise Exception("module name unknown")
        learner.setMemento(learnerMemento)
        return learner
Exemplo n.º 4
0
    def __loadLearner(self, iteration):
        # read data from file
        learnerFilename = self.composeName(iteration,
                                           self.fold) + ".learner.gz"

        learnerMemento = LearnerFormatter().deserializeFromFile(
            learnerFilename)

        # reconstruct the object
        learnerType = learnerMemento['module']
        if "bin.learner.Classifier" in learnerType:
            learner = Classifier()
        elif "bin.learner.Regressor" in learnerType:
            learner = Regressor()
        else:
            raise Exception("module name unknown")
        learner.setMemento(learnerMemento)
        return learner
Exemplo n.º 5
0
    def testLearnDataWithFolding(self, ):
        correct = [
            0.6612903226, 0.1428571429, 0.5741935484, 0.9142857143,
            0.6193548387, 0.5142857143, 0.5870967742, 0.7714285714,
            0.6032258065, 0.5714285714, 0.6387096774, 0.4000000000,
            0.5935483871, 0.7428571429, 0.6193548387, 0.5142857143,
            0.5903225806, 0.7714285714, 0.6063492063, 0.5666666667
        ]
        level = 2
        dim = 6
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(
            pathlocal +
            "/../../../datasets/liver/liver-disorders_normalized.arff"
        ).loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = SequentialFoldingPolicy(dataContainer, 10)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.getGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        spec.setCOperatorType('laplace')
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        self.classifier.setSolver(CGSolver())

        self.classifier.learnDataWithFolding()
        for i in range(10):
            self.assertAlmostEqual(correct[2 * i],
                                   self.classifier.trainAccuracy[i])
            self.assertAlmostEqual(correct[2 * i + 1],
                                   self.classifier.testAccuracy[i])
Exemplo n.º 6
0
    def testLearnDataWithFolding(self,):
        correct = [0.6612903226, 0.1428571429,
                   0.5741935484, 0.9142857143,
                   0.6193548387, 0.5142857143,
                   0.5870967742, 0.7714285714,
                   0.6032258065, 0.5714285714,
                   0.6387096774, 0.4000000000,
                   0.5935483871, 0.7428571429,
                   0.6193548387, 0.5142857143,
                   0.5903225806, 0.7714285714,
                   0.6063492063, 0.5666666667]
        level = 2
        dim = 6
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(pathlocal + "/datasets/liver-disorders_normalized.arff").loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = SequentialFoldingPolicy(dataContainer, 10)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.createGridGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        self.classifier.setSolver(CGSolver())

        self.classifier.learnDataWithFolding()
        for i in xrange(10):
            self.assertAlmostEqual(correct[2*i], self.classifier.trainAccuracy[i])
            self.assertAlmostEqual(correct[2*i+1], self.classifier.testAccuracy[i])
Exemplo n.º 7
0
class TestClassifier(unittest.TestCase):

    ## Set up the variables
    def setUp(self):
        level = 2
        dim = 2
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(
            pathlocal + "/datasets/classifier.train.arff").loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = FoldingPolicy(dataContainer)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.createGridGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        solver = CGSolver()
        #solver.attachEventController(InfoToScreen())
        solver.setImax(500)
        solver.setReuse(True)
        self.classifier.setSolver(solver)


#     ##
#     # Tests the function @link python.learner.Learner.Learner.learnDataWithTest() Classifier.learnDataWithTest() @endlink
#     def testLearnDataWithTest(self,):
#         correct = [-0.33360635579319858346,
#                    0.67890792146517364714,
#                    17.37781054927400248289,
#                    19.86707480839170614217,
#                    -1.61960456623131343612,
#                    -7.70442957659598182119,
#                    -22.14900166819601423640,
#                    8.92745373469135827804,
#                    -11.12477960213342775830
#                    ]
#         testDataset = ARFFAdapter(pathlocal + "/datasets/classifier.test.arff").loadData("test")
#         self.classifier.setDataContainer(self.classifier.dataContainer.combine(testDataset))
#         self.classifier.stopPolicy.setAdaptiveIterationLimit(1)
#         self.classifier.specification.setAdaptPoints(1)
#         alpha = self.classifier.learnDataWithTest(self.classifier.dataContainer.combine(testDataset))
#         self.assertEqual(len(alpha), len(correct))
#         for i in xrange(len(correct)):
#             self.assertAlmostEqual(alpha[i], correct[i], 3)

##
# Tests the function @link python.learner.Learner.Learner.applyData() Classifier.applyData() @endlink

    def testApplyData(self, ):
        correct = [0.253400605292, -0.25507958758, 0.0530555506998]
        points = [[0.5, 0.1], [0.3, 0.4], [0.9, 0.7]]
        self.classifier.learnData()
        data = DataMatrix(3, 2)
        for i in xrange(3):
            temp = DataVector(2)
            temp[0] = points[i][0]
            temp[1] = points[i][1]
            data.setRow(i, temp)

        val = self.classifier.applyData(data)
        places = 7 if cvar.USING_DOUBLE_PRECISION else 6

        self.assertEqual(len(val), len(correct))
        for i in xrange(len(correct)):
            self.assertAlmostEqual(val[i], correct[i], places=places)

    ##
    # Tests the function @link python.learner.Learner.Learner.learnData() Classifier.learnData() @endlink
    def testLearnData(self):
        correct = [
            -0.03105750236900508068, -0.61865507797281660274,
            0.64903026441541222802, 0.64903026441541267211,
            -0.61865507797281593660
        ]

        alpha = self.classifier.learnData()
        places = 7 if cvar.USING_DOUBLE_PRECISION else 6
        for i in xrange(len(alpha)):
            self.assertAlmostEqual(correct[i], alpha[i], places=places)

    ##
    # Tests the function @link python.learner.Learner.Learner.learnDataWithFolding() Classifier.learnDataWithFolding() @endlink
    def testLearnDataWithFolding(self, ):
        correct = [
            0.6612903226, 0.1428571429, 0.5741935484, 0.9142857143,
            0.6193548387, 0.5142857143, 0.5870967742, 0.7714285714,
            0.6032258065, 0.5714285714, 0.6387096774, 0.4000000000,
            0.5935483871, 0.7428571429, 0.6193548387, 0.5142857143,
            0.5903225806, 0.7714285714, 0.6063492063, 0.5666666667
        ]
        level = 2
        dim = 6
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(
            pathlocal +
            "/datasets/liver-disorders_normalized.arff").loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = SequentialFoldingPolicy(dataContainer, 10)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.createGridGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        self.classifier.setSolver(CGSolver())

        self.classifier.learnDataWithFolding()
        for i in xrange(10):
            self.assertAlmostEqual(correct[2 * i],
                                   self.classifier.trainAccuracy[i])
            self.assertAlmostEqual(correct[2 * i + 1],
                                   self.classifier.testAccuracy[i])
Exemplo n.º 8
0
class TestClassifier(unittest.TestCase):

    ## Set up the variables
    def setUp(self):
        level = 2
        dim = 2
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(pathlocal + "/datasets/classifier.train.arff").loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = FoldingPolicy(dataContainer)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.createGridGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        solver = CGSolver()
        #solver.attachEventController(InfoToScreen())
        solver.setImax(500)
        solver.setReuse(True)
        self.classifier.setSolver(solver)

#     ##
#     # Tests the function @link python.learner.Learner.Learner.learnDataWithTest() Classifier.learnDataWithTest() @endlink
#     def testLearnDataWithTest(self,):
#         correct = [-0.33360635579319858346,
#                    0.67890792146517364714,
#                    17.37781054927400248289,
#                    19.86707480839170614217,
#                    -1.61960456623131343612,
#                    -7.70442957659598182119,
#                    -22.14900166819601423640,
#                    8.92745373469135827804,
#                    -11.12477960213342775830
#                    ]
#         testDataset = ARFFAdapter(pathlocal + "/datasets/classifier.test.arff").loadData("test")
#         self.classifier.setDataContainer(self.classifier.dataContainer.combine(testDataset))
#         self.classifier.stopPolicy.setAdaptiveIterationLimit(1)
#         self.classifier.specification.setAdaptPoints(1)
#         alpha = self.classifier.learnDataWithTest(self.classifier.dataContainer.combine(testDataset))
#         self.assertEqual(len(alpha), len(correct))
#         for i in xrange(len(correct)):
#             self.assertAlmostEqual(alpha[i], correct[i], 3)

    ##
    # Tests the function @link python.learner.Learner.Learner.applyData() Classifier.applyData() @endlink
    def testApplyData(self,):
        correct = [0.253400605292, -0.25507958758, 0.0530555506998]
        points = [[0.5, 0.1], [0.3, 0.4], [0.9, 0.7]]
        self.classifier.learnData()
        data = DataMatrix(3,2)
        for i in xrange(3):
            temp = DataVector(2)
            temp[0] = points[i][0]
            temp[1] = points[i][1]
            data.setRow(i, temp)

        val = self.classifier.applyData(data)
        places = 7 if cvar.USING_DOUBLE_PRECISION else 6

        self.assertEqual(len(val), len(correct))
        for i in xrange(len(correct)):
            self.assertAlmostEqual(val[i], correct[i], places=places)

    ##
    # Tests the function @link python.learner.Learner.Learner.learnData() Classifier.learnData() @endlink
    def testLearnData(self):
        correct = [-0.03105750236900508068,
                   -0.61865507797281660274,
                   0.64903026441541222802,
                   0.64903026441541267211,
                   -0.61865507797281593660]

        alpha = self.classifier.learnData()
        places = 7 if cvar.USING_DOUBLE_PRECISION else 6
        for i in xrange(len(alpha)):
            self.assertAlmostEqual(correct[i], alpha[i], places=places)

    ##
    # Tests the function @link python.learner.Learner.Learner.learnDataWithFolding() Classifier.learnDataWithFolding() @endlink
    def testLearnDataWithFolding(self,):
        correct = [0.6612903226, 0.1428571429,
                   0.5741935484, 0.9142857143,
                   0.6193548387, 0.5142857143,
                   0.5870967742, 0.7714285714,
                   0.6032258065, 0.5714285714,
                   0.6387096774, 0.4000000000,
                   0.5935483871, 0.7428571429,
                   0.6193548387, 0.5142857143,
                   0.5903225806, 0.7714285714,
                   0.6063492063, 0.5666666667]
        level = 2
        dim = 6
        l = 0.00001
        self.classifier = Classifier()
        dataContainer = ARFFAdapter(pathlocal + "/datasets/liver-disorders_normalized.arff").loadData()
        self.classifier.setDataContainer(dataContainer)
        foldingPolicy = SequentialFoldingPolicy(dataContainer, 10)
        self.classifier.setFoldingPolicy(foldingPolicy)
        grid = Grid.createLinearGrid(dim)
        storage = grid.createGridGenerator()
        storage.regular(level)
        self.classifier.setGrid(grid)
        self.classifier.setLearnedKnowledge(LearnedKnowledge())
        spec = TrainingSpecification()
        spec.setL(l)
        spec.setCOperator(createOperationLaplace(grid))
        self.classifier.setSpecification(spec)
        stopPolicy = TrainingStopPolicy()
        stopPolicy.setAdaptiveIterationLimit(0)
        self.classifier.setStopPolicy(stopPolicy)
        self.classifier.setSolver(CGSolver())

        self.classifier.learnDataWithFolding()
        for i in xrange(10):
            self.assertAlmostEqual(correct[2*i], self.classifier.trainAccuracy[i])
            self.assertAlmostEqual(correct[2*i+1], self.classifier.testAccuracy[i])