Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15854 for trunk


Ignore:
Timestamp:
03/22/18 13:13:05 (7 years ago)
Author:
gkronber
Message:

#2905: don't include the target variable in the svm_problem when a trained SVM is used for prediction

Location:
trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r15583 r15854  
    155155    private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
    156156      // calculate predictions for the currently requested rows
    157       svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, TargetVariable, allowedInputVariables, rows);
     157      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, allowedInputVariables, rows);
    158158      svm_problem scaledProblem = rangeTransform.Scale(problem);
    159159
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs

    r15583 r15854  
    3535  public class SupportVectorMachineUtil {
    3636    /// <summary>
    37     /// Transforms <paramref name="problemData"/> into a data structure as needed by libSVM.
    38     /// </summary>
    39     /// <param name="problemData">The problem data to transform</param>
     37    /// Transforms <paramref name="dataset"/> into a data structure as needed by libSVM.
     38    /// </summary>
     39    /// <param name="dataset">The source dataset</param>
     40    /// <param name="targetVariable">The target variable</param>
     41    /// <param name="inputVariables">The selected input variables to include in the svm_problem.</param>
    4042    /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param>
    4143    /// <returns>A problem data type that can be used to train a support vector machine.</returns>
    4244    public static svm_problem CreateSvmProblem(IDataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
    43       double[] targetVector = dataset.GetDoubleValues(targetVariable, rowIndices).ToArray();
    44       svm_node[][] nodes = new svm_node[targetVector.Length][];
     45      double[] targetVector ;
     46      var nRows = rowIndices.Count();
     47      if (string.IsNullOrEmpty(targetVariable)) {
     48        // if the target variable is not set (e.g. for prediction of a trained model) we just use a zero vector
     49        targetVector = new double[nRows];
     50      } else {
     51        targetVector = dataset.GetDoubleValues(targetVariable, rowIndices).ToArray();
     52      }
     53      svm_node[][] nodes = new svm_node[nRows][];
    4554      int maxNodeIndex = 0;
    4655      int svmProblemRowIndex = 0;
     
    6675
    6776    /// <summary>
     77    /// Transforms <paramref name="dataset"/> into a data structure as needed by libSVM for prediction.
     78    /// </summary>
     79    /// <param name="dataset">The problem data to transform</param>
     80    /// <param name="inputVariables">The selected input variables to include in the svm_problem.</param>
     81    /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param>
     82    /// <returns>A problem data type that can be used for prediction with a trained support vector machine.</returns>
     83    public static svm_problem CreateSvmProblem(IDataset dataset, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
     84      // for prediction we don't need a target variable
     85      return CreateSvmProblem(dataset, string.Empty, inputVariables, rowIndices);
     86    }
     87
     88    /// <summary>
    6889    /// Instantiate and return a svm_parameter object with default values.
    6990    /// </summary>
Note: See TracChangeset for help on using the changeset viewer.