Changeset 8660 for branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
- Timestamp:
- 09/14/12 18:58:15 (12 years ago)
- Location:
- branches/GP-MoveOperators
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.user
-
- Property svn:ignore
-
branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r7294 r8660 33 33 /// </summary> 34 34 [StorableClass] 35 [Item("NearestNeighbourModel", "Represents a ne ural networkfor regression and classification.")]35 [Item("NearestNeighbourModel", "Represents a nearest neighbour model for regression and classification.")] 36 36 public sealed class NearestNeighbourModel : NamedItem, INearestNeighbourModel { 37 37 … … 56 56 [Storable] 57 57 private int k; 58 58 59 [StorableConstructor] 59 60 private NearestNeighbourModel(bool deserializing) … … 95 96 this.classValues = (double[])original.classValues.Clone(); 96 97 } 97 public NearestNeighbourModel(alglib.nearestneighbor.kdtree kdTree, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 98 : base() { 99 this.name = ItemName; 100 this.description = ItemDescription; 101 this.kdTree = kdTree; 98 public NearestNeighbourModel(Dataset dataset, IEnumerable<int> rows, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) { 99 Name = ItemName; 100 Description = ItemDescription; 102 101 this.k = k; 103 102 this.targetVariable = targetVariable; 104 103 this.allowedInputVariables = allowedInputVariables.ToArray(); 105 if (classValues != null) 104 105 var inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, 106 allowedInputVariables.Concat(new string[] { targetVariable }), 107 rows); 108 109 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 110 throw new NotSupportedException( 111 "Nearest neighbour classification does not support NaN or infinity values in the input dataset."); 112 113 this.kdTree = new alglib.nearestneighbor.kdtree(); 114 115 var nRows = inputMatrix.GetLength(0); 116 var nFeatures = inputMatrix.GetLength(1) - 1; 117 118 if (classValues != null) { 106 119 this.classValues = (double[])classValues.Clone(); 120 int nClasses = classValues.Length; 121 // map original class values to values [0..nClasses-1] 122 var classIndices = new Dictionary<double, double>(); 123 for (int i = 0; i < nClasses; i++) 124 classIndices[classValues[i]] = i; 125 126 for (int row = 0; row < nRows; row++) { 127 inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]]; 128 } 129 } 130 alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdTree); 107 131 } 108 132 … … 140 164 141 165 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 166 if (classValues == null) throw new InvalidOperationException("No class values are defined."); 142 167 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 143 168 … … 177 202 178 203 public INearestNeighbourRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 179 return new NearestNeighbourRegressionSolution( problemData, this);204 return new NearestNeighbourRegressionSolution(new RegressionProblemData(problemData), this); 180 205 } 181 206 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 183 208 } 184 209 public INearestNeighbourClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 185 return new NearestNeighbourClassificationSolution( problemData, this);210 return new NearestNeighbourClassificationSolution(new ClassificationProblemData(problemData), this); 186 211 } 187 212 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
Note: See TracChangeset
for help on using the changeset viewer.