Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5690 for branches


Ignore:
Timestamp:
03/15/11 15:35:32 (13 years ago)
Author:
gkronber
Message:

#1418 removed SupportVectorIndizes field from libSVM models.

Location:
branches/DataAnalysis Refactoring
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs

    r5649 r5690  
    142142      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    143143      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    144       var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables);
     144      var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
    145145
    146146      return new SupportVectorClassificationSolution(model, problemData);
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs

    r5649 r5690  
    4141    }
    4242
    43     public Dataset SupportVectors {
    44       get { return CalculateSupportVectors(); }
    45     }
    46 
    4743    [StorableConstructor]
    4844    private SupportVectorClassificationSolution(bool deserializing) : base(deserializing) { }
     
    5753      return new SupportVectorClassificationSolution(this, cloner);
    5854    }
    59 
    60     protected override void OnProblemDataChanged(EventArgs e) {
    61       Model.Model.SupportVectorIndizes = new int[0];
    62       base.OnProblemDataChanged(e);
    63     }
    64 
    65     private Dataset CalculateSupportVectors() {
    66       if (Model.Model.SupportVectorIndizes.Length == 0)
    67         return new Dataset(new List<string>(), new double[0, 0]);
    68 
    69       double[,] data = new double[Model.Model.SupportVectorIndizes.Length, ProblemData.Dataset.Columns];
    70       for (int i = 0; i < Model.Model.SupportVectorIndizes.Length; i++) {
    71         for (int column = 0; column < ProblemData.Dataset.Columns; column++)
    72           data[i, column] = ProblemData.Dataset[Model.Model.SupportVectorIndizes[i], column];
    73       }
    74       return new Dataset(ProblemData.Dataset.VariableNames, data);
    75     }
    7655  }
    7756}
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r5649 r5690  
    7070    }
    7171
     72    public Dataset SupportVectors {
     73      get {
     74        var data = new double[Model.SupportVectorCount, allowedInputVariables.Count()];
     75        for (int i = 0; i < Model.SupportVectorCount; i++) {
     76          var sv = Model.SupportVectors[i];
     77          for (int j = 0; j < sv.Length; j++) {
     78            data[i, sv[j].Index] = sv[j].Value;
     79          }
     80        }
     81        return new Dataset(allowedInputVariables, data);
     82      }
     83    }
     84
    7285    [Storable]
    7386    private string targetVariable;
    7487    [Storable]
    7588    private string[] allowedInputVariables;
     89    [Storable]
     90    private double[] classValues; // only for SVM classification models
    7691
    7792    [StorableConstructor]
     
    8297      this.model = original.model;
    8398      this.rangeTransform = original.rangeTransform;
    84 
     99      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     100      if (original.classValues != null)
     101        this.classValues = (double[])original.classValues.Clone();
     102    }
     103    public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
     104      : this(model, rangeTransform, targetVariable, allowedInputVariables) {
     105      this.classValues = classValues.ToArray();
    85106    }
    86107    public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
     
    106127    #region IClassificationModel Members
    107128    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
    108       return GetEstimatedValuesHelper(dataset, rows);
     129      if (classValues == null) throw new NotSupportedException();
     130      // return the original class value instead of the predicted value of the model
     131      // svm classification only works for integer classes
     132      foreach (var estimated in GetEstimatedValuesHelper(dataset, rows)) {
     133        // find closest class
     134        double bestDist = double.MaxValue;
     135        double bestClass = -1;
     136        for (int i = 0; i < classValues.Length; i++) {
     137          double d = Math.Abs(estimated - classValues[i]);
     138          if (d < bestDist) {
     139            bestDist = d;
     140            bestClass = classValues[i];
     141            if (d.IsAlmost(0.0)) break; // exact match no need to look further
     142          }
     143        }
     144        yield return bestClass;
     145      }
    109146    }
    110147    #endregion
     
    113150      SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
    114151
    115       return (from row in Enumerable.Range(0, scaledProblem.Count)
    116               select SVM.Prediction.Predict(Model, scaledProblem.X[row]))
    117               .ToList();
     152      foreach (var row in Enumerable.Range(0, scaledProblem.Count)) {
     153        yield return SVM.Prediction.Predict(Model, scaledProblem.X[row]);
     154      }
    118155    }
    119156    #region events
     
    127164
    128165    #region persistence
    129     [Storable]
    130     private int[] SupportVectorIndizes {
    131       get { return this.Model.SupportVectorIndizes; }
    132       set { this.Model.SupportVectorIndizes = value; }
    133     }
    134 
    135166    [Storable]
    136167    private string ModelAsString {
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs

    r5649 r5690  
    4141    }
    4242
    43     public Dataset SupportVectors {
    44       get { return CalculateSupportVectors(); }
    45     }
    46 
    4743    [StorableConstructor]
    4844    private SupportVectorRegressionSolution(bool deserializing) : base(deserializing) { }
     
    5753      return new SupportVectorRegressionSolution(this, cloner);
    5854    }
    59 
    60     protected override void OnProblemDataChanged(EventArgs e) {
    61       Model.Model.SupportVectorIndizes = new int[0];
    62       base.OnProblemDataChanged(e);
    63     }
    64 
    65 
    66     private Dataset CalculateSupportVectors() {
    67       if (Model.Model.SupportVectorIndizes.Length == 0)
    68         return new Dataset(new List<string>(), new double[0, 0]);
    69 
    70       double[,] data = new double[Model.Model.SupportVectorIndizes.Length, ProblemData.Dataset.Columns];
    71       for (int i = 0; i < Model.Model.SupportVectorIndizes.Length; i++) {
    72         for (int column = 0; column < ProblemData.Dataset.Columns; column++)
    73           data[i, column] = ProblemData.Dataset[Model.Model.SupportVectorIndizes[i], column];
    74       }
    75       return new Dataset(ProblemData.Dataset.VariableNames, data);
    76     }
    7755  }
    7856}
  • branches/DataAnalysis Refactoring/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Model.cs

    r4545 r5690  
    3131    private int _numberOfClasses;
    3232    private int _supportVectorCount;
    33     private int[] _supportVectorIndizes;
    3433    private Node[][] _supportVectors;
    3534    private double[][] _supportVectorCoefficients;
     
    7776      set {
    7877        _supportVectorCount = value;
    79       }
    80     }
    81 
    82     /// <summary>
    83     /// Indizes of support vectors identified in the training.
    84     /// </summary>
    85     public int[] SupportVectorIndizes {
    86       get {
    87         return _supportVectorIndizes;
    88       }
    89       set {
    90         _supportVectorIndizes = value;
    9178      }
    9279    }
     
    215202      model.ClassLabels = null;
    216203      model.NumberOfSVPerClass = null;
    217       model.SupportVectorIndizes = new int[0];
    218204
    219205      bool headerFinished = false;
     
    416402        output.Write("nr_sv");
    417403        for (int i = 0; i < nr_class; i++)
    418           output.Write(" " + model.NumberOfSVPerClass[i].ToString("r"));
     404          output.Write(" " + model.NumberOfSVPerClass[i]);
    419405        output.Write(Environment.NewLine);
    420406      }
  • branches/DataAnalysis Refactoring/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Solver.cs

    r4068 r5690  
    14601460          if (Math.Abs(f.alpha[i]) > 0) ++nSV;
    14611461        model.SupportVectorCount = nSV;
    1462         model.SupportVectorIndizes = new int[nSV];
    14631462        model.SupportVectors = new Node[nSV][];
    14641463        model.SupportVectorCoefficients[0] = new double[nSV];
     
    14681467          if (Math.Abs(f.alpha[i]) > 0) {
    14691468            model.SupportVectors[j] = prob.X[i];
    1470             model.SupportVectorIndizes[j] = i;
    14711469            model.SupportVectorCoefficients[0][j] = f.alpha[i];
    14721470
     
    15961594        model.SupportVectorCount = nnz;
    15971595        model.SupportVectors = new Node[nnz][];
    1598         model.SupportVectorIndizes = new int[nnz];
    15991596        p = 0;
    16001597        for (i = 0; i < l; i++) {
    16011598          if (nonzero[i]) {
    16021599            model.SupportVectors[p] = x[i];
    1603             model.SupportVectorIndizes[p] = i;
    16041600            p++;
    16051601          }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression/SupportVectorRegressionSolution.cs

    r5445 r5690  
    4646    }
    4747
    48     public Dataset SupportVectors {
    49       get { return CalculateSupportVectors(); }
    50     }
    51 
    5248    private List<double> estimatedValues;
    5349    public override IEnumerable<double> EstimatedValues {
     
    7066    }
    7167
     68    public Dataset SupportVectors {
     69      get {
     70        int nCol = inputVariables.Count;
     71        double[,] data = new double[Model.Model.SupportVectorCount, nCol];
     72        int row = 0;
     73        foreach (var sv in Model.SupportVectors) {
     74          for (int col = 0; col < nCol; col++) {
     75            data[row, col] = sv[col];
     76          }
     77          row++;
     78        }
     79        return new Dataset(inputVariables, data);
     80      }
     81    }
     82
     83    [Storable]
     84    private List<string> inputVariables;
     85
    7286    [StorableConstructor]
    7387    private SupportVectorRegressionSolution(bool deserializing) : base(deserializing) { }
    74     private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner) : base(original, cloner) { }
     88    private SupportVectorRegressionSolution(SupportVectorRegressionSolution original, Cloner cloner)
     89      : base(original, cloner) {
     90      this.inputVariables = new List<string>(original.inputVariables);
     91    }
    7592    public SupportVectorRegressionSolution() : base() { }
    7693    public SupportVectorRegressionSolution(DataAnalysisProblemData problemData, SupportVectorMachineModel model, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
    7794      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
    7895      this.Model = model;
     96      this.inputVariables = new List<string>(inputVariables);
     97    }
     98
     99    [StorableHook(HookType.AfterDeserialization)]
     100    private void AfterDeserialization() {
     101      #region backwards compatibility
     102      if (inputVariables == null) inputVariables = ProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value).ToList();
     103      #endregion
    79104    }
    80105
    81106    public override IDeepCloneable Clone(Cloner cloner) {
    82107      return new SupportVectorRegressionSolution(this, cloner);
    83     }
    84 
    85     protected override void OnProblemDataChanged() {
    86       Model.Model.SupportVectorIndizes = new int[0];
    87       base.OnProblemDataChanged();
    88     }
    89 
    90     private Dataset CalculateSupportVectors() {
    91       if (Model.Model.SupportVectorIndizes.Length == 0)
    92         return new Dataset(new List<string>(), new double[0, 0]);
    93 
    94       double[,] data = new double[Model.Model.SupportVectorIndizes.Length, ProblemData.Dataset.Columns];
    95       for (int i = 0; i < Model.Model.SupportVectorIndizes.Length; i++) {
    96         for (int column = 0; column < ProblemData.Dataset.Columns; column++)
    97           data[i, column] = ProblemData.Dataset[Model.Model.SupportVectorIndizes[i], column];
    98       }
    99       return new Dataset(ProblemData.Dataset.VariableNames, data);
    100108    }
    101109
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/SupportVectorMachineModel.cs

    r5445 r5690  
    3737  [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
    3838  public sealed class SupportVectorMachineModel : NamedItem, IDataAnalysisModel {
    39     [StorableConstructor]
    40     private SupportVectorMachineModel(bool deserializing) : base(deserializing) { }
    41     private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)
    42       : base(original, cloner) {
    43       // only using a shallow copy here! (gkronber)
    44       this.model = original.model;
    45       this.rangeTransform = original.rangeTransform;
    46     }
    47     public SupportVectorMachineModel() : base() { }
    48 
    4939    private SVM.Model model;
    5040    /// <summary>
     
    7767    }
    7868
     69    public IEnumerable<double[]> SupportVectors {
     70      get {
     71        return from sv in Model.SupportVectors
     72               select (from svx in sv
     73                       select svx.Value).ToArray();
     74      }
     75    }
     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    public SupportVectorMachineModel() : base() { }
     86
    7987    public IEnumerable<double> GetEstimatedValues(DataAnalysisProblemData problemData, int start, int end) {
    8088      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, Enumerable.Range(start, end - start));
     
    96104
    97105    #region persistence
    98     [Storable]
    99     private int[] SupportVectorIndizes {
    100       get { return this.Model.SupportVectorIndizes; }
    101       set { this.Model.SupportVectorIndizes = value; }
    102     }
    103 
    104106    [Storable]
    105107    private string ModelAsString {
Note: See TracChangeset for help on using the changeset viewer.