public void TestSmallSample2Fold()
        {
            var stratifiedKFoldCrossValidation =
                new StratifiedKFoldCrossValidation <string>(smallSample, 2, 1);

            string[] expected3 = { "7", "9", "5", "2", "10", "20", "26", "16", "22", "14", "27", "25", "28", "29", "11" };
            Assert.AreEqual(expected3, stratifiedKFoldCrossValidation.GetTestFold(0).ToArray());
        }
        public void TestSmallSample5Fold()
        {
            var stratifiedKFoldCrossValidation =
                new StratifiedKFoldCrossValidation <string>(smallSample, 5, 1);

            string[] expected2 = { "7", "9", "20", "26", "16", "22" };
            Assert.AreEqual(expected2, stratifiedKFoldCrossValidation.GetTestFold(0).ToArray());
        }
        /**
         * <summary> Execute Stratified Single K-fold cross-validation with the given classifier on the given data set using the given parameters.</summary>
         *
         * <param name="experiment">Experiment to be run.</param>
         * <returns>A Performance instance.</returns>
         */
        public Performance.Performance Execute(Experiment experiment)
        {
            var crossValidation =
                new StratifiedKFoldCrossValidation <Instance.Instance>(experiment.GetDataSet().GetClassInstances(), _k,
                                                                       experiment.GetParameter().GetSeed());
            var trainSet = new InstanceList.InstanceList(crossValidation.GetTrainFold(0));
            var testSet  = new InstanceList.InstanceList(crossValidation.GetTestFold(0));

            return(experiment.GetClassifier().SingleRun(experiment.GetParameter(), trainSet, testSet));
        }
Exemplo n.º 4
0
        /**
         * <summary> Execute Stratified K-fold cross-validation with the given classifier on the given data set using the given parameters.</summary>
         *
         * <param name="experiment">Experiment to be run.</param>
         * <returns>An ExperimentPerformance instance.</returns>
         */
        public override ExperimentPerformance Execute(Experiment experiment)
        {
            var result          = new ExperimentPerformance();
            var crossValidation =
                new StratifiedKFoldCrossValidation <Instance.Instance>(experiment.GetDataSet().GetClassInstances(), K,
                                                                       experiment.GetParameter().GetSeed());

            RunExperiment(experiment.GetClassifier(), experiment.GetParameter(), result, crossValidation);
            return(result);
        }
        /**
         * <summary> Execute Stratified K-fold cross-validation with the given classifier on the given data set using the given parameters.</summary>
         *
         * <param name="experiment">Experiment to be run.</param>
         * <returns>An ExperimentPerformance instance.</returns>
         */
        public override ExperimentPerformance Execute(Experiment experiment)
        {
            var result          = new ExperimentPerformance();
            var instanceList    = experiment.GetDataSet().GetInstanceList();
            var partition       = instanceList.Partition(0.25, new Random(experiment.GetParameter().GetSeed()));
            var crossValidation =
                new StratifiedKFoldCrossValidation <Instance.Instance>(partition.Get(1).DivideIntoClasses().GetLists(),
                                                                       K, experiment.GetParameter().GetSeed());

            RunExperiment(experiment.GetClassifier(), experiment.GetParameter(), result, crossValidation,
                          partition.Get(0));
            return(result);
        }
        public void TestLargeSample10Fold()
        {
            var stratifiedKFoldCrossValidation =
                new StratifiedKFoldCrossValidation <int>(largeSample, 10, 1);

            for (var i = 0; i < 10; i++)
            {
                var items     = new HashSet <int>();
                var trainFold = stratifiedKFoldCrossValidation.GetTrainFold(i);
                var testFold  = stratifiedKFoldCrossValidation.GetTestFold(i);
                items.UnionWith(trainFold);
                items.UnionWith(testFold);
                Assert.AreEqual(900, testFold.Count);
                Assert.AreEqual(8100, trainFold.Count);
                Assert.AreEqual(9000, items.Count);
                var trainCounts = new int[3];
                foreach (var integer in trainFold)
                {
                    if (integer < 1000)
                    {
                        trainCounts[0]++;
                    }
                    else
                    {
                        if (integer < 4000)
                        {
                            trainCounts[1]++;
                        }
                        else
                        {
                            trainCounts[2]++;
                        }
                    }
                }
                Assert.AreEqual(900, trainCounts[0]);
                Assert.AreEqual(2700, trainCounts[1]);
                Assert.AreEqual(4500, trainCounts[2]);
                var testCounts = new int[3];
                foreach (var integer in testFold)
                {
                    if (integer < 1000)
                    {
                        testCounts[0]++;
                    }
                    else
                    {
                        if (integer < 4000)
                        {
                            testCounts[1]++;
                        }
                        else
                        {
                            testCounts[2]++;
                        }
                    }
                }
                Assert.AreEqual(100, testCounts[0]);
                Assert.AreEqual(300, testCounts[1]);
                Assert.AreEqual(500, testCounts[2]);
            }
        }