using System; using System.Collections.Generic; using System.Linq; using ExcelDna.Integration; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLabExcel { public class ExcelFunctions { /* Standard example from ExcelDNA */ [ExcelFunction(Description = "Multiplies two numbers", Category = "Useful functions")] public static double MultiplyThem(double x, double y) { return x * y; } [ExcelFunction(Description = "Random forest", Category = "Data Analysis")] public static double[,] PredictRandomForest(double[,] x, double[] y) { int nRows = x.GetLength(0); int nCols = x.GetLength(1); if (nRows > 5000) throw new ArgumentException("y"); if (nCols >= nRows) throw new ArgumentException("x"); var inputs = Enumerable.Range(0, nCols).Select(i => "x" + i); var target = "y"; var variables = inputs.Concat(new string[] { target }); // copy data var xy = new double[nRows, nCols + 1]; for (int r = 0; r < nRows; r++) { for (int c = 0; c < nCols; c++) { xy[r, c] = x[r, c]; } if (r < y.Length) xy[r, nCols] = y[r]; } var ds = new Dataset(variables, xy); var problemData = new RegressionProblemData(ds, inputs, target); problemData.TrainingPartition.Start = 0; problemData.TrainingPartition.End = y.Length; problemData.TestPartition.Start = y.Length; problemData.TestPartition.End = nRows; double rmsError; double oobAvgRelError; double oobRmsError; double avgRelError; var rf = HeuristicLab.Algorithms.DataAnalysis.RandomForestRegression.CreateRandomForestRegressionSolution(problemData, 100, 0.5, 0.5, 31415, out rmsError, out avgRelError, out oobRmsError, out oobAvgRelError); // copy for output var res = new double[nRows, 1]; var estValuesEnum = rf.EstimatedValues.GetEnumerator(); estValuesEnum.MoveNext(); for (int r = 0; r < nRows; r++) { res[r, 0] = estValuesEnum.Current; estValuesEnum.MoveNext(); } return res; } } }