Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/11 10:00:09 (14 years ago)
Author:
gkronber
Message:

#1418 Implemented classes for classification based on a discriminant function and thresholds and implemented interfaces and base classes for clustering.

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  
    7676      base.Start();
    7777      OnStarted();
    78       Run();
    79       OnStopped();
     78      try {
     79        Run();
     80      }
     81      catch (Exception e) {
     82        OnExceptionOccurred(e);
     83      }
     84      finally {
     85        OnStopped();
     86      }
    8087    }
    8188
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs

    r5624 r5649  
    4242  [StorableClass]
    4343  public sealed class LinearRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
    44     private const string LinearRegressionModelResultName = "LinearRegressionModel";
     44    private const string LinearRegressionModelResultName = "Linear regression solution";
    4545
    4646    [StorableConstructor]
     
    5151    public LinearRegression()
    5252      : base() {
     53      Problem = new RegressionProblem();
    5354    }
    5455    [StorableHook(HookType.AfterDeserialization)]
     
    6364      double rmsError, cvRmsError;
    6465      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 model on 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 model via 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)));
    6869    }
    6970
     
    7172      Dataset dataset = problemData.Dataset;
    7273      string targetVariable = problemData.TargetVariable;
    73       IEnumerable<string> allowedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
     74      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
    7475      int samplesStart = problemData.TrainingPartitionStart.Value;
    7576      int samplesEnd = problemData.TrainingPartitionEnd.Value;
    76 
    77       IEnumerable<string> selectedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
    7877
    7978      double[,] inputMatrix = LinearRegressionUtil.PrepareInputMatrix(dataset, targetVariable, allowedInputVariables, samplesStart, samplesEnd);
     
    8786      int retVal = 1;
    8887      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");
    9089      rmsError = ar.rmserror;
    9190      cvRmsError = ar.cvrmserror;
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs

    r5626 r5649  
    8888    public SupportVectorClassification()
    8989      : base() {
     90      Problem = new ClassificationProblem();
     91
    9092      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVC", "EPSILON_SVC" }
    9193                                    select new StringValue(type).AsReadOnly())
     
    9597                                       select new StringValue(type).AsReadOnly())
    9698                                   .ToList();
    97       ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(svrTypes);
     99      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
    98100      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
    99101      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
     
    112114    protected override void Run() {
    113115      IClassificationProblemData problemData = Problem.ProblemData;
    114       IEnumerable<string> selectedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
     116      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
    115117      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value);
    116118
     
    140142      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    141143      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);
    145145
    146       return new SupportVectorClassificationSolution(model, problemData, allowedInputVariables, double.NegativeInfinity, double.PositiveInfinity);
     146      return new SupportVectorClassificationSolution(model, problemData);
    147147    }
    148148    #endregion
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs

    r5626 r5649  
    4141    }
    4242
    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 
    6243    public Dataset SupportVectors {
    6344      get { return CalculateSupportVectors(); }
     
    6849    private SupportVectorClassificationSolution(SupportVectorClassificationSolution original, Cloner cloner)
    6950      : base(original, cloner) {
    70       inputVariables = new List<string>(original.inputVariables);
    71       lowerEstimationLimit = original.lowerEstimationLimit;
    72       upperEstimationLimit = original.upperEstimationLimit;
    7351    }
    74     public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
     52    public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData)
    7553      : base(model, problemData) {
    76       this.inputVariables = new List<string>(inputVariables);
    77       this.lowerEstimationLimit = lowerEstimationLimit;
    78       this.upperEstimationLimit = upperEstimationLimit;
    7954    }
    8055
     
    8762      base.OnProblemDataChanged(e);
    8863    }
    89    
     64
    9065    private Dataset CalculateSupportVectors() {
    9166      if (Model.Model.SupportVectorIndizes.Length == 0)
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r5626 r5649  
    3030using SVM;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using System.Drawing;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3839  [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
    3940  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() { }
    4941
    5042    private SVM.Model model;
     
    7769      }
    7870    }
     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
    79101    #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);
    82104    }
    83105    #endregion
    84106    #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);
    91109    }
    92110    #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);
    96113      SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
    97114
     
    149166    }
    150167    #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     }
    194168  }
    195169}
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs

    r5626 r5649  
    9595    public SupportVectorRegression()
    9696      : base() {
     97      Problem = new RegressionProblem();
     98
    9799      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
    98100                                    select new StringValue(type).AsReadOnly())
     
    102104                                       select new StringValue(type).AsReadOnly())
    103105                                   .ToList();
    104       ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(svrTypes);
     106      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
    105107      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
    106108      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
     
    120122    protected override void Run() {
    121123      IRegressionProblemData problemData = Problem.ProblemData;
    122       IEnumerable<string> selectedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
     124      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
    123125      var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value);
    124126
     
    149151      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    150152      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);
    156155    }
    157156    #endregion
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs

    r5626 r5649  
    4141    }
    4242
    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 
    6243    public Dataset SupportVectors {
    6344      get { return CalculateSupportVectors(); }
     
    6849    private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner)
    6950      : base(original, cloner) {
    70       inputVariables = new List<string>(original.inputVariables);
    71       lowerEstimationLimit = original.lowerEstimationLimit;
    72       upperEstimationLimit = original.upperEstimationLimit;
    7351    }
    74     public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
     52    public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData)
    7553      : base(model, problemData) {
    76       this.inputVariables = new List<string>(inputVariables);
    77       this.lowerEstimationLimit = lowerEstimationLimit;
    78       this.upperEstimationLimit = upperEstimationLimit;
    7954    }
    8055
Note: See TracChangeset for help on using the changeset viewer.