Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5626


Ignore:
Timestamp:
03/07/11 18:45:46 (14 years ago)
Author:
gkronber
Message:

#1418 implemented support vector classification algorithm.

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4
Files:
2 added
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r5624 r5626  
    120120    </Compile>
    121121    <Compile Include="Properties\AssemblyInfo.cs" />
     122    <Compile Include="SupportVectorMachine\SupportVectorClassification.cs" />
     123    <Compile Include="SupportVectorMachine\SupportVectorClassificationSolution.cs" />
     124    <Compile Include="SupportVectorMachine\SupportVectorMachineModel.cs" />
    122125    <Compile Include="SupportVectorMachine\SupportVectorMachineUtil.cs">
    123126      <SubType>Code</SubType>
    124127    </Compile>
    125128    <Compile Include="SupportVectorMachine\SupportVectorRegression.cs">
    126       <SubType>Code</SubType>
    127     </Compile>
    128     <Compile Include="SupportVectorMachine\SupportVectorRegressionModel.cs">
    129129      <SubType>Code</SubType>
    130130    </Compile>
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r5624 r5626  
    3333namespace HeuristicLab.Algorithms.DataAnalysis {
    3434  /// <summary>
    35   /// Represents a support vector machine regression model.
     35  /// Represents a support vector machine model.
    3636  /// </summary>
    3737  [StorableClass]
    38   [Item("SupportVectorRegressionModel", "Represents a support vector machine regression model.")]
    39   public sealed class SupportVectorRegressionModel : NamedItem, IRegressionModel {
     38  [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
     39  public sealed class SupportVectorMachineModel : NamedItem, IRegressionModel, IClassificationModel {
    4040    [StorableConstructor]
    41     private SupportVectorRegressionModel(bool deserializing) : base(deserializing) { }
    42     private SupportVectorRegressionModel(SupportVectorRegressionModel original, Cloner cloner)
     41    private SupportVectorMachineModel(bool deserializing) : base(deserializing) { }
     42    private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)
    4343      : base(original, cloner) {
    4444      // only using a shallow copy here! (gkronber)
     
    4646      this.rangeTransform = original.rangeTransform;
    4747    }
    48     public SupportVectorRegressionModel() : base() { }
     48    public SupportVectorMachineModel() : base() { }
    4949
    5050    private SVM.Model model;
     
    7777      }
    7878    }
     79    #region IRegressionModel Members
     80    public IEnumerable<double> GetEstimatedValues(IRegressionProblemData problemData, IEnumerable<int> rows) {
     81      return GetEstimatedValues(problemData, problemData.TargetVariable, rows);
     82    }
     83    #endregion
     84    #region IClassificationModel Members
     85    public IEnumerable<double> GetEstimatedValues(IClassificationProblemData problemData, IEnumerable<int> rows) {
     86      return GetEstimatedValues(problemData, problemData.TargetVariable, rows);
     87    }
    7988
    80     public IEnumerable<double> GetEstimatedValues(IRegressionProblemData problemData, IEnumerable<int> rows) {
     89    public IEnumerable<double> GetEstimatedClassValues(IClassificationProblemData problemData, IEnumerable<int> rows) {
     90      return GetEstimatedValues(problemData, problemData.TargetVariable, rows);
     91    }
     92    #endregion
     93    private IEnumerable<double> GetEstimatedValues(IDataAnalysisProblemData problemData, string targetVariable, IEnumerable<int> rows) {
    8194      var allowedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
    82       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData.Dataset, problemData.TargetVariable, allowedInputVariables, rows);
     95      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData.Dataset, targetVariable, allowedInputVariables, rows);
    8396      SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
    8497
     
    87100              .ToList();
    88101    }
    89 
    90102    #region events
    91103    public event EventHandler Changed;
     
    139151
    140152    public override IDeepCloneable Clone(Cloner cloner) {
    141       return new SupportVectorRegressionModel(this, cloner);
     153      return new SupportVectorMachineModel(this, cloner);
    142154    }
    143155
     
    145157    ///  Exports the <paramref name="model"/> in string representation to stream <paramref name="s"/>
    146158    /// </summary>
    147     /// <param name="model">The support vector regression model to export</param>
     159    /// <param name="model">The support vector machine model to export</param>
    148160    /// <param name="s">The stream to export the model to</param>
    149     public static void Export(SupportVectorRegressionModel model, Stream s) {
     161    public static void Export(SupportVectorMachineModel model, Stream s) {
    150162      StreamWriter writer = new StreamWriter(s);
    151163      writer.WriteLine("RangeTransform:");
     
    171183    /// <param name="reader">The reader to retrieve the string representation from</param>
    172184    /// <returns>The imported support vector machine model.</returns>
    173     public static SupportVectorRegressionModel Import(TextReader reader) {
    174       SupportVectorRegressionModel model = new SupportVectorRegressionModel();
     185    public static SupportVectorMachineModel Import(TextReader reader) {
     186      SupportVectorMachineModel model = new SupportVectorMachineModel();
    175187      while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform";
    176188      model.RangeTransform = SVM.RangeTransform.Read(reader);
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs

    r5624 r5626  
    9595    public SupportVectorRegression()
    9696      : base() {
    97       StringValue nuSvrType = new StringValue("NU_SVR").AsReadOnly();
    98       StringValue rbfKernelType = new StringValue("RBF").AsReadOnly();
    99       Parameters.Add(new ValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", nuSvrType));
    100       Parameters.Add(new ValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", rbfKernelType));
    101       Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC, one-class SVM and nu-SVR.", new DoubleValue(1.0)));
    102       Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC, epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
     97      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
     98                                    select new StringValue(type).AsReadOnly())
     99                                   .ToList();
     100      ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
     101      List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
     102                                       select new StringValue(type).AsReadOnly())
     103                                   .ToList();
     104      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(svrTypes);
     105      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
     106      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
     107      Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter of the nu-SVR.", new DoubleValue(0.5)));
     108      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
    103109      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
    104       Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(1.0)));
     110      Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
    105111    }
    106112    [StorableHook(HookType.AfterDeserialization)]
     
    143149      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    144150      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    145       var model = new SupportVectorRegressionModel();
     151      var model = new SupportVectorMachineModel();
    146152      model.Model = SVM.Training.Train(scaledProblem, parameter);
    147153      model.RangeTransform = rangeTransform;
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs

    r5624 r5626  
    3737  public sealed class SupportVectorRegressionSolution : RegressionSolution {
    3838
    39     public new SupportVectorRegressionModel Model {
    40       get { return (SupportVectorRegressionModel)base.Model; }
     39    public new SupportVectorMachineModel Model {
     40      get { return (SupportVectorMachineModel)base.Model; }
    4141    }
    42 
    43     //IRegressionModel IRegressionSolution.Model {
    44     //  get { return model; }
    45     //}
    46     //IDataAnalysisModel IDataAnalysisSolution.Model {
    47     //  get { return model; }
    48     //}
    49 
    50     //[Storable]
    51     //private IRegressionProblemData problemData;
    52     //public IRegressionProblemData ProblemData {
    53     //  get { return problemData; }
    54     //}
    55     //IDataAnalysisProblemData IDataAnalysisSolution.ProblemData {
    56     //  get { return ProblemData; }
    57     //}
    5842
    5943    [Storable]
     
    7660    }
    7761
    78 
    79     //public event EventHandler ModelChanged;
    80 
    81     //public event EventHandler ProblemDataChanged;
    82 
    8362    public Dataset SupportVectors {
    8463      get { return CalculateSupportVectors(); }
    8564    }
    86 
    87     //private List<double> estimatedValues;
    8865
    8966    [StorableConstructor]
     
    9168    private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner)
    9269      : base(original, cloner) {
    93       //problemData = cloner.Clone(original.problemData);
    94       //model = cloner.Clone(original.model);
    9570      inputVariables = new List<string>(original.inputVariables);
    9671      lowerEstimationLimit = original.lowerEstimationLimit;
    9772      upperEstimationLimit = original.upperEstimationLimit;
    9873    }
    99     public SupportVectorRegressionSolution(SupportVectorRegressionModel model, IRegressionProblemData problemData, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
     74    public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
    10075      : base(model, problemData) {
    101       //this.problemData = problemData;
    102       //this.model = model;
    10376      this.inputVariables = new List<string>(inputVariables);
    10477      this.lowerEstimationLimit = lowerEstimationLimit;
     
    11588    }
    11689
    117     //public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
    118     //  if (estimatedValues == null) RecalculateEstimatedValues();
    119     //  foreach (int row in rows)
    120     //    yield return estimatedValues[row];
    121     //}
    122 
    123     //public override IEnumerable<double> EstimatedValues {
    124     //  get {
    125     //    if (estimatedValues == null) RecalculateEstimatedValues();
    126     //    return estimatedValues;
    127     //  }
    128     //}
    129 
    130     //public override IEnumerable<double> EstimatedTrainingValues {
    131     //  get {
    132     //    return GetEstimatedValues(ProblemData.TrainingIndizes);
    133     //  }
    134     //}
    135 
    136     //public override IEnumerable<double> EstimatedTestValues {
    137     //  get {
    138     //    return GetEstimatedValues(ProblemData.TestIndizes);
    139     //  }
    140     //}
    14190
    14291    private Dataset CalculateSupportVectors() {
     
    151100      return new Dataset(ProblemData.Dataset.VariableNames, data);
    152101    }
    153 
    154     //protected override void RecalculateEstimatedValues() {
    155     //  Dataset dataset = problemData.Dataset;
    156     //  string targetVariable = problemData.TargetVariable;
    157     //  IEnumerable<string> allowedInputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value);
    158     //  int start = 0;
    159     //  int end = dataset.Rows;
    160     //  IEnumerable<int> rows = Enumerable.Range(start, end - start);
    161     //  SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    162     //  SVM.Problem scaledProblem = SVM.Scaling.Scale(Model.RangeTransform, problem);
    163 
    164     //  estimatedValues = (from row in Enumerable.Range(0, scaledProblem.Count)
    165     //                     let prediction = SVM.Prediction.Predict(Model.Model, scaledProblem.X[row])
    166     //                     let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, prediction))
    167     //                     select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
    168     //}
    169102  }
    170103}
Note: See TracChangeset for help on using the changeset viewer.