Changeset 9363 for branches/OaaS/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 04/16/13 13:13:41 (11 years ago)
- Location:
- branches/OaaS
- Files:
-
- 35 edited
- 11 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll 23 24 packages
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.user
-
- Property svn:ignore
-
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs
r7738 r9363 24 24 using System.Drawing; 25 25 using System.Linq; 26 using System.Threading; 26 27 using HeuristicLab.Collections; 27 28 using HeuristicLab.Common; … … 44 45 45 46 executionState = ExecutionState.Stopped; 46 runs = new RunCollection ();47 runs = new RunCollection { OptimizerName = name }; 47 48 runsCounter = 0; 48 49 … … 119 120 } 120 121 OnAlgorithmChanged(); 121 if (algorithm != null) OnProblemChanged();122 122 Prepare(); 123 123 } … … 245 245 } 246 246 #endregion 247 248 protected override void OnNameChanged() { 249 base.OnNameChanged(); 250 Runs.OptimizerName = Name; 251 } 247 252 248 253 public void Prepare() { … … 306 311 if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || 307 312 clonedAlgorithm.ExecutionState == ExecutionState.Paused) { 308 clonedAlgorithm.Start(); 309 startedAlgorithms++; 313 314 // start and wait until the alg is started 315 using (var signal = new ManualResetEvent(false)) { 316 EventHandler signalSetter = (sender, args) => { signal.Set(); }; 317 clonedAlgorithm.Started += signalSetter; 318 clonedAlgorithm.Start(); 319 signal.WaitOne(); 320 clonedAlgorithm.Started -= signalSetter; 321 322 startedAlgorithms++; 323 } 310 324 } 311 325 } … … 447 461 problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value; 448 462 // clone models 449 var ensembleSolution = new ClassificationEnsembleSolution( 450 solutions.Value.Select(x => cloner.Clone(x.Model)), 451 problemDataClone, 452 solutions.Value.Select(x => cloner.Clone(x.ProblemData.TrainingPartition)), 453 solutions.Value.Select(x => cloner.Clone(x.ProblemData.TestPartition))); 463 var ensembleSolution = new ClassificationEnsembleSolution(problemDataClone); 464 ensembleSolution.AddClassificationSolutions(solutions.Value); 454 465 455 466 aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution)); … … 509 520 private void RegisterEvents() { 510 521 Folds.ValueChanged += new EventHandler(Folds_ValueChanged); 511 SamplesStart.ValueChanged += new EventHandler(SamplesStart_ValueChanged);512 SamplesEnd.ValueChanged += new EventHandler(SamplesEnd_ValueChanged);513 522 RegisterClonedAlgorithmsEvents(); 514 523 } … … 518 527 } 519 528 520 private bool samplesChanged = false;521 private void SamplesStart_ValueChanged(object sender, EventArgs e) {522 samplesChanged = true;523 if (Problem != null) Problem.ProblemData.TrainingPartition.Start = SamplesStart.Value;524 samplesChanged = false;525 }526 private void SamplesEnd_ValueChanged(object sender, EventArgs e) {527 samplesChanged = true;528 if (Problem != null) Problem.ProblemData.TrainingPartition.End = SamplesEnd.Value;529 samplesChanged = false;530 }531 529 532 530 #region template algorithms events … … 541 539 algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged); 542 540 algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged); 541 if (Problem != null) Problem.Reset += new EventHandler(Problem_Reset); 543 542 } 544 543 private void DeregisterAlgorithmEvents() { 545 544 algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged); 546 545 algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged); 546 if (Problem != null) Problem.Reset -= new EventHandler(Problem_Reset); 547 547 } 548 548 private void Algorithm_ProblemChanged(object sender, EventArgs e) { … … 551 551 throw new ArgumentException("A cross validation algorithm can only contain DataAnalysisProblems."); 552 552 } 553 algorithm.Problem.Reset += (x, y) => OnProblemChanged(); 553 if (problem != null) problem.Reset -= new EventHandler(Problem_Reset); 554 if (Problem != null) Problem.Reset += new EventHandler(Problem_Reset); 554 555 problem = (IDataAnalysisProblem)algorithm.Problem; 555 556 OnProblemChanged(); … … 559 560 EventHandler handler = ProblemChanged; 560 561 if (handler != null) handler(this, EventArgs.Empty); 561 if (samplesChanged) return; 562 562 ConfigureProblem(); 563 } 564 565 private void Problem_Reset(object sender, EventArgs e) { 566 ConfigureProblem(); 567 } 568 569 private void ConfigureProblem() { 563 570 SamplesStart.Value = 0; 564 571 if (Problem != null) { … … 581 588 } else 582 589 SamplesEnd.Value = 0; 583 584 SamplesStart_ValueChanged(this, EventArgs.Empty);585 SamplesEnd_ValueChanged(this, EventArgs.Empty);586 590 } 587 591 … … 677 681 lock (locker) { 678 682 if (!stopPending && ExecutionState == ExecutionState.Started) { 679 IAlgorithm preparedAlgorithm = clonedAlgorithms. Where(alg => alg.ExecutionState == ExecutionState.Prepared ||680 alg.ExecutionState == ExecutionState.Paused).FirstOrDefault();683 IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared || 684 alg.ExecutionState == ExecutionState.Paused); 681 685 if (preparedAlgorithm != null) preparedAlgorithm.Start(); 682 686 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs
r7259 r9363 96 96 private void Run(object state) { 97 97 CancellationToken cancellationToken = (CancellationToken)state; 98 lastUpdateTime = DateTime. Now;98 lastUpdateTime = DateTime.UtcNow; 99 99 System.Timers.Timer timer = new System.Timers.Timer(250); 100 100 timer.AutoReset = true; … … 107 107 timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed); 108 108 timer.Stop(); 109 ExecutionTime += DateTime. Now - lastUpdateTime;109 ExecutionTime += DateTime.UtcNow - lastUpdateTime; 110 110 } 111 111 … … 121 121 System.Timers.Timer timer = (System.Timers.Timer)sender; 122 122 timer.Enabled = false; 123 DateTime now = DateTime. Now;123 DateTime now = DateTime.UtcNow; 124 124 ExecutionTime += now - lastUpdateTime; 125 125 lastUpdateTime = now; -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r7825 r9363 101 101 </PropertyGroup> 102 102 <ItemGroup> 103 <Reference Include="ALGLIB-3. 5.0, Version=3.5.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">104 <HintPath>..\..\bin\ALGLIB-3. 5.0.dll</HintPath>103 <Reference Include="ALGLIB-3.7.0, Version=3.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 104 <HintPath>..\..\bin\ALGLIB-3.7.0.dll</HintPath> 105 105 <Private>False</Private> 106 106 </Reference> 107 <Reference Include="LibSVM-1.6.3, Version=1.6.3.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 108 <HintPath>..\..\bin\LibSVM-1.6.3.dll</HintPath> 107 <Reference Include="AutoDiff-1.0, Version=1.0.0.14388, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 108 <SpecificVersion>False</SpecificVersion> 109 <HintPath>..\..\bin\AutoDiff-1.0.dll</HintPath> 110 </Reference> 111 <Reference Include="LibSVM-3.12, Version=3.12.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 112 <HintPath>..\..\bin\LibSVM-3.12.dll</HintPath> 109 113 <Private>False</Private> 110 114 </Reference> … … 113 117 <RequiredTargetFramework>3.5</RequiredTargetFramework> 114 118 </Reference> 115 <Reference Include="System.Data" />116 119 <Reference Include="System.Drawing" /> 117 <Reference Include="System.Xml" />118 120 </ItemGroup> 119 121 <ItemGroup> … … 122 124 </Compile> 123 125 <Compile Include="FixedDataAnalysisAlgorithm.cs" /> 126 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceNeuralNetwork.cs" /> 127 <Compile Include="GaussianProcess\GaussianProcessBase.cs" /> 128 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceConst.cs"> 129 <SubType>Code</SubType> 130 </Compile> 131 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceLinear.cs"> 132 <SubType>Code</SubType> 133 </Compile> 134 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceLinearArd.cs"> 135 <SubType>Code</SubType> 136 </Compile> 137 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceMask.cs"> 138 <SubType>Code</SubType> 139 </Compile> 140 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceMaternIso.cs"> 141 <SubType>Code</SubType> 142 </Compile> 143 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceNoise.cs"> 144 <SubType>Code</SubType> 145 </Compile> 146 <Compile Include="GaussianProcess\CovarianceFunctions\CovariancePeriodic.cs"> 147 <SubType>Code</SubType> 148 </Compile> 149 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceProduct.cs"> 150 <SubType>Code</SubType> 151 </Compile> 152 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceRationalQuadraticArd.cs"> 153 <SubType>Code</SubType> 154 </Compile> 155 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceRationalQuadraticIso.cs"> 156 <SubType>Code</SubType> 157 </Compile> 158 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceScale.cs"> 159 <SubType>Code</SubType> 160 </Compile> 161 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSquaredExponentialArd.cs"> 162 <SubType>Code</SubType> 163 </Compile> 164 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSquaredExponentialIso.cs" /> 165 <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSum.cs"> 166 <SubType>Code</SubType> 167 </Compile> 168 <Compile Include="GaussianProcess\GaussianProcessClassificationSolutionCreator.cs" /> 169 <Compile Include="GaussianProcess\GaussianProcessClassificationModelCreator.cs" /> 170 <Compile Include="GaussianProcess\GaussianProcessClassification.cs" /> 171 <Compile Include="GaussianProcess\MeanFunctions\MeanConst.cs" /> 172 <Compile Include="GaussianProcess\MeanFunctions\MeanLinear.cs" /> 173 <Compile Include="GaussianProcess\MeanFunctions\MeanProduct.cs"> 174 <SubType>Code</SubType> 175 </Compile> 176 <Compile Include="GaussianProcess\MeanFunctions\MeanSum.cs"> 177 <SubType>Code</SubType> 178 </Compile> 179 <Compile Include="GaussianProcess\MeanFunctions\MeanZero.cs" /> 180 <Compile Include="GaussianProcess\GaussianProcessHyperparameterInitializer.cs" /> 181 <Compile Include="GaussianProcess\GaussianProcessRegressionSolutionCreator.cs" /> 182 <Compile Include="GaussianProcess\GaussianProcessRegressionModelCreator.cs" /> 183 <Compile Include="GaussianProcess\GaussianProcessModelCreator.cs"> 184 <SubType>Code</SubType> 185 </Compile> 186 <Compile Include="GaussianProcess\Util.cs" /> 187 <Compile Include="GaussianProcess\IMeanFunction.cs" /> 188 <Compile Include="GaussianProcess\GaussianProcessModel.cs" /> 189 <Compile Include="GaussianProcess\GaussianProcessRegression.cs" /> 190 <Compile Include="GaussianProcess\GaussianProcessRegressionSolution.cs" /> 191 <Compile Include="GaussianProcess\ICovarianceFunction.cs" /> 192 <Compile Include="Interfaces\IGaussianProcessClassificationModelCreator.cs" /> 193 <Compile Include="Interfaces\IGaussianProcessRegressionModelCreator.cs" /> 194 <Compile Include="Interfaces\IGaussianProcessModelCreator.cs" /> 195 <Compile Include="Interfaces\IGaussianProcessModel.cs" /> 196 <Compile Include="Interfaces\IGaussianProcessSolution.cs" /> 197 <Compile Include="Interfaces\INcaClassificationSolution.cs" /> 198 <Compile Include="Interfaces\INcaModel.cs" /> 124 199 <Compile Include="Interfaces\INearestNeighbourClassificationSolution.cs" /> 125 200 <Compile Include="Interfaces\INearestNeighbourRegressionSolution.cs" /> … … 144 219 </Compile> 145 220 <Compile Include="Linear\AlglibUtil.cs" /> 221 <Compile Include="Linear\Scaling.cs" /> 146 222 <Compile Include="Linear\LinearDiscriminantAnalysis.cs" /> 147 223 <Compile Include="Linear\LinearRegression.cs"> … … 151 227 <Compile Include="Linear\MultinomialLogitClassificationSolution.cs" /> 152 228 <Compile Include="Linear\MultinomialLogitModel.cs" /> 229 <Compile Include="Nca\Initialization\INcaInitializer.cs" /> 230 <Compile Include="Nca\Initialization\LdaInitializer.cs" /> 231 <Compile Include="Nca\Initialization\NcaInitializer.cs" /> 232 <Compile Include="Nca\Initialization\PcaInitializer.cs" /> 233 <Compile Include="Nca\Initialization\RandomInitializer.cs" /> 234 <Compile Include="Nca\Matrix.cs" /> 235 <Compile Include="Nca\ModelCreation\INcaModelCreator.cs" /> 236 <Compile Include="Nca\ModelCreation\NcaModelCreator.cs" /> 237 <Compile Include="Nca\NcaAlgorithm.cs" /> 238 <Compile Include="Nca\NcaClassificationSolution.cs" /> 239 <Compile Include="Nca\NcaGradientCalculator.cs" /> 240 <Compile Include="Nca\NcaModel.cs" /> 241 <Compile Include="Nca\SolutionCreation\INcaSolutionCreator.cs" /> 242 <Compile Include="Nca\SolutionCreation\NcaSolutionCreator.cs" /> 153 243 <Compile Include="NearestNeighbour\NearestNeighbourClassification.cs" /> 154 244 <Compile Include="NearestNeighbour\NearestNeighbourClassificationSolution.cs" /> … … 185 275 <SubType>Code</SubType> 186 276 </Compile> 277 <Compile Include="TimeSeries\AutoregressiveModeling.cs" /> 187 278 </ItemGroup> 188 279 <ItemGroup> 280 <ProjectReference Include="..\..\HeuristicLab.Algorithms.GradientDescent\3.3\HeuristicLab.Algorithms.GradientDescent-3.3.csproj"> 281 <Project>{1256B945-EEA9-4BE4-9880-76B5B113F089}</Project> 282 <Name>HeuristicLab.Algorithms.GradientDescent</Name> 283 <Private>False</Private> 284 </ProjectReference> 285 <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj"> 286 <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project> 287 <Name>HeuristicLab.Analysis-3.3</Name> 288 <Private>False</Private> 289 </ProjectReference> 189 290 <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj"> 190 291 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project> … … 212 313 <Private>False</Private> 213 314 </ProjectReference> 315 <ProjectReference Include="..\..\HeuristicLab.Encodings.RealVectorEncoding\3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj"> 316 <Project>{BB6D334A-4BB6-4674-9883-31A6EBB32CAB}</Project> 317 <Name>HeuristicLab.Encodings.RealVectorEncoding-3.3</Name> 318 <Private>False</Private> 319 </ProjectReference> 214 320 <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> 215 321 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> … … 217 323 <Private>False</Private> 218 324 </ProjectReference> 325 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> 326 <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project> 327 <Name>HeuristicLab.Operators-3.3</Name> 328 <Private>False</Private> 329 </ProjectReference> 219 330 <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj"> 220 331 <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project> … … 247 358 <Private>False</Private> 248 359 </ProjectReference> 360 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4.csproj"> 361 <Project>{07486E68-1517-4B9D-A58D-A38E99AE71AB}</Project> 362 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4</Name> 363 </ProjectReference> 249 364 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj"> 250 365 <Project>{3D28463F-EC96-4D82-AFEE-38BE91A0CA00}</Project> … … 260 375 <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project> 261 376 <Name>HeuristicLab.Problems.Instances-3.3</Name> 377 <Private>False</Private> 378 </ProjectReference> 379 <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj"> 380 <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project> 381 <Name>HeuristicLab.Random-3.3</Name> 262 382 <Private>False</Private> 263 383 </ProjectReference> … … 294 414 --> 295 415 <PropertyGroup> 296 <PreBuildEvent >set Path=%25Path%25;$(ProjectDir);$(SolutionDir)416 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir) 297 417 set ProjectDir=$(ProjectDir) 298 418 set SolutionDir=$(SolutionDir) … … 301 421 call PreBuildEvent.cmd 302 422 </PreBuildEvent> 423 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' "> 424 export ProjectDir=$(ProjectDir) 425 export SolutionDir=$(SolutionDir) 426 427 $SolutionDir/PreBuildEvent.sh 428 </PreBuildEvent> 303 429 </PropertyGroup> 304 430 </Project> -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/ISupportVectorMachineModel.cs
r7259 r9363 20 20 #endregion 21 21 22 using HeuristicLab.Optimization;23 22 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab.Core; 25 using System.Collections.Generic; 23 using LibSVM; 26 24 27 25 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 28 /// </summary> 31 29 public interface ISupportVectorMachineModel : IDataAnalysisModel, IRegressionModel, IClassificationModel { 32 SVM.Model Model { get; }33 SVM.RangeTransform RangeTransform { get; }30 svm_model Model { get; } 31 RangeTransform RangeTransform { get; } 34 32 Dataset SupportVectors { get; } 35 33 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs
r7259 r9363 45 45 return matrix; 46 46 } 47 public static double[,] PrepareAndScaleInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows, Scaling scaling) { 48 List<string> variablesList = variables.ToList(); 49 List<int> rowsList = rows.ToList(); 50 51 double[,] matrix = new double[rowsList.Count, variablesList.Count]; 52 53 int col = 0; 54 foreach (string column in variables) { 55 var values = scaling.GetScaledValues(dataset, column, rows); 56 int row = 0; 57 foreach (var value in values) { 58 matrix[row, col] = value; 59 row++; 60 } 61 col++; 62 } 63 64 return matrix; 65 } 47 66 } 48 67 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs
r8139 r9363 111 111 IClassificationProblemData problemData, 112 112 IEnumerable<int> rows) { 113 return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter); 113 var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, new AccuracyMaximizationThresholdCalculator()); 114 model.RecalculateModelParameters(problemData, rows); 115 return model; 114 116 } 115 117 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitClassificationSolution.cs
r7259 r9363 45 45 public MultinomialLogitClassificationSolution(IClassificationProblemData problemData, MultinomialLogitModel logitModel) 46 46 : base(logitModel, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new MultinomialLogitClassificationSolution(this, cloner); 52 51 } 53 54 protected override void RecalculateResults() {55 CalculateResults();56 }57 52 } 58 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitModel.cs
r7259 r9363 109 109 110 110 public MultinomialLogitClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 111 return new MultinomialLogitClassificationSolution( problemData, this);111 return new MultinomialLogitClassificationSolution(new ClassificationProblemData(problemData), this); 112 112 } 113 113 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs
r8139 r9363 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; 27 26 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 27 using HeuristicLab.Optimization; 28 using HeuristicLab.Parameters; 30 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 30 using HeuristicLab.Problems.DataAnalysis; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic;33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;34 using HeuristicLab.Parameters;35 31 36 32 namespace HeuristicLab.Algorithms.DataAnalysis { … … 84 80 85 81 public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData 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 classification does not support NaN or infinity values in the input dataset."); 82 var problemDataClone = (IClassificationProblemData)problemData.Clone(); 83 return new NearestNeighbourClassificationSolution(problemDataClone, Train(problemDataClone, k)); 84 } 93 85 94 alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree(); 95 96 int nRows = inputMatrix.GetLength(0); 97 int nFeatures = inputMatrix.GetLength(1) - 1; 98 double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray(); 99 int nClasses = classValues.Count(); 100 // map original class values to values [0..nClasses-1] 101 Dictionary<double, double> classIndices = new Dictionary<double, double>(); 102 for (int i = 0; i < nClasses; i++) { 103 classIndices[classValues[i]] = i; 104 } 105 for (int row = 0; row < nRows; row++) { 106 inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]]; 107 } 108 alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree); 109 var problemDataClone = (IClassificationProblemData) problemData.Clone(); 110 return new NearestNeighbourClassificationSolution(problemDataClone, new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray())); 86 public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k) { 87 return new NearestNeighbourModel(problemData.Dataset, 88 problemData.TrainingIndices, 89 k, 90 problemData.TargetVariable, 91 problemData.AllowedInputVariables, 92 problemData.ClassValues.ToArray()); 111 93 } 112 94 #endregion -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassificationSolution.cs
r7259 r9363 45 45 public NearestNeighbourClassificationSolution(IClassificationProblemData problemData, INearestNeighbourModel nnModel) 46 46 : base(nnModel, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new NearestNeighbourClassificationSolution(this, cloner); 52 51 } 53 54 protected override void RecalculateResults() {55 CalculateResults();56 }57 52 } 58 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r7294 r9363 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) { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs
r8139 r9363 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 -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegressionSolution.cs
r7259 r9363 45 45 public NearestNeighbourRegressionSolution(IRegressionProblemData problemData, INearestNeighbourModel nnModel) 46 46 : base(nnModel, problemData) { 47 RecalculateResults();48 47 } 49 48 -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassificationSolution.cs
r7259 r9363 45 45 public NeuralNetworkClassificationSolution(IClassificationProblemData problemData, INeuralNetworkModel nnModel) 46 46 : base(nnModel, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new NeuralNetworkClassificationSolution(this, cloner); 52 51 } 53 protected override void RecalculateResults() { 54 CalculateResults(); 55 } 52 56 53 } 57 54 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleClassificationSolution.cs
r7259 r9363 45 45 public NeuralNetworkEnsembleClassificationSolution(IClassificationProblemData problemData, INeuralNetworkEnsembleModel nnModel) 46 46 : base(nnModel, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new NeuralNetworkEnsembleClassificationSolution(this, cloner); 52 51 } 53 54 protected override void RecalculateResults() {55 CalculateResults();56 }57 52 } 58 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs
r7694 r9363 130 130 131 131 public INeuralNetworkEnsembleRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 132 return new NeuralNetworkEnsembleRegressionSolution( problemData, this);132 return new NeuralNetworkEnsembleRegressionSolution(new RegressionEnsembleProblemData(problemData), this); 133 133 } 134 134 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 136 136 } 137 137 public INeuralNetworkEnsembleClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 138 return new NeuralNetworkEnsembleClassificationSolution( problemData, this);138 return new NeuralNetworkEnsembleClassificationSolution(new ClassificationEnsembleProblemData(problemData), this); 139 139 } 140 140 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs
r7259 r9363 138 138 139 139 public INeuralNetworkRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 140 return new NeuralNetworkRegressionSolution( problemData, this);140 return new NeuralNetworkRegressionSolution(new RegressionProblemData(problemData), this); 141 141 } 142 142 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 144 144 } 145 145 public INeuralNetworkClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 146 return new NeuralNetworkClassificationSolution( problemData, this);146 return new NeuralNetworkClassificationSolution(new ClassificationProblemData(problemData), this); 147 147 } 148 148 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegression.cs
r8139 r9363 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic;33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;34 using HeuristicLab.Parameters;35 32 36 33 namespace HeuristicLab.Algorithms.DataAnalysis { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegressionSolution.cs
r7259 r9363 45 45 public NeuralNetworkRegressionSolution(IRegressionProblemData problemData, INeuralNetworkModel nnModel) 46 46 : base(nnModel, problemData) { 47 RecalculateResults();48 47 } 49 48 -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Plugin.cs.frame
r7943 r9363 26 26 /// Plugin class for HeuristicLab.Algorithms.DataAnalysis plugin. 27 27 /// </summary> 28 [Plugin("HeuristicLab.Algorithms.DataAnalysis", "Provides wrappers for data analysis algorithms implemented in external libraries (linear regression, linear discriminant analysis, k-means clustering, support vector classification and regression)", "3.4. 2.$WCREV$")]28 [Plugin("HeuristicLab.Algorithms.DataAnalysis", "Provides wrappers for data analysis algorithms implemented in external libraries (linear regression, linear discriminant analysis, k-means clustering, support vector classification and regression)", "3.4.3.$WCREV$")] 29 29 [PluginFile("HeuristicLab.Algorithms.DataAnalysis-3.4.dll", PluginFileType.Assembly)] 30 [PluginDependency("HeuristicLab.ALGLIB", "3.5.0")] 31 [PluginDependency("HeuristicLab.LibSVM", "1.6.3")] 30 [PluginDependency("HeuristicLab.ALGLIB", "3.7.0")] 31 [PluginDependency("HeuristicLab.AutoDiff", "1.0.0")] 32 [PluginDependency("HeuristicLab.Algorithms.GradientDescent", "3.3")] 33 [PluginDependency("HeuristicLab.Analysis", "3.3")] 32 34 [PluginDependency("HeuristicLab.Collections", "3.3")] 33 35 [PluginDependency("HeuristicLab.Common", "3.3")] … … 35 37 [PluginDependency("HeuristicLab.Core", "3.3")] 36 38 [PluginDependency("HeuristicLab.Data", "3.3")] 39 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 37 40 [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 41 [PluginDependency("HeuristicLab.Operators", "3.3")] 38 42 [PluginDependency("HeuristicLab.Optimization", "3.3")] 39 43 [PluginDependency("HeuristicLab.Parameters", "3.3")] … … 43 47 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification", "3.4")] 44 48 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")] 49 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis", "3.4")] 50 [PluginDependency("HeuristicLab.LibSVM", "3.12")] 51 [PluginDependency("HeuristicLab.Random", "3.3")] 45 52 public class HeuristicLabAlgorithmsDataAnalysisPlugin : PluginBase { 46 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Properties/AssemblyInfo.cs.frame
r7259 r9363 53 53 // by using the '*' as shown below: 54 54 [assembly: AssemblyVersion("3.4.0.0")] 55 [assembly: AssemblyFileVersion("3.4. 2.$WCREV$")]55 [assembly: AssemblyFileVersion("3.4.3.$WCREV$")] -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
r8139 r9363 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic;33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;34 using HeuristicLab.Parameters;35 32 36 33 namespace HeuristicLab.Algorithms.DataAnalysis { … … 45 42 private const string NumberOfTreesParameterName = "Number of trees"; 46 43 private const string RParameterName = "R"; 44 private const string MParameterName = "M"; 45 private const string SeedParameterName = "Seed"; 46 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 47 47 48 #region parameter properties 48 public I ValueParameter<IntValue> NumberOfTreesParameter {49 get { return (I ValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }49 public IFixedValueParameter<IntValue> NumberOfTreesParameter { 50 get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; } 50 51 } 51 public IValueParameter<DoubleValue> RParameter { 52 get { return (IValueParameter<DoubleValue>)Parameters[RParameterName]; } 52 public IFixedValueParameter<DoubleValue> RParameter { 53 get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; } 54 } 55 public IFixedValueParameter<DoubleValue> MParameter { 56 get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; } 57 } 58 public IFixedValueParameter<IntValue> SeedParameter { 59 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 60 } 61 public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter { 62 get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 53 63 } 54 64 #endregion … … 62 72 set { RParameter.Value.Value = value; } 63 73 } 74 public double M { 75 get { return MParameter.Value.Value; } 76 set { MParameter.Value.Value = value; } 77 } 78 public int Seed { 79 get { return SeedParameter.Value.Value; } 80 set { SeedParameter.Value.Value = value; } 81 } 82 public bool SetSeedRandomly { 83 get { return SetSeedRandomlyParameter.Value.Value; } 84 set { SetSeedRandomlyParameter.Value.Value = value; } 85 } 64 86 #endregion 87 65 88 [StorableConstructor] 66 89 private RandomForestClassification(bool deserializing) : base(deserializing) { } … … 68 91 : base(original, cloner) { 69 92 } 93 70 94 public RandomForestClassification() 71 95 : base() { 72 96 Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50))); 73 97 Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "The ratio of the training set that will be used in the construction of individual trees (0<r<=1). Should be adjusted depending on the noise level in the dataset in the range from 0.66 (low noise) to 0.05 (high noise). This parameter should be adjusted to achieve good generalization error.", new DoubleValue(0.3))); 98 Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5))); 99 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 100 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 74 101 Problem = new ClassificationProblem(); 75 102 } 103 76 104 [StorableHook(HookType.AfterDeserialization)] 77 private void AfterDeserialization() { } 105 private void AfterDeserialization() { 106 if (!Parameters.ContainsKey(MParameterName)) 107 Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5))); 108 if (!Parameters.ContainsKey(SeedParameterName)) 109 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 110 if (!Parameters.ContainsKey((SetSeedRandomlyParameterName))) 111 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 112 } 78 113 79 114 public override IDeepCloneable Clone(Cloner cloner) { … … 84 119 protected override void Run() { 85 120 double rmsError, relClassificationError, outOfBagRmsError, outOfBagRelClassificationError; 86 var solution = CreateRandomForestClassificationSolution(Problem.ProblemData, NumberOfTrees, R, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError); 121 if (SetSeedRandomly) Seed = new System.Random().Next(); 122 123 var solution = CreateRandomForestClassificationSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError); 87 124 Results.Add(new Result(RandomForestClassificationModelResultName, "The random forest classification solution.", solution)); 88 125 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError))); … … 92 129 } 93 130 94 public static IClassificationSolution CreateRandomForestClassificationSolution(IClassificationProblemData problemData, int nTrees, double r, 131 public static IClassificationSolution CreateRandomForestClassificationSolution(IClassificationProblemData problemData, int nTrees, double r, double m, int seed, 95 132 out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError) { 133 if (r <= 0 || r > 1) throw new ArgumentException("The R parameter in the random forest regression must be between 0 and 1."); 134 if (m <= 0 || m > 1) throw new ArgumentException("The M parameter in the random forest regression must be between 0 and 1."); 135 136 alglib.math.rndobject = new System.Random(seed); 137 96 138 Dataset dataset = problemData.Dataset; 97 139 string targetVariable = problemData.TargetVariable; … … 102 144 throw new NotSupportedException("Random forest classification does not support NaN or infinity values in the input dataset."); 103 145 146 int info = 0; 147 alglib.decisionforest dForest = new alglib.decisionforest(); 148 alglib.dfreport rep = new alglib.dfreport(); ; 149 int nRows = inputMatrix.GetLength(0); 150 int nColumns = inputMatrix.GetLength(1); 151 int sampleSize = Math.Max((int)Math.Round(r * nRows), 1); 152 int nFeatures = Math.Max((int)Math.Round(m * (nColumns - 1)), 1); 104 153 105 alglib.decisionforest dforest; 106 alglib.dfreport rep; 107 int nRows = inputMatrix.GetLength(0); 108 int nCols = inputMatrix.GetLength(1); 109 int info; 110 double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray(); 111 int nClasses = classValues.Count(); 154 155 double[] classValues = problemData.ClassValues.ToArray(); 156 int nClasses = problemData.Classes; 112 157 // map original class values to values [0..nClasses-1] 113 158 Dictionary<double, double> classIndices = new Dictionary<double, double>(); … … 116 161 } 117 162 for (int row = 0; row < nRows; row++) { 118 inputMatrix[row, nCol s - 1] = classIndices[inputMatrix[row, nCols - 1]];163 inputMatrix[row, nColumns - 1] = classIndices[inputMatrix[row, nColumns - 1]]; 119 164 } 120 // execute random forest algorithm 121 alglib.df buildrandomdecisionforest(inputMatrix, nRows, nCols - 1, nClasses, nTrees, r, out info, out dforest, out rep);165 // execute random forest algorithm 166 alglib.dforest.dfbuildinternal(inputMatrix, nRows, nColumns - 1, nClasses, nTrees, sampleSize, nFeatures, alglib.dforest.dfusestrongsplits + alglib.dforest.dfuseevs, ref info, dForest.innerobj, rep.innerobj); 122 167 if (info != 1) throw new ArgumentException("Error in calculation of random forest classification solution"); 123 168 … … 126 171 relClassificationError = rep.relclserror; 127 172 outOfBagRelClassificationError = rep.oobrelclserror; 128 return new RandomForestClassificationSolution((IClassificationProblemData)problemData.Clone(), new RandomForestModel(d forest, targetVariable, allowedInputVariables, classValues));173 return new RandomForestClassificationSolution((IClassificationProblemData)problemData.Clone(), new RandomForestModel(dForest, targetVariable, allowedInputVariables, classValues)); 129 174 } 130 175 #endregion -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs
r7259 r9363 45 45 public RandomForestClassificationSolution(IClassificationProblemData problemData, IRandomForestModel randomForestModel) 46 46 : base(randomForestModel, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new RandomForestClassificationSolution(this, cloner); 52 51 } 53 54 protected override void RecalculateResults() {55 CalculateResults();56 }57 52 } 58 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r7259 r9363 132 132 133 133 public IRandomForestRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 134 return new RandomForestRegressionSolution( problemData, this);134 return new RandomForestRegressionSolution(new RegressionProblemData(problemData), this); 135 135 } 136 136 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 138 138 } 139 139 public IRandomForestClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 140 return new RandomForestClassificationSolution( problemData, this);140 return new RandomForestClassificationSolution(new ClassificationProblemData(problemData), this); 141 141 } 142 142 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs
r8139 r9363 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using HeuristicLab.Problems.DataAnalysis.Symbolic;33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;34 using HeuristicLab.Parameters;35 32 36 33 namespace HeuristicLab.Algorithms.DataAnalysis { … … 45 42 private const string NumberOfTreesParameterName = "Number of trees"; 46 43 private const string RParameterName = "R"; 44 private const string MParameterName = "M"; 45 private const string SeedParameterName = "Seed"; 46 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 47 47 48 #region parameter properties 48 public I ValueParameter<IntValue> NumberOfTreesParameter {49 get { return (I ValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }49 public IFixedValueParameter<IntValue> NumberOfTreesParameter { 50 get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; } 50 51 } 51 public IValueParameter<DoubleValue> RParameter { 52 get { return (IValueParameter<DoubleValue>)Parameters[RParameterName]; } 52 public IFixedValueParameter<DoubleValue> RParameter { 53 get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; } 54 } 55 public IFixedValueParameter<DoubleValue> MParameter { 56 get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; } 57 } 58 public IFixedValueParameter<IntValue> SeedParameter { 59 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 60 } 61 public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter { 62 get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 53 63 } 54 64 #endregion … … 62 72 set { RParameter.Value.Value = value; } 63 73 } 74 public double M { 75 get { return MParameter.Value.Value; } 76 set { MParameter.Value.Value = value; } 77 } 78 public int Seed { 79 get { return SeedParameter.Value.Value; } 80 set { SeedParameter.Value.Value = value; } 81 } 82 public bool SetSeedRandomly { 83 get { return SetSeedRandomlyParameter.Value.Value; } 84 set { SetSeedRandomlyParameter.Value.Value = value; } 85 } 64 86 #endregion 65 87 [StorableConstructor] … … 68 90 : base(original, cloner) { 69 91 } 92 70 93 public RandomForestRegression() 71 94 : base() { 72 95 Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50))); 73 96 Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "The ratio of the training set that will be used in the construction of individual trees (0<r<=1). Should be adjusted depending on the noise level in the dataset in the range from 0.66 (low noise) to 0.05 (high noise). This parameter should be adjusted to achieve good generalization error.", new DoubleValue(0.3))); 97 Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5))); 98 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 99 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 74 100 Problem = new RegressionProblem(); 75 101 } 102 76 103 [StorableHook(HookType.AfterDeserialization)] 77 private void AfterDeserialization() { } 104 private void AfterDeserialization() { 105 if (!Parameters.ContainsKey(MParameterName)) 106 Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5))); 107 if (!Parameters.ContainsKey(SeedParameterName)) 108 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 109 if (!Parameters.ContainsKey((SetSeedRandomlyParameterName))) 110 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 111 } 78 112 79 113 public override IDeepCloneable Clone(Cloner cloner) { … … 84 118 protected override void Run() { 85 119 double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError; 86 var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError); 120 if (SetSeedRandomly) Seed = new System.Random().Next(); 121 122 var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError); 87 123 Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution)); 88 124 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError))); … … 92 128 } 93 129 94 public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, 130 public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, double m, int seed, 95 131 out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) { 132 if (r <= 0 || r > 1) throw new ArgumentException("The R parameter in the random forest regression must be between 0 and 1."); 133 if (m <= 0 || m > 1) throw new ArgumentException("The M parameter in the random forest regression must be between 0 and 1."); 134 135 alglib.math.rndobject = new System.Random(seed); 136 96 137 Dataset dataset = problemData.Dataset; 97 138 string targetVariable = problemData.TargetVariable; … … 102 143 throw new NotSupportedException("Random forest regression does not support NaN or infinity values in the input dataset."); 103 144 145 int info = 0; 146 alglib.decisionforest dForest = new alglib.decisionforest(); 147 alglib.dfreport rep = new alglib.dfreport(); ; 148 int nRows = inputMatrix.GetLength(0); 149 int nColumns = inputMatrix.GetLength(1); 150 int sampleSize = Math.Max((int)Math.Round(r * nRows), 1); 151 int nFeatures = Math.Max((int)Math.Round(m * (nColumns - 1)), 1); 104 152 105 alglib.decisionforest dforest; 106 alglib.dfreport rep; 107 int nRows = inputMatrix.GetLength(0); 108 109 int info; 110 alglib.dfbuildrandomdecisionforest(inputMatrix, nRows, allowedInputVariables.Count(), 1, nTrees, r, out info, out dforest, out rep); 153 alglib.dforest.dfbuildinternal(inputMatrix, nRows, nColumns - 1, 1, nTrees, sampleSize, nFeatures, alglib.dforest.dfusestrongsplits + alglib.dforest.dfuseevs, ref info, dForest.innerobj, rep.innerobj); 111 154 if (info != 1) throw new ArgumentException("Error in calculation of random forest regression solution"); 112 155 … … 116 159 outOfBagRmsError = rep.oobrmserror; 117 160 118 return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), new RandomForestModel(d forest, targetVariable, allowedInputVariables));161 return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), new RandomForestModel(dForest, targetVariable, allowedInputVariables)); 119 162 } 120 163 #endregion -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs
r7259 r9363 45 45 public RandomForestRegressionSolution(IRegressionProblemData problemData, IRandomForestModel randomForestModel) 46 46 : base(randomForestModel, problemData) { 47 RecalculateResults();48 47 } 49 48 -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs
r8139 r9363 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using LibSVM; 32 33 33 34 namespace HeuristicLab.Algorithms.DataAnalysis { … … 44 45 private const string NuParameterName = "Nu"; 45 46 private const string GammaParameterName = "Gamma"; 47 private const string DegreeParameterName = "Degree"; 46 48 47 49 #region parameter properties … … 60 62 public IValueParameter<DoubleValue> GammaParameter { 61 63 get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; } 64 } 65 public IValueParameter<IntValue> DegreeParameter { 66 get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; } 62 67 } 63 68 #endregion … … 79 84 public DoubleValue Gamma { 80 85 get { return GammaParameter.Value; } 86 } 87 public IntValue Degree { 88 get { return DegreeParameter.Value; } 81 89 } 82 90 #endregion … … 103 111 Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0))); 104 112 Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0))); 113 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 105 114 } 106 115 [StorableHook(HookType.AfterDeserialization)] 107 private void AfterDeserialization() { } 116 private void AfterDeserialization() { 117 #region backwards compatibility (change with 3.4) 118 if (!Parameters.ContainsKey(DegreeParameterName)) 119 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 120 #endregion 121 } 108 122 109 123 public override IDeepCloneable Clone(Cloner cloner) { … … 118 132 int nSv; 119 133 var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, 120 SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, 134 SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Degree.Value, 121 135 out trainingAccuracy, out testAccuracy, out nSv); 122 136 123 137 Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution)); 124 138 Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy))); 125 Results.Add(new Result("Test R²", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));139 Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy))); 126 140 Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv))); 127 141 } 128 142 129 143 public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables, 130 string svmType, string kernelType, double cost, double nu, double gamma, 144 string svmType, string kernelType, double cost, double nu, double gamma, int degree, 131 145 out double trainingAccuracy, out double testAccuracy, out int nSv) { 132 146 Dataset dataset = problemData.Dataset; … … 135 149 136 150 //extract SVM parameters from scope and set them 137 SVM.Parameter parameter = new SVM.Parameter();138 parameter. SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);139 parameter. KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);151 svm_parameter parameter = new svm_parameter(); 152 parameter.svm_type = GetSvmType(svmType); 153 parameter.kernel_type = GetKernelType(kernelType); 140 154 parameter.C = cost; 141 parameter.Nu = nu; 142 parameter.Gamma = gamma; 143 parameter.CacheSize = 500; 144 parameter.Probability = false; 145 155 parameter.nu = nu; 156 parameter.gamma = gamma; 157 parameter.cache_size = 500; 158 parameter.probability = 0; 159 parameter.eps = 0.001; 160 parameter.degree = degree; 161 parameter.shrinking = 1; 162 parameter.coef0 = 0; 163 164 165 var weightLabels = new List<int>(); 166 var weights = new List<double>(); 146 167 foreach (double c in problemData.ClassValues) { 147 168 double wSum = 0.0; … … 151 172 } 152 173 } 153 parameter.Weights.Add((int)c, wSum); 174 weightLabels.Add((int)c); 175 weights.Add(wSum); 154 176 } 155 156 157 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 158 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 159 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 160 var svmModel = SVM.Training.Train(scaledProblem, parameter); 177 parameter.weight_label = weightLabels.ToArray(); 178 parameter.weight = weights.ToArray(); 179 180 181 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 182 RangeTransform rangeTransform = RangeTransform.Compute(problem); 183 svm_problem scaledProblem = rangeTransform.Scale(problem); 184 var svmModel = svm.svm_train(scaledProblem, parameter); 161 185 var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues); 162 186 var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 163 187 164 nSv = svmModel.S upportVectorCount;188 nSv = svmModel.SV.Length; 165 189 trainingAccuracy = solution.TrainingAccuracy; 166 190 testAccuracy = solution.TestAccuracy; 167 191 168 192 return solution; 193 } 194 195 private static int GetSvmType(string svmType) { 196 if (svmType == "NU_SVC") return svm_parameter.NU_SVC; 197 if (svmType == "C_SVC") return svm_parameter.C_SVC; 198 throw new ArgumentException("Unknown SVM type"); 199 } 200 201 private static int GetKernelType(string kernelType) { 202 if (kernelType == "LINEAR") return svm_parameter.LINEAR; 203 if (kernelType == "POLY") return svm_parameter.POLY; 204 if (kernelType == "SIGMOID") return svm_parameter.SIGMOID; 205 if (kernelType == "RBF") return svm_parameter.RBF; 206 throw new ArgumentException("Unknown kernel type"); 169 207 } 170 208 #endregion -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs
r7259 r9363 45 45 public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData) 46 46 : base(model, problemData) { 47 RecalculateResults();48 47 } 49 48 … … 51 50 return new SupportVectorClassificationSolution(this, cloner); 52 51 } 53 54 protected override void RecalculateResults() {55 CalculateResults();56 }57 52 } 58 53 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs
r7259 r9363 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 30 using HeuristicLab.Problems.DataAnalysis; 31 using SVM;31 using LibSVM; 32 32 33 33 namespace HeuristicLab.Algorithms.DataAnalysis { … … 39 39 public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel { 40 40 41 private SVM.Model model;41 private svm_model model; 42 42 /// <summary> 43 43 /// Gets or sets the SVM model. 44 44 /// </summary> 45 public SVM.Model Model {45 public svm_model Model { 46 46 get { return model; } 47 47 set { … … 57 57 /// Gets or sets the range transformation for the model. 58 58 /// </summary> 59 private SVM.RangeTransform rangeTransform;60 public SVM.RangeTransform RangeTransform {59 private RangeTransform rangeTransform; 60 public RangeTransform RangeTransform { 61 61 get { return rangeTransform; } 62 62 set { … … 71 71 public Dataset SupportVectors { 72 72 get { 73 var data = new double[Model. SupportVectorCount, allowedInputVariables.Count()];74 for (int i = 0; i < Model. SupportVectorCount; i++) {75 var sv = Model.S upportVectors[i];73 var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()]; 74 for (int i = 0; i < Model.sv_coef.Length; i++) { 75 var sv = Model.SV[i]; 76 76 for (int j = 0; j < sv.Length; j++) { 77 data[i, j] = sv[j]. Value;77 data[i, j] = sv[j].value; 78 78 } 79 79 } … … 101 101 this.classValues = (double[])original.classValues.Clone(); 102 102 } 103 public SupportVectorMachineModel( SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)103 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues) 104 104 : this(model, rangeTransform, targetVariable, allowedInputVariables) { 105 105 this.classValues = classValues.ToArray(); 106 106 } 107 public SupportVectorMachineModel( SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)107 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables) 108 108 : base() { 109 109 this.name = ItemName; … … 124 124 } 125 125 public SupportVectorRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 126 return new SupportVectorRegressionSolution(this, problemData);126 return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData)); 127 127 } 128 128 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { … … 153 153 154 154 public SupportVectorClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 155 return new SupportVectorClassificationSolution(this, problemData);155 return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData)); 156 156 } 157 157 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { … … 161 161 private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) { 162 162 // calculate predictions for the currently requested rows 163 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);164 SVM.Problem scaledProblem = Scaling.Scale(RangeTransform,problem);165 166 for (int i = 0; i < scaledProblem.Count; i++) {167 yield return SVM.Prediction.Predict(Model, scaledProblem.X[i]);163 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 164 svm_problem scaledProblem = rangeTransform.Scale(problem); 165 166 for (int i = 0; i < problem.l; i++) { 167 yield return svm.svm_predict(Model, scaledProblem.x[i]); 168 168 } 169 169 } … … 183 183 get { 184 184 using (MemoryStream stream = new MemoryStream()) { 185 SVM.Model.Write(stream, Model);185 svm.svm_save_model(new StreamWriter(stream), Model); 186 186 stream.Seek(0, System.IO.SeekOrigin.Begin); 187 187 StreamReader reader = new StreamReader(stream); … … 191 191 set { 192 192 using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) { 193 model = SVM.Model.Read(stream);193 model = svm.svm_load_model(new StreamReader(stream)); 194 194 } 195 195 } … … 199 199 get { 200 200 using (MemoryStream stream = new MemoryStream()) { 201 SVM.RangeTransform.Write(stream, RangeTransform);201 RangeTransform.Write(stream, RangeTransform); 202 202 stream.Seek(0, System.IO.SeekOrigin.Begin); 203 203 StreamReader reader = new StreamReader(stream); … … 207 207 set { 208 208 using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) { 209 RangeTransform = SVM.RangeTransform.Read(stream);209 RangeTransform = RangeTransform.Read(stream); 210 210 } 211 211 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs
r7259 r9363 23 23 using System.Linq; 24 24 using HeuristicLab.Problems.DataAnalysis; 25 using LibSVM; 25 26 26 27 namespace HeuristicLab.Algorithms.DataAnalysis { … … 32 33 /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param> 33 34 /// <returns>A problem data type that can be used to train a support vector machine.</returns> 34 public static SVM.Problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {35 public static svm_problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) { 35 36 double[] targetVector = 36 37 dataset.GetDoubleValues(targetVariable, rowIndices).ToArray(); 37 38 38 SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];39 List< SVM.Node> tempRow;39 svm_node[][] nodes = new svm_node[targetVector.Length][]; 40 List<svm_node> tempRow; 40 41 int maxNodeIndex = 0; 41 42 int svmProblemRowIndex = 0; 42 43 List<string> inputVariablesList = inputVariables.ToList(); 43 44 foreach (int row in rowIndices) { 44 tempRow = new List< SVM.Node>();45 tempRow = new List<svm_node>(); 45 46 int colIndex = 1; // make sure the smallest node index for SVM = 1 46 47 foreach (var inputVariable in inputVariablesList) { … … 49 50 // => don't add NaN values in the dataset to the sparse SVM matrix representation 50 51 if (!double.IsNaN(value)) { 51 tempRow.Add(new SVM.Node(colIndex, value)); // nodes must be sorted in ascending ordered by column index52 tempRow.Add(new svm_node() { index = colIndex, value = value }); // nodes must be sorted in ascending ordered by column index 52 53 if (colIndex > maxNodeIndex) maxNodeIndex = colIndex; 53 54 } … … 57 58 } 58 59 59 return new SVM.Problem(targetVector.Length, targetVector, nodes, maxNodeIndex);60 return new svm_problem() { l = targetVector.Length, y = targetVector, x = nodes }; 60 61 } 61 62 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs
r8139 r9363 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using LibSVM; 32 33 33 34 namespace HeuristicLab.Algorithms.DataAnalysis { … … 45 46 private const string GammaParameterName = "Gamma"; 46 47 private const string EpsilonParameterName = "Epsilon"; 48 private const string DegreeParameterName = "Degree"; 47 49 48 50 #region parameter properties … … 64 66 public IValueParameter<DoubleValue> EpsilonParameter { 65 67 get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; } 68 } 69 public IValueParameter<IntValue> DegreeParameter { 70 get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; } 66 71 } 67 72 #endregion … … 86 91 public DoubleValue Epsilon { 87 92 get { return EpsilonParameter.Value; } 93 } 94 public IntValue Degree { 95 get { return DegreeParameter.Value; } 88 96 } 89 97 #endregion … … 111 119 Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0))); 112 120 Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1))); 121 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 113 122 } 114 123 [StorableHook(HookType.AfterDeserialization)] 115 private void AfterDeserialization() { } 124 private void AfterDeserialization() { 125 #region backwards compatibility (change with 3.4) 126 if (!Parameters.ContainsKey(DegreeParameterName)) 127 Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3))); 128 #endregion 129 } 116 130 117 131 public override IDeepCloneable Clone(Cloner cloner) { … … 126 140 int nSv; 127 141 var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value, 128 KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value, 142 KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value, Degree.Value, 129 143 out trainR2, out testR2, out nSv); 130 144 … … 136 150 137 151 public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables, 138 string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, 152 string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree, 139 153 out double trainingR2, out double testR2, out int nSv) { 140 154 Dataset dataset = problemData.Dataset; … … 143 157 144 158 //extract SVM parameters from scope and set them 145 SVM.Parameter parameter = new SVM.Parameter();146 parameter. SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);147 parameter. KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);159 svm_parameter parameter = new svm_parameter(); 160 parameter.svm_type = GetSvmType(svmType); 161 parameter.kernel_type = GetKernelType(kernelType); 148 162 parameter.C = cost; 149 parameter.Nu = nu; 150 parameter.Gamma = gamma; 151 parameter.P = epsilon; 152 parameter.CacheSize = 500; 153 parameter.Probability = false; 154 155 156 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 157 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 158 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 159 var svmModel = SVM.Training.Train(scaledProblem, parameter); 160 nSv = svmModel.SupportVectorCount; 163 parameter.nu = nu; 164 parameter.gamma = gamma; 165 parameter.p = epsilon; 166 parameter.cache_size = 500; 167 parameter.probability = 0; 168 parameter.eps = 0.001; 169 parameter.degree = degree; 170 parameter.shrinking = 1; 171 parameter.coef0 = 0; 172 173 174 175 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 176 RangeTransform rangeTransform = RangeTransform.Compute(problem); 177 svm_problem scaledProblem = rangeTransform.Scale(problem); 178 var svmModel = svm.svm_train(scaledProblem, parameter); 179 nSv = svmModel.SV.Length; 161 180 var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables); 162 181 var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone()); … … 165 184 return solution; 166 185 } 186 187 private static int GetSvmType(string svmType) { 188 if (svmType == "NU_SVR") return svm_parameter.NU_SVR; 189 if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR; 190 throw new ArgumentException("Unknown SVM type"); 191 } 192 193 private static int GetKernelType(string kernelType) { 194 if (kernelType == "LINEAR") return svm_parameter.LINEAR; 195 if (kernelType == "POLY") return svm_parameter.POLY; 196 if (kernelType == "SIGMOID") return svm_parameter.SIGMOID; 197 if (kernelType == "RBF") return svm_parameter.RBF; 198 throw new ArgumentException("Unknown kernel type"); 199 } 167 200 #endregion 168 201 } -
branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs
r7259 r9363 45 45 public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData) 46 46 : base(model, problemData) { 47 RecalculateResults();48 47 } 49 48
Note: See TracChangeset
for help on using the changeset viewer.