Free cookie consent management tool by TermsFeed Policy Generator

Changeset 708


Ignore:
Timestamp:
11/02/08 22:42:40 (15 years ago)
Author:
gkronber
Message:

added a check for the value of nFolds to fix #313 (CrossValidation operator doesn't work with fold=1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/CrossValidation.cs

    r668 r708  
    2828using HeuristicLab.DataAnalysis;
    2929
    30 namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class CrossValidation : OperatorBase {
     30namespace HeuristicLab.GP.StructureIdentification.Classification
     31{
     32    public class CrossValidation : OperatorBase
     33    {
    3234
    33     private const string DATASET = "Dataset";
    34     private const string NFOLD = "n-Fold";
    35     private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
    36     private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
    37     private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
    38     private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
    39     private const string TESTSAMPLESSTART = "TestSamplesStart";
    40     private const string TESTSAMPLESEND = "TestSamplesEnd";
     35        private const string DATASET = "Dataset";
     36        private const string NFOLD = "n-Fold";
     37        private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
     38        private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
     39        private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
     40        private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
     41        private const string TESTSAMPLESSTART = "TestSamplesStart";
     42        private const string TESTSAMPLESEND = "TestSamplesEnd";
    4143
    42     public override string Description {
    43       get { return @"TASK"; }
    44     }
     44        public override string Description
     45        {
     46            get { return @"TASK"; }
     47        }
    4548
    46     public CrossValidation()
    47       : base() {
    48       AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new datasets in the newly created subscopes", typeof(Dataset), VariableKind.In));
    49       AddVariableInfo(new VariableInfo(NFOLD, "Number of folds for the cross-validation", typeof(IntData), VariableKind.In));
    50       AddVariableInfo(new VariableInfo(TRAININGSAMPLESSTART, "The start of training samples in the original dataset and starts of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
    51       AddVariableInfo(new VariableInfo(TRAININGSAMPLESEND, "The end of training samples in the original dataset and ends of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
    52       AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESSTART, "The start of validation samples in the original dataset and starts of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
    53       AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESEND, "The end of validation samples in the original dataset and ends of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
    54       AddVariableInfo(new VariableInfo(TESTSAMPLESSTART, "The start of the test samples in the new datasets", typeof(IntData), VariableKind.New));
    55       AddVariableInfo(new VariableInfo(TESTSAMPLESEND, "The end of the test samples in the new datasets", typeof(IntData), VariableKind.New));
    56     }
     49        public CrossValidation()
     50            : base()
     51        {
     52            AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new datasets in the newly created subscopes", typeof(Dataset), VariableKind.In));
     53            AddVariableInfo(new VariableInfo(NFOLD, "Number of folds for the cross-validation", typeof(IntData), VariableKind.In));
     54            AddVariableInfo(new VariableInfo(TRAININGSAMPLESSTART, "The start of training samples in the original dataset and starts of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
     55            AddVariableInfo(new VariableInfo(TRAININGSAMPLESEND, "The end of training samples in the original dataset and ends of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
     56            AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESSTART, "The start of validation samples in the original dataset and starts of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
     57            AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESEND, "The end of validation samples in the original dataset and ends of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
     58            AddVariableInfo(new VariableInfo(TESTSAMPLESSTART, "The start of the test samples in the new datasets", typeof(IntData), VariableKind.New));
     59            AddVariableInfo(new VariableInfo(TESTSAMPLESEND, "The end of the test samples in the new datasets", typeof(IntData), VariableKind.New));
     60        }
    5761
    58     public override IOperation Apply(IScope scope) {
     62        public override IOperation Apply(IScope scope) {
    5963      Dataset origDataset = GetVariableValue<Dataset>(DATASET, scope, true);
    6064      int nFolds = GetVariableValue<IntData>(NFOLD, scope, true).Data;
     65      if (nFolds < 2) throw new ArgumentException("The number of folds (nFolds) has to be >=2 for cross validation");
    6166      int origTrainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
    6267      int origTrainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
     
    101106    }
    102107
    103     private void RotateArray(double[] samples, int p) {
    104       Array.Reverse(samples, 0, p);
    105       Array.Reverse(samples, p, samples.Length - p);
    106       Array.Reverse(samples);
     108        private void RotateArray(double[] samples, int p)
     109        {
     110            Array.Reverse(samples, 0, p);
     111            Array.Reverse(samples, p, samples.Length - p);
     112            Array.Reverse(samples);
     113        }
    107114    }
    108   }
    109115}
Note: See TracChangeset for help on using the changeset viewer.