Changeset 2347
- Timestamp:
- 09/14/09 10:08:40 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.SupportVectorMachines/3.2
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs
r2328 r2347 40 40 private Dictionary<string, int> variableNames = new Dictionary<string, int>(); 41 41 private string targetVariable; 42 private int minTimeOffset; 43 private int maxTimeOffset; 42 44 43 45 public Predictor() : base() { } // for persistence 44 46 45 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) 47 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) : 48 this(model, targetVariable, variableNames, 0, 0) { 49 } 50 51 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames, int minTimeOffset, int maxTimeOffset) 46 52 : base() { 47 53 this.svmModel = model; 48 54 this.targetVariable = targetVariable; 49 55 this.variableNames = variableNames; 56 this.minTimeOffset = minTimeOffset; 57 this.maxTimeOffset = maxTimeOffset; 50 58 } 51 59 … … 61 69 } 62 70 63 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, start, end); 71 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, 72 start, end, minTimeOffset, maxTimeOffset); 64 73 Problem scaledProblem = SVM.Scaling.Scale(p, transform); 65 74 … … 82 91 clone.targetVariable = targetVariable; 83 92 clone.variableNames = new Dictionary<string, int>(variableNames); 93 clone.minTimeOffset = minTimeOffset; 94 clone.maxTimeOffset = maxTimeOffset; 84 95 return clone; 85 96 } … … 90 101 targetVarAttr.Value = targetVariable; 91 102 node.Attributes.Append(targetVarAttr); 103 XmlAttribute minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset"); 104 XmlAttribute maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset"); 105 minTimeOffsetAttr.Value = XmlConvert.ToString(minTimeOffset); 106 maxTimeOffsetAttr.Value = XmlConvert.ToString(maxTimeOffset); 107 node.Attributes.Append(minTimeOffsetAttr); 108 node.Attributes.Append(maxTimeOffsetAttr); 92 109 node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects)); 93 110 XmlNode variablesNode = document.CreateElement("Variables"); … … 111 128 svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects); 112 129 130 if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value); 131 if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value); 113 132 variableNames = new Dictionary<string, int>(); 114 133 XmlNode variablesNode = node.ChildNodes[1]; -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs
r2290 r2347 10 10 public class SVMHelper { 11 11 12 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end ) {13 return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end );12 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end, int minTimeOffset, int maxTimeOffset) { 13 return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end, minTimeOffset, maxTimeOffset); 14 14 } 15 15 16 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end ) {16 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end, int minTimeOffset, int maxTimeOffset) { 17 17 int rowCount = end - start; 18 18 List<int> skippedFeatures = new List<int>(); … … 24 24 } 25 25 26 int maxColumns = dataset.Columns - skippedFeatures.Count();26 int maxColumns = (dataset.Columns - skippedFeatures.Count()) * (maxTimeOffset-minTimeOffset); 27 27 28 28 double[] targetVector = new double[rowCount]; … … 36 36 List<SVM.Node> tempRow; 37 37 int addedRows = 0; 38 int timeOffsetBase = columnMapping.Count; 38 39 for (int row = 0; row < rowCount; row++) { 39 40 tempRow = new List<SVM.Node>(); 40 41 for (int col = 0; col < dataset.Columns; col++) { 41 42 if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) { 42 double value = dataset.GetValue(start + row, col); 43 if (!double.IsNaN(value)) 44 tempRow.Add(new SVM.Node(columnMapping[col], value)); 43 for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) { 44 int actualColumn = columnMapping[col] * (maxTimeOffset - minTimeOffset) + (timeOffset - maxTimeOffset) + 1; 45 double value = dataset.GetValue(start + row + timeOffset, col); 46 if (!double.IsNaN(value)) 47 tempRow.Add(new SVM.Node(actualColumn, value)); 48 } 45 49 } 46 50 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs
r2165 r2347 42 42 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 43 43 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In)); 45 AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal time offset for time-series prognosis", typeof(IntData), VariableKind.In)); 44 46 45 47 //SVM parameters … … 69 71 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 70 72 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 71 73 int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data; 74 int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data; 72 75 string svmType = GetVariableValue<StringData>("SVMType", scope, true).Data; 73 76 string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data; … … 81 84 parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data; 82 85 83 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end );86 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset); 84 87 SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem); 85 88 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform); -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs
r2165 r2347 38 38 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 39 39 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 40 40 AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal allowed time offset for input variables", typeof(IntData), VariableKind.In)); 41 AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal allowed time offset for input variables", typeof(IntData), VariableKind.In)); 41 42 AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.In)); 42 43 AddVariableInfo(new VariableInfo("Values", "Target vs predicted values", typeof(DoubleMatrixData), VariableKind.New | VariableKind.Out)); … … 49 50 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 50 51 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 51 52 int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data; 53 int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data; 52 54 SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true); 53 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end );55 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset); 54 56 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, modelData.RangeTransform); 55 57 56 58 double[,] values = new double[scaledProblem.Count, 2]; 57 59 for (int i = 0; i < scaledProblem.Count; i++) { 58 values[i, 0] = SVM.Prediction.Predict(modelData.Model, scaledProblem.X[i]);59 values[i, 1] = dataset.GetValue(start + i,targetVariable);60 values[i, 0] = SVM.Prediction.Predict(modelData.Model, scaledProblem.X[i]); 61 values[i, 1] = dataset.GetValue(start + i, targetVariable); 60 62 } 61 63 -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs
r2344 r2347 107 107 } 108 108 109 public int MaxTimeOffset { 110 get { return GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data; } 111 set { GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data = value; } 112 } 113 114 public int MinTimeOffset { 115 get { return GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data; } 116 set { GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data = value; } 117 } 118 109 119 public SupportVectorRegression() { 110 120 engine = new SequentialEngine.SequentialEngine(); … … 115 125 MaxNuIndex = NuList.Data.Length; 116 126 MaxGammaIndex = GammaList.Data.Length; 127 MaxTimeOffset = 0; 128 MinTimeOffset = 0; 117 129 } 118 130 … … 147 159 seq.AddSubOperator(new RandomInjector()); 148 160 149 DatasetShuffler shuffler = new DatasetShuffler();150 shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";151 shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";152 seq.AddSubOperator(shuffler);161 //DatasetShuffler shuffler = new DatasetShuffler(); 162 //shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart"; 163 //shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd"; 164 //seq.AddSubOperator(shuffler); 153 165 return seq; 154 166 } … … 395 407 injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR"))); 396 408 injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0))); 397 409 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTimeOffset", new IntData())); 410 injector.AddVariable(new HeuristicLab.Core.Variable("MinTimeOffset", new IntData())); 398 411 return injector; 399 412 }
Note: See TracChangeset
for help on using the changeset viewer.