Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/14/09 19:56:04 (15 years ago)
Author:
gkronber
Message:
  • introduced a variablename to index mapping for SVM models (to make sure we can use the model for prediction in the model analyzer)
  • added support to enable and disable algorithms in the dispatcher and removed DispatcherBase
  • fixed bugs when calculating variable impacts and reading the final model of GP algorithms

#722 (IModel should provide a Predict() method to get predicted values for an input vector)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs

    r2285 r2290  
    3434  public class Predictor : ItemBase, IPredictor {
    3535    private SVMModel svmModel;
     36    private Dictionary<string, int> variableNames = new Dictionary<string, int>();
    3637    private string targetVariable;
    3738
    3839    public Predictor() : base() { } // for persistence
    3940
    40     public Predictor(SVMModel model, string targetVariable)
     41    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames)
    4142      : base() {
    4243      this.svmModel = model;
    4344      this.targetVariable = targetVariable;
     45      this.variableNames = variableNames;
    4446    }
    4547
     
    4951      RangeTransform transform = svmModel.RangeTransform;
    5052      Model model = svmModel.Model;
     53      // maps columns of the current input dataset to the columns that were originally used in training
     54      Dictionary<int, int> newIndex = new Dictionary<int, int>();
     55      foreach (var pair in variableNames) {
     56        newIndex[input.GetVariableIndex(pair.Key)] = pair.Value;
     57      }
    5158
    52       Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), start, end);
     59      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, start, end);
    5360      Problem scaledProblem = SVM.Scaling.Scale(p, transform);
    5461
     
    7077      clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
    7178      clone.targetVariable = targetVariable;
     79      clone.variableNames = new Dictionary<string, int>(variableNames);
    7280      return clone;
    7381    }
     
    7987      node.Attributes.Append(targetVarAttr);
    8088      node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
     89      XmlNode variablesNode = document.CreateElement("Variables");
     90      foreach (var pair in variableNames) {
     91        XmlNode pairNode = document.CreateElement("Variable");
     92        XmlAttribute nameAttr = document.CreateAttribute("Name");
     93        XmlAttribute indexAttr = document.CreateAttribute("Index");
     94        nameAttr.Value = pair.Key;
     95        indexAttr.Value = XmlConvert.ToString(pair.Value);
     96        pairNode.Attributes.Append(nameAttr);
     97        pairNode.Attributes.Append(indexAttr);
     98        variablesNode.AppendChild(pairNode);
     99      }
     100      node.AppendChild(variablesNode);
    81101      return node;
    82102    }
     
    86106      targetVariable = node.Attributes["TargetVariable"].Value;
    87107      svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
     108
     109      variableNames = new Dictionary<string, int>();
     110      XmlNode variablesNode = node.ChildNodes[1];
     111      foreach (XmlNode pairNode in variablesNode.ChildNodes) {
     112        variableNames[pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value);
     113      }
    88114    }
    89115  }
Note: See TracChangeset for help on using the changeset viewer.