- Timestamp:
- 03/10/11 10:00:09 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs
r5617 r5649 76 76 base.Start(); 77 77 OnStarted(); 78 Run(); 79 OnStopped(); 78 try { 79 Run(); 80 } 81 catch (Exception e) { 82 OnExceptionOccurred(e); 83 } 84 finally { 85 OnStopped(); 86 } 80 87 } 81 88 -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs
r5624 r5649 42 42 [StorableClass] 43 43 public sealed class LinearRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> { 44 private const string LinearRegressionModelResultName = "Linear RegressionModel";44 private const string LinearRegressionModelResultName = "Linear regression solution"; 45 45 46 46 [StorableConstructor] … … 51 51 public LinearRegression() 52 52 : base() { 53 Problem = new RegressionProblem(); 53 54 } 54 55 [StorableHook(HookType.AfterDeserialization)] … … 63 64 double rmsError, cvRmsError; 64 65 var solution = CreateLinearRegressionSolution(Problem.ProblemData, out rmsError, out cvRmsError); 65 Results.Add(new Result(LinearRegressionModelResultName, "The linear regression model.", solution));66 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear regression modelon the training set.", new DoubleValue(rmsError)));67 Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear regression modelvia cross validation.", new DoubleValue(cvRmsError)));66 Results.Add(new Result(LinearRegressionModelResultName, "The linear regression solution.", solution)); 67 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear regression solution on the training set.", new DoubleValue(rmsError))); 68 Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear regression solution via cross validation.", new DoubleValue(cvRmsError))); 68 69 } 69 70 … … 71 72 Dataset dataset = problemData.Dataset; 72 73 string targetVariable = problemData.TargetVariable; 73 IEnumerable<string> allowedInputVariables = problemData. InputVariables.CheckedItems.Select(x => x.Value);74 IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables; 74 75 int samplesStart = problemData.TrainingPartitionStart.Value; 75 76 int samplesEnd = problemData.TrainingPartitionEnd.Value; 76 77 IEnumerable<string> selectedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);78 77 79 78 double[,] inputMatrix = LinearRegressionUtil.PrepareInputMatrix(dataset, targetVariable, allowedInputVariables, samplesStart, samplesEnd); … … 87 86 int retVal = 1; 88 87 alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar); 89 if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");88 if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression solution"); 90 89 rmsError = ar.rmserror; 91 90 cvRmsError = ar.cvrmserror; -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs
r5626 r5649 88 88 public SupportVectorClassification() 89 89 : base() { 90 Problem = new ClassificationProblem(); 91 90 92 List<StringValue> svrTypes = (from type in new List<string> { "NU_SVC", "EPSILON_SVC" } 91 93 select new StringValue(type).AsReadOnly()) … … 95 97 select new StringValue(type).AsReadOnly()) 96 98 .ToList(); 97 ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>( svrTypes);99 ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes); 98 100 Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0])); 99 101 Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3])); … … 112 114 protected override void Run() { 113 115 IClassificationProblemData problemData = Problem.ProblemData; 114 IEnumerable<string> selectedInputVariables = problemData. InputVariables.CheckedItems.Select(x => x.Value);116 IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables; 115 117 var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value); 116 118 … … 140 142 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 141 143 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 142 var model = new SupportVectorMachineModel(); 143 model.Model = SVM.Training.Train(scaledProblem, parameter); 144 model.RangeTransform = rangeTransform; 144 var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables); 145 145 146 return new SupportVectorClassificationSolution(model, problemData , allowedInputVariables, double.NegativeInfinity, double.PositiveInfinity);146 return new SupportVectorClassificationSolution(model, problemData); 147 147 } 148 148 #endregion -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs
r5626 r5649 41 41 } 42 42 43 [Storable]44 private double lowerEstimationLimit;45 public double LowerEstimationLimit {46 get { return lowerEstimationLimit; }47 }48 49 [Storable]50 private double upperEstimationLimit;51 public double UpperEstimationLimit {52 get { return upperEstimationLimit; }53 }54 55 private List<string> inputVariables;56 [Storable]57 private IEnumerable<string> InputVariablesStorable {58 get { return inputVariables; }59 set { inputVariables = new List<string>(value); }60 }61 62 43 public Dataset SupportVectors { 63 44 get { return CalculateSupportVectors(); } … … 68 49 private SupportVectorClassificationSolution(SupportVectorClassificationSolution original, Cloner cloner) 69 50 : base(original, cloner) { 70 inputVariables = new List<string>(original.inputVariables);71 lowerEstimationLimit = original.lowerEstimationLimit;72 upperEstimationLimit = original.upperEstimationLimit;73 51 } 74 public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData , IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)52 public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData) 75 53 : base(model, problemData) { 76 this.inputVariables = new List<string>(inputVariables);77 this.lowerEstimationLimit = lowerEstimationLimit;78 this.upperEstimationLimit = upperEstimationLimit;79 54 } 80 55 … … 87 62 base.OnProblemDataChanged(e); 88 63 } 89 64 90 65 private Dataset CalculateSupportVectors() { 91 66 if (Model.Model.SupportVectorIndizes.Length == 0) -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs
r5626 r5649 30 30 using SVM; 31 31 using HeuristicLab.Problems.DataAnalysis; 32 using System.Drawing; 32 33 33 34 namespace HeuristicLab.Algorithms.DataAnalysis { … … 38 39 [Item("SupportVectorMachineModel", "Represents a support vector machine model.")] 39 40 public sealed class SupportVectorMachineModel : NamedItem, IRegressionModel, IClassificationModel { 40 [StorableConstructor]41 private SupportVectorMachineModel(bool deserializing) : base(deserializing) { }42 private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)43 : base(original, cloner) {44 // only using a shallow copy here! (gkronber)45 this.model = original.model;46 this.rangeTransform = original.rangeTransform;47 }48 public SupportVectorMachineModel() : base() { }49 41 50 42 private SVM.Model model; … … 77 69 } 78 70 } 71 72 [Storable] 73 private string targetVariable; 74 [Storable] 75 private string[] allowedInputVariables; 76 77 [StorableConstructor] 78 private SupportVectorMachineModel(bool deserializing) : base(deserializing) { } 79 private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner) 80 : base(original, cloner) { 81 // only using a shallow copy here! (gkronber) 82 this.model = original.model; 83 this.rangeTransform = original.rangeTransform; 84 85 } 86 public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables) 87 : base() { 88 this.name = ItemName; 89 this.description = ItemDescription; 90 this.model = model; 91 this.rangeTransform = rangeTransform; 92 this.targetVariable = targetVariable; 93 this.allowedInputVariables = allowedInputVariables.ToArray(); 94 } 95 96 public override IDeepCloneable Clone(Cloner cloner) { 97 return new SupportVectorMachineModel(this, cloner); 98 } 99 100 79 101 #region IRegressionModel Members 80 public IEnumerable<double> GetEstimatedValues( IRegressionProblemData problemData, IEnumerable<int> rows) {81 return GetEstimatedValues (problemData, problemData.TargetVariable, rows);102 public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) { 103 return GetEstimatedValuesHelper(dataset, rows); 82 104 } 83 105 #endregion 84 106 #region IClassificationModel Members 85 public IEnumerable<double> GetEstimatedValues(IClassificationProblemData problemData, IEnumerable<int> rows) { 86 return GetEstimatedValues(problemData, problemData.TargetVariable, rows); 87 } 88 89 public IEnumerable<double> GetEstimatedClassValues(IClassificationProblemData problemData, IEnumerable<int> rows) { 90 return GetEstimatedValues(problemData, problemData.TargetVariable, rows); 107 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 108 return GetEstimatedValuesHelper(dataset, rows); 91 109 } 92 110 #endregion 93 private IEnumerable<double> GetEstimatedValues(IDataAnalysisProblemData problemData, string targetVariable, IEnumerable<int> rows) { 94 var allowedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value); 95 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData.Dataset, targetVariable, allowedInputVariables, rows); 111 private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) { 112 SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows); 96 113 SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem); 97 114 … … 149 166 } 150 167 #endregion 151 152 public override IDeepCloneable Clone(Cloner cloner) {153 return new SupportVectorMachineModel(this, cloner);154 }155 156 /// <summary>157 /// Exports the <paramref name="model"/> in string representation to stream <paramref name="s"/>158 /// </summary>159 /// <param name="model">The support vector machine model to export</param>160 /// <param name="s">The stream to export the model to</param>161 public static void Export(SupportVectorMachineModel model, Stream s) {162 StreamWriter writer = new StreamWriter(s);163 writer.WriteLine("RangeTransform:");164 writer.Flush();165 using (MemoryStream memStream = new MemoryStream()) {166 SVM.RangeTransform.Write(memStream, model.RangeTransform);167 memStream.Seek(0, SeekOrigin.Begin);168 memStream.WriteTo(s);169 }170 writer.WriteLine("Model:");171 writer.Flush();172 using (MemoryStream memStream = new MemoryStream()) {173 SVM.Model.Write(memStream, model.Model);174 memStream.Seek(0, SeekOrigin.Begin);175 memStream.WriteTo(s);176 }177 s.Flush();178 }179 180 /// <summary>181 /// Imports a support vector machine model given as string representation.182 /// </summary>183 /// <param name="reader">The reader to retrieve the string representation from</param>184 /// <returns>The imported support vector machine model.</returns>185 public static SupportVectorMachineModel Import(TextReader reader) {186 SupportVectorMachineModel model = new SupportVectorMachineModel();187 while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform";188 model.RangeTransform = SVM.RangeTransform.Read(reader);189 // read until "Model:"190 while (reader.ReadLine().Trim() != "Model:") ;191 model.Model = SVM.Model.Read(reader);192 return model;193 }194 168 } 195 169 } -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs
r5626 r5649 95 95 public SupportVectorRegression() 96 96 : base() { 97 Problem = new RegressionProblem(); 98 97 99 List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" } 98 100 select new StringValue(type).AsReadOnly()) … … 102 104 select new StringValue(type).AsReadOnly()) 103 105 .ToList(); 104 ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>( svrTypes);106 ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes); 105 107 Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0])); 106 108 Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3])); … … 120 122 protected override void Run() { 121 123 IRegressionProblemData problemData = Problem.ProblemData; 122 IEnumerable<string> selectedInputVariables = problemData. InputVariables.CheckedItems.Select(x => x.Value);124 IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables; 123 125 var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value); 124 126 … … 149 151 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 150 152 SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem); 151 var model = new SupportVectorMachineModel(); 152 model.Model = SVM.Training.Train(scaledProblem, parameter); 153 model.RangeTransform = rangeTransform; 154 155 return new SupportVectorRegressionSolution(model, problemData, allowedInputVariables, double.NegativeInfinity, double.PositiveInfinity); 153 var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables); 154 return new SupportVectorRegressionSolution(model, problemData); 156 155 } 157 156 #endregion -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs
r5626 r5649 41 41 } 42 42 43 [Storable]44 private double lowerEstimationLimit;45 public double LowerEstimationLimit {46 get { return lowerEstimationLimit; }47 }48 49 [Storable]50 private double upperEstimationLimit;51 public double UpperEstimationLimit {52 get { return upperEstimationLimit; }53 }54 55 private List<string> inputVariables;56 [Storable]57 private IEnumerable<string> InputVariablesStorable {58 get { return inputVariables; }59 set { inputVariables = new List<string>(value); }60 }61 62 43 public Dataset SupportVectors { 63 44 get { return CalculateSupportVectors(); } … … 68 49 private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner) 69 50 : base(original, cloner) { 70 inputVariables = new List<string>(original.inputVariables);71 lowerEstimationLimit = original.lowerEstimationLimit;72 upperEstimationLimit = original.upperEstimationLimit;73 51 } 74 public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData , IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)52 public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData) 75 53 : base(model, problemData) { 76 this.inputVariables = new List<string>(inputVariables);77 this.lowerEstimationLimit = lowerEstimationLimit;78 this.upperEstimationLimit = upperEstimationLimit;79 54 } 80 55
Note: See TracChangeset
for help on using the changeset viewer.