Changeset 8465 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs
- Timestamp:
- 08/10/12 14:57:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs
r8139 r8465 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 26 using HeuristicLab.Optimization; 27 using HeuristicLab.Parameters; 30 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 29 using HeuristicLab.Problems.DataAnalysis; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic;33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;34 using HeuristicLab.Parameters;35 30 36 31 namespace HeuristicLab.Algorithms.DataAnalysis { … … 84 79 85 80 public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) { 86 Dataset dataset = problemData.Dataset; 87 string targetVariable = problemData.TargetVariable; 88 IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables; 89 IEnumerable<int> rows = problemData.TrainingIndices; 90 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows); 91 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 92 throw new NotSupportedException("Nearest neighbour regression does not support NaN or infinity values in the input dataset."); 81 var clonedProblemData = (IRegressionProblemData)problemData.Clone(); 82 return new NearestNeighbourRegressionSolution(clonedProblemData, Train(problemData, k)); 83 } 93 84 94 alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree(); 95 96 int nRows = inputMatrix.GetLength(0); 97 98 alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree); 99 100 return new NearestNeighbourRegressionSolution((IRegressionProblemData)problemData.Clone(), new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables)); 85 public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k) { 86 return new NearestNeighbourModel(problemData.Dataset, 87 problemData.TrainingIndices, 88 k, 89 problemData.TargetVariable, 90 problemData.AllowedInputVariables); 101 91 } 102 92 #endregion
Note: See TracChangeset
for help on using the changeset viewer.