def execute(self, experiment: Experiment) -> ExperimentPerformance: """ Execute K-fold cross-validation with separate test set with the given classifier on the given data set using the given parameters. PARAMETERS ---------- experiment : Experiment Experiment to be run. RETURNS ------- ExperimentPerformance An ExperimentPerformance instance. """ result = ExperimentPerformance() instanceList = experiment.getDataSet().getInstanceList() partition = Partition(instanceList, 0.25, experiment.getParameter().getSeed(), True) crossValidation = KFoldCrossValidation( partition.get(1).getInstances(), self.K, experiment.getParameter().getSeed()) self.runExperiment(experiment.getClassifier(), experiment.getParameter(), result, crossValidation, partition.get(0)) return result
def execute(self, experiment: Experiment) -> ExperimentPerformance: result = ExperimentPerformance() for j in range(self.M): instanceList = experiment.getDataSet().getInstanceList() partition = Partition(instanceList, 0.25, experiment.getParameter().getSeed(), True) crossValidation = StratifiedKFoldCrossValidation( Partition(partition.get(1)).getLists(), self.K, experiment.getParameter().getSeed()) self.runExperiment(experiment.getClassifier(), experiment.getParameter(), result, crossValidation, partition.get(0)) return result
def execute(self, experiment: Experiment) -> Performance: """ Execute Single K-fold cross-validation with the given classifier on the given data set using the given parameters. PARAMETERS ----- experiment : Experiment Experiment to be run. RETURNS ------- Performance A Performance instance. """ crossValidation = KFoldCrossValidation(experiment.getDataSet().getInstances(), self.__K, experiment.getParameter().getSeed()) return self.runExperiment(experiment.getClassifier(), experiment.getParameter(), crossValidation)
def test_SubSetSelectionLda(self): kFoldRun = KFoldRun(10) forwardSelection = ForwardSelection() experiment = Experiment(Lda(), Parameter(1), self.dermatology) self.assertAlmostEqual(11, forwardSelection.execute(kFoldRun, experiment).size()) backwardSelection = BackwardSelection(self.dermatology.attributeCount()) self.assertAlmostEqual(33, backwardSelection.execute(kFoldRun, experiment).size()) floatingSelection = FloatingSelection() self.assertAlmostEqual(11, floatingSelection.execute(kFoldRun, experiment).size())
def test_SubSetSelectionKnn(self): kFoldRun = KFoldRun(10) forwardSelection = ForwardSelection() experiment = Experiment(Knn(), KnnParameter(1, 3), self.car) self.assertAlmostEqual(5, forwardSelection.execute(kFoldRun, experiment).size()) backwardSelection = BackwardSelection(self.car.attributeCount()) self.assertAlmostEqual(5, backwardSelection.execute(kFoldRun, experiment).size()) floatingSelection = FloatingSelection() self.assertAlmostEqual(5, floatingSelection.execute(kFoldRun, experiment).size())
def test_SubSetSelectionC45(self): kFoldRun = KFoldRun(10) forwardSelection = ForwardSelection() experiment = Experiment(C45(), C45Parameter(1, True, 0.2), self.iris) self.assertAlmostEqual(1, forwardSelection.execute(kFoldRun, experiment).size()) backwardSelection = BackwardSelection(self.iris.attributeCount()) self.assertAlmostEqual(3, backwardSelection.execute(kFoldRun, experiment).size()) floatingSelection = FloatingSelection() self.assertAlmostEqual(1, floatingSelection.execute(kFoldRun, experiment).size())
def test_SubSetSelectionNaiveBayes(self): kFoldRun = KFoldRun(10) forwardSelection = ForwardSelection() experiment = Experiment(NaiveBayes(), Parameter(1), self.nursery) self.assertAlmostEqual(3, forwardSelection.execute(kFoldRun, experiment).size()) backwardSelection = BackwardSelection(self.nursery.attributeCount()) self.assertAlmostEqual(8, backwardSelection.execute(kFoldRun, experiment).size()) floatingSelection = FloatingSelection() self.assertAlmostEqual(3, floatingSelection.execute(kFoldRun, experiment).size())
def execute(self, experiment: Experiment) -> ExperimentPerformance: """ Execute Stratified K-fold cross-validation with the given classifier on the given data set using the given parameters. PARAMETERS ---------- experiment : Experiment Experiment to be run. RETURNS ------- ExperimentPerformance An ExperimentPerformance instance. """ result = ExperimentPerformance() crossValidation = StratifiedKFoldCrossValidation(experiment.getDataSet().getClassInstances(), self.K, experiment.getParameter().getSeed()) self.runExperiment(experiment.getClassifier(), experiment.getParameter(), result, crossValidation) return result
def execute(self, experiment: Experiment) -> Performance: """ Execute Stratified Single K-fold cross-validation with the given classifier on the given data set using the given parameters. PARAMETERS ---------- experiment : Experiment Experiment to be run. RETURNS ------- Performance A Performance instance. """ crossValidation = StratifiedKFoldCrossValidation( experiment.getDataSet().getClassInstances(), self.__K, experiment.getParameter().getSeed()) trainSet = InstanceList(crossValidation.getTrainFold(0)) testSet = InstanceList(crossValidation.getTestFold(0)) return experiment.getClassifier().singleRun(experiment.getParameter(), trainSet, testSet)
def execute(self, experiment: Experiment) -> ExperimentPerformance: """ Execute the bootstrap run with the given classifier on the given data set using the given parameters. PARAMETERS ---------- experiment : Experiment Experiment to be run. RETURNS ------- ExperimentPerformance An ExperimentPerformance instance. """ result = ExperimentPerformance() for i in range(self.__numberOfBootstraps): bootstrap = Bootstrap(experiment.getDataSet().getInstances(), i + experiment.getParameter().getSeed()) bootstrapSample = InstanceList(bootstrap.getSample()) experiment.getClassifier().train(bootstrapSample, experiment.getParameter()) result.add(experiment.getClassifier().test( experiment.getDataSet().getInstanceList())) return result
def test_Compare(self): mxKFoldRun = MxKFoldRun(5, 2) combined5x2t = Combined5x2t() experimentPerformance1 = mxKFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) experimentPerformance2 = mxKFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual( 0.186, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 3) experimentPerformance1 = mxKFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) experimentPerformance2 = mxKFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual( 0.0000059, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 7) experimentPerformance1 = mxKFoldRun.execute( Experiment(Lda(), Parameter(1), self.dermatology)) experimentPerformance2 = mxKFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual( 0.9819, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4) experimentPerformance1 = mxKFoldRun.execute( Experiment(Dummy(), Parameter(1), self.nursery)) experimentPerformance2 = mxKFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual( 0.0, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4) experimentPerformance1 = mxKFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.car)) experimentPerformance2 = mxKFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual( 0.00043, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 5) experimentPerformance1 = mxKFoldRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) experimentPerformance2 = mxKFoldRun.execute( Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual( 0.0663, combined5x2t.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4)
def test_Execute(self): mxKFoldRun = MxKFoldRun(5, 2) experimentPerformance = mxKFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) self.assertAlmostEqual( 6.13, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) self.assertAlmostEqual( 23.51, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) self.assertAlmostEqual( 37.05, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.dermatology)) self.assertAlmostEqual( 15.41, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual( 34.72, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Lda(), Parameter(1), self.dermatology)) self.assertAlmostEqual( 4.04, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual( 5.2, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual( 5.46, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.car)) self.assertAlmostEqual( 16.52, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual( 9.80, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual( 8.77, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual( 9.77, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Dummy(), Parameter(1), self.nursery)) self.assertAlmostEqual( 67.09, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = mxKFoldRun.execute( Experiment(Dummy(), Parameter(1), self.iris)) self.assertAlmostEqual( 70.53, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2)
def test_Compare(self): kFoldRun = KFoldRun(10) pairedt = Pairedt() experimentPerformance1 = kFoldRun.execute(Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) experimentPerformance2 = kFoldRun.execute(Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual(0.379, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 3) experimentPerformance1 = kFoldRun.execute(Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) experimentPerformance2 = kFoldRun.execute(Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual(0.00000692, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 7) experimentPerformance1 = kFoldRun.execute(Experiment(Lda(), Parameter(1), self.dermatology)) experimentPerformance2 = kFoldRun.execute(Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual(0.7842, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4) experimentPerformance1 = kFoldRun.execute(Experiment(Dummy(), Parameter(1), self.nursery)) experimentPerformance2 = kFoldRun.execute(Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual(0.0, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4) experimentPerformance1 = kFoldRun.execute(Experiment(NaiveBayes(), Parameter(1), self.car)) experimentPerformance2 = kFoldRun.execute(Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual(0.00000336, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 7) experimentPerformance1 = kFoldRun.execute(Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) experimentPerformance2 = kFoldRun.execute(Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual(0.1640, pairedt.compare(experimentPerformance1, experimentPerformance2).getPValue(), 4)
def test_Execute(self): kFoldRun = KFoldRun(10) experimentPerformance = kFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) self.assertAlmostEqual( 6.00, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) self.assertAlmostEqual( 18.78, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) self.assertAlmostEqual( 36.85, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.dermatology)) self.assertAlmostEqual( 10.92, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual( 31.61, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Lda(), Parameter(1), self.dermatology)) self.assertAlmostEqual( 3.30, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual( 5.33, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual( 3.81, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.car)) self.assertAlmostEqual( 14.88, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual( 9.71, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual( 3.55, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual( 6.77, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Dummy(), Parameter(1), self.nursery)) self.assertAlmostEqual( 67.12, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = kFoldRun.execute( Experiment(Dummy(), Parameter(1), self.iris)) self.assertAlmostEqual( 79.33, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2)
def test_Execute(self): stratifiedMxKRun = StratifiedMxKFoldRun(5, 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) self.assertAlmostEqual( 8.00, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) self.assertAlmostEqual( 22.03, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) self.assertAlmostEqual( 33.33, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.dermatology)) self.assertAlmostEqual( 13.66, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual( 33.05, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Lda(), Parameter(1), self.dermatology)) self.assertAlmostEqual( 3.55, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual( 6.00, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual( 4.66, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(NaiveBayes(), Parameter(1), self.car)) self.assertAlmostEqual( 15.11, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual( 9.68, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual( 9.29, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual( 9.20, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Dummy(), Parameter(1), self.nursery)) self.assertAlmostEqual( 66.67, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = stratifiedMxKRun.execute( Experiment(Dummy(), Parameter(1), self.iris)) self.assertAlmostEqual( 66.67, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2)
def test_Execute(self): bootstrapRun = BootstrapRun(10) experimentPerformance = bootstrapRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.iris)) self.assertAlmostEqual( 4.33, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(C45(), C45Parameter(1, True, 0.2), self.tictactoe)) self.assertAlmostEqual( 13.16, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.bupa)) self.assertAlmostEqual( 24.84, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Knn(), KnnParameter(1, 3, EuclidianDistance()), self.dermatology)) self.assertAlmostEqual( 8.01, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Lda(), Parameter(1), self.bupa)) self.assertAlmostEqual( 32.03, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Lda(), Parameter(1), self.dermatology)) self.assertAlmostEqual( 2.95, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.iris)) self.assertAlmostEqual( 3.27, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(LinearPerceptron(), LinearPerceptronParameter(1, 0.1, 0.99, 0.2, 100), self.dermatology)) self.assertAlmostEqual( 2.65, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(NaiveBayes(), Parameter(1), self.car)) self.assertAlmostEqual( 14.75, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(NaiveBayes(), Parameter(1), self.nursery)) self.assertAlmostEqual( 9.71, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.tictactoe)) self.assertAlmostEqual( 3.00, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Bagging(), BaggingParameter(1, 50), self.car)) self.assertAlmostEqual( 3.44, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Dummy(), Parameter(1), self.nursery)) self.assertAlmostEqual( 66.79, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2) experimentPerformance = bootstrapRun.execute( Experiment(Dummy(), Parameter(1), self.iris)) self.assertAlmostEqual( 66.67, 100 * experimentPerformance.meanPerformance().getErrorRate(), 2)