Exemplo n.º 1
0
    private RandomForestModel(RandomForestModel original, Cloner cloner)
      : base(original, cloner) {
      randomForest = new alglib.decisionforest();
      randomForest.innerobj.bufsize = original.randomForest.innerobj.bufsize;
      randomForest.innerobj.nclasses = original.randomForest.innerobj.nclasses;
      randomForest.innerobj.ntrees = original.randomForest.innerobj.ntrees;
      randomForest.innerobj.nvars = original.randomForest.innerobj.nvars;
      // we assume that the trees array (double[]) is immutable in alglib
      randomForest.innerobj.trees = original.randomForest.innerobj.trees;

      // allowedInputVariables is immutable so we don't need to clone
      allowedInputVariables = original.allowedInputVariables;

      // clone data which is necessary to rebuild the model
      this.seed = original.seed;
      this.originalTrainingData = cloner.Clone(original.originalTrainingData);
      // classvalues is immutable so we don't need to clone
      this.classValues = original.classValues;
      this.nTrees = original.nTrees;
      this.r = original.r;
      this.m = original.m;
    }
Exemplo n.º 2
0
        private RandomForestModel(RandomForestModel original, Cloner cloner)
            : base(original, cloner)
        {
            randomForest = new alglib.decisionforest();
            randomForest.innerobj.bufsize  = original.randomForest.innerobj.bufsize;
            randomForest.innerobj.nclasses = original.randomForest.innerobj.nclasses;
            randomForest.innerobj.ntrees   = original.randomForest.innerobj.ntrees;
            randomForest.innerobj.nvars    = original.randomForest.innerobj.nvars;
            // we assume that the trees array (double[]) is immutable in alglib
            randomForest.innerobj.trees = original.randomForest.innerobj.trees;

            // allowedInputVariables is immutable so we don't need to clone
            allowedInputVariables = original.allowedInputVariables;

            // clone data which is necessary to rebuild the model
            this.seed = original.seed;
            this.originalTrainingData = cloner.Clone(original.originalTrainingData);
            // classvalues is immutable so we don't need to clone
            this.classValues = original.classValues;
            this.nTrees      = original.nTrees;
            this.r           = original.r;
            this.m           = original.m;
        }
        private static void CrossValidate(IRegressionProblemData problemData, Tuple <IEnumerable <int>, IEnumerable <int> >[] partitions, int nTrees, double r, double m, int seed, out double avgTestMse)
        {
            avgTestMse = 0;
            var ds             = problemData.Dataset;
            var targetVariable = GetTargetVariableName(problemData);

            foreach (var tuple in partitions)
            {
                double rmsError, avgRelError, outOfBagAvgRelError, outOfBagRmsError;
                var    trainingRandomForestPartition = tuple.Item1;
                var    testRandomForestPartition     = tuple.Item2;
                var    model           = RandomForestModel.CreateRegressionModel(problemData, trainingRandomForestPartition, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
                var    estimatedValues = model.GetEstimatedValues(ds, testRandomForestPartition);
                var    targetValues    = ds.GetDoubleValues(targetVariable, testRandomForestPartition);
                OnlineCalculatorError calculatorError;
                double mse = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, targetValues, out calculatorError);
                if (calculatorError != OnlineCalculatorError.None)
                {
                    mse = double.NaN;
                }
                avgTestMse += mse;
            }
            avgTestMse /= partitions.Length;
        }
 public static RandomForestModel CreateRandomForestClassificationModel(IClassificationProblemData problemData, int nTrees, double r, double m, int seed,
                                                                       out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError)
 {
     return(RandomForestModel.CreateClassificationModel(problemData, nTrees, r, m, seed,
                                                        rmsError: out rmsError, relClassificationError: out relClassificationError, outOfBagRmsError: out outOfBagRmsError, outOfBagRelClassificationError: out outOfBagRelClassificationError));
 }
 public static RandomForestModel CreateRandomForestRegressionModel(IRegressionProblemData problemData, int nTrees,
                                                                   double r, double m, int seed,
                                                                   out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError)
 {
     return(RandomForestModel.CreateRegressionModel(problemData, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError));
 }