public void do_random_forest_analyse(Classifiers.IClassifier cls, double allowErr, Func <double[], double> meFunc, Func <double[], double[]> calcDerivative) { int[] count = new int[N]; for (int i = 0; i < N; i++) { count[i] = (Min[i] == Max[i]) ? 1 : NGRID; } create_grid(count); analyse_voronoi(); analyse_error(); int n = candidates.Length; Console.WriteLine(candidates.Length); //int n = grid.Node.Length; Classifiers.LabeledData[] ldata = new Classifiers.LabeledData[n]; int featureCount = 0; for (int i = 0; i < n; i++) { // min, max in locality double maxNeighbours = double.MinValue; double minNeighbours = double.MaxValue; foreach (var neighbour in grid.Neighbours(candidates[i])) //foreach (var neighbour in grid.Neighbours(i)) { double[] calcNeighbour = (double[])grid.Node[neighbour].Clone(); this.func.Calculate(calcNeighbour); if (calcNeighbour[calcNeighbour.Length - 1] < minNeighbours) { minNeighbours = calcNeighbour[calcNeighbour.Length - 1]; } if (calcNeighbour[calcNeighbour.Length - 1] > maxNeighbours) { maxNeighbours = calcNeighbour[calcNeighbour.Length - 1]; } } // current val double[] cuurentNode = (double[])grid.Node[candidates[i]].Clone(); // double[] cuurentNode = (double[])grid.Node[i].Clone(); this.func.Calculate(cuurentNode); double cuurentNodeVal = cuurentNode[cuurentNode.Length - 1]; if (cuurentNodeVal < minNeighbours) { minNeighbours = cuurentNodeVal; } if (cuurentNodeVal > maxNeighbours) { maxNeighbours = cuurentNodeVal; } // is real function and approximation are equal, class for point //derivative double[] derivative = calcDerivative(grid.Node[candidates[i]]); //double[] derivative = calcDerivative(grid.Node[i]); // build features vector double[] features = new double[5 + derivative.Length]; features[0] = borderdist[i]; features[1] = error[i]; features[2] = maxNeighbours; features[3] = minNeighbours; features[4] = cuurentNodeVal; for (int k = 0; k < derivative.Length; k++) { features[5 + k] = derivative[k]; } ldata[i] = new Classifiers.LabeledData(features, 0); featureCount = features.Length; } List <int> newCandidates = new List <int>(); int[] y = new int[ldata.Length]; for (int i = 0; i < ldata.Length; i++) { cls.infer(ldata[i].data, out y[i]); if (y[i] == 1) { //newCandidates.Add(candidates[i]); newCandidates.Add(i); } } candidates = newCandidates.ToArray(); Console.WriteLine(candidates.Length); xfcandidates = Tools.Sub(grid.Node, candidates); }
private int testWithRandomForest(string configFileToLearn, string pointFileToLearn, string configFile, string pointFile, Func <double[], double> funcToLearn, Func <double[], double[]> derivativeFuncToLearn, Func <double[], double> func, Func <double[], double[]> derivativeFunc) { Parser parserToLearn = new Parser(configFile, pointFile); int pointAmountToLearn = parserToLearn.PointAmount; Shepard model = new Shepard(parserToLearn.FunctionDimension, parserToLearn.Points); Analyzer analyzer = new Analyzer(model, parserToLearn.Points); Classifiers.IClassifier cls = analyzer.learn_random_forest_on_grid(funcToLearn, derivativeFuncToLearn, parserToLearn.Approximation); Parser parser = new Parser(configFile, pointFile); int pointAmount = parser.PointAmount; double[][] points = new double[parser.PointAmount][]; for (int j = 0; j < parser.PointAmount; j++) { points[j] = (double[])parser.Points[j].Clone(); } int i = 0; double maxErr = 10; while (i < 10 && maxErr > parser.Approximation) { model = new Shepard(parser.FunctionDimension, points); analyzer = new Analyzer(model, points); analyzer.do_random_forest_analyse(cls, parser.Approximation, func, derivativeFunc); double[][] xx = analyzer.Result; pointAmount = pointAmount + parser.PredictionPointAmount; double[][] newPoints = new double[pointAmount][]; for (int j = 0; j < pointAmount; j++) { if (j < pointAmount - parser.PredictionPointAmount) { newPoints[j] = (double[])points[j].Clone(); } else { newPoints[j] = (double[])xx[j - pointAmount + parser.PredictionPointAmount].Clone(); newPoints[j][parser.FunctionDimension] = func(newPoints[j]); } } points = newPoints; double[][] new_points = new double[parser.PredictionPointAmount][]; for (int j = 0; j < parser.PredictionPointAmount; j++) { new_points[j] = new double[xx[j].Length]; Array.Copy(xx[j], new_points[j], xx[j].Length); model.Calculate(new_points[j]); } double tempErr = 0; for (int k = 0; k < new_points.Length; k++) { double err = Math.Abs(points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]); Console.WriteLine(" \n " + (points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]) + " " + points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] + " " + new_points[k][parser.FunctionDimension] + " \n "); if (err > tempErr) { tempErr = err; } Console.WriteLine("f({0}) real val {1} predict val {2} err {3}", String.Join(", ", xx[k]), points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension], new_points[k][parser.FunctionDimension], err); } maxErr = tempErr; i++; } testResult(parser.FunctionDimension, points, func); return(i); }