Changeset 2421
- Timestamp:
- 10/08/09 16:22:35 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Modeling/3.2/AnalyzerModel.cs
r2371 r2421 112 112 113 113 public IEnumerable<KeyValuePair<ModelingResult, double>> GetVariableResults(string variableName) { 114 return variableResults[variableName]; 114 if (variableResults.ContainsKey(variableName)) 115 return variableResults[variableName]; 116 else return new KeyValuePair<ModelingResult, double>[] { }; 115 117 } 116 118 -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/HeuristicLab.SupportVectorMachines-3.2.csproj
r2361 r2421 112 112 </ItemGroup> 113 113 <ItemGroup> 114 <ProjectReference Include="..\..\HeuristicLab.Common\3.2\HeuristicLab.Common-3.2.csproj"> 115 <Project>{1FC004FC-59AF-4249-B1B6-FF25873A20E4}</Project> 116 <Name>HeuristicLab.Common-3.2</Name> 117 </ProjectReference> 114 118 <ProjectReference Include="..\..\HeuristicLab.Core\3.2\HeuristicLab.Core-3.2.csproj"> 115 119 <Project>{F43B59AB-2B8C-4570-BC1E-15592086517C}</Project> -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs
r2418 r2421 39 39 } 40 40 41 private Dictionary<string, int> variableNames = new Dictionary<string, int>();41 private List<string> variableNames; 42 42 private string targetVariable; 43 43 private int minTimeOffset; … … 52 52 public Predictor() : base() { } // for persistence 53 53 54 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) :54 public Predictor(SVMModel model, string targetVariable, IEnumerable<string> variableNames) : 55 55 this(model, targetVariable, variableNames, 0, 0) { 56 56 } 57 57 58 public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames, int minTimeOffset, int maxTimeOffset)58 public Predictor(SVMModel model, string targetVariable, IEnumerable<string> variableNames, int minTimeOffset, int maxTimeOffset) 59 59 : base() { 60 60 this.svmModel = model; 61 61 this.targetVariable = targetVariable; 62 this.variableNames = variableNames;62 this.variableNames = new List<string>(variableNames); 63 63 this.minTimeOffset = minTimeOffset; 64 64 this.maxTimeOffset = maxTimeOffset; … … 70 70 RangeTransform transform = svmModel.RangeTransform; 71 71 Model model = svmModel.Model; 72 // maps columns of the current input dataset to the columns that were originally used in training73 Dictionary<int, int> newIndex = new Dictionary<int, int>();74 foreach (var pair in variableNames) {75 newIndex[input.GetVariableIndex(pair.Key)] = pair.Value;76 }77 72 78 79 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, 73 Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), variableNames, 80 74 start, end, minTimeOffset, maxTimeOffset); 81 75 Problem scaledProblem = transform.Scale(p); … … 88 82 if (double.IsNaN(input.GetValue(resultRow, targetVariableIndex))) 89 83 result[resultRow] = UpperPredictionLimit; 90 else 84 else if (resultRow + maxTimeOffset < 0) { 85 result[resultRow] = UpperPredictionLimit; 86 problemRow++; 87 } else { 91 88 result[resultRow] = Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[problemRow++]), UpperPredictionLimit), LowerPredictionLimit); 89 } 92 90 } 93 91 return result; … … 95 93 96 94 public override IEnumerable<string> GetInputVariables() { 97 return from pair in variableNames 98 where pair.Key != targetVariable 99 orderby pair.Value 100 select pair.Key; 95 return variableNames; 101 96 } 102 97 … … 109 104 clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects); 110 105 clone.targetVariable = targetVariable; 111 clone.variableNames = new Dictionary<string, int>(variableNames);106 clone.variableNames = new List<string>(variableNames); 112 107 clone.minTimeOffset = minTimeOffset; 113 108 clone.maxTimeOffset = maxTimeOffset; … … 128 123 node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects)); 129 124 XmlNode variablesNode = document.CreateElement("Variables"); 130 foreach (var pairin variableNames) {131 XmlNode pairNode = document.CreateElement("Variable");125 foreach (var variableName in variableNames) { 126 XmlNode variableNameNode = document.CreateElement("Variable"); 132 127 XmlAttribute nameAttr = document.CreateAttribute("Name"); 133 XmlAttribute indexAttr = document.CreateAttribute("Index"); 134 nameAttr.Value = pair.Key; 135 indexAttr.Value = XmlConvert.ToString(pair.Value); 136 pairNode.Attributes.Append(nameAttr); 137 pairNode.Attributes.Append(indexAttr); 138 variablesNode.AppendChild(pairNode); 128 nameAttr.Value = variableName; 129 variableNameNode.Attributes.Append(nameAttr); 130 variablesNode.AppendChild(variableNameNode); 139 131 } 140 132 node.AppendChild(variablesNode); … … 149 141 if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value); 150 142 if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value); 151 variableNames = new Dictionary<string, int>();143 variableNames = new List<string>(); 152 144 XmlNode variablesNode = node.ChildNodes[1]; 153 foreach (XmlNode pairNode in variablesNode.ChildNodes) {154 variableNames [pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value);145 foreach (XmlNode variableNameNode in variablesNode.ChildNodes) { 146 variableNames.Add(variableNameNode.Attributes["Name"].Value); 155 147 } 156 148 } … … 190 182 p.maxTimeOffset = int.Parse(maxTimeOffsetLine[1]); 191 183 p.minTimeOffset = int.Parse(minTimeOffsetLine[1]); 192 int i = 1;193 184 foreach (string inputVariable in inputVariableLine.Skip(1)) { 194 p.variableNames [inputVariable.Trim()] = i++;185 p.variableNames.Add(inputVariable.Trim()); 195 186 } 196 187 p.svmModel = SVMModel.Import(reader); -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/PredictorBuilder.cs
r2373 r2421 36 36 AddVariableInfo(new VariableInfo("SVMModel", "The SVM model", typeof(SVMModel), VariableKind.In)); 37 37 AddVariableInfo(new VariableInfo("TargetVariable", "The target variable", typeof(StringData), VariableKind.In)); 38 AddVariableInfo(new VariableInfo("InputVariables", "The input variable names", typeof( StringData), VariableKind.In));38 AddVariableInfo(new VariableInfo("InputVariables", "The input variable names", typeof(ItemList), VariableKind.In)); 39 39 AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of the training set", typeof(IntData), VariableKind.In)); 40 40 AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of the training set", typeof(IntData), VariableKind.In)); … … 63 63 string targetVariableName = ds.GetVariableName(targetVariable); 64 64 ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true); 65 Dictionary<string, int> variableNames = new Dictionary<string, int>();66 for (int i = 0; i < ds.Columns; i++) variableNames[ds.GetVariableName(i)] = i;65 var inputVariableNames = from x in inputVariables 66 select ((StringData)x).Data; 67 67 68 68 double mean = ds.GetMean(targetVariable, start, end); 69 69 double range = ds.GetRange(targetVariable, start, end); 70 70 71 Predictor predictor = new Predictor(model, targetVariableName, variableNames, minTimeOffset, maxTimeOffset);71 Predictor predictor = new Predictor(model, targetVariableName, inputVariableNames, minTimeOffset, maxTimeOffset); 72 72 predictor.LowerPredictionLimit = mean - punishmentFactor * range; 73 73 predictor.UpperPredictionLimit = mean + punishmentFactor * range; -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs
r2417 r2421 6 6 using HeuristicLab.Data; 7 7 using HeuristicLab.DataAnalysis; 8 using HeuristicLab.Common; 8 9 9 10 namespace HeuristicLab.SupportVectorMachines { 10 11 public class SVMHelper { 12 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariableIndex, IEnumerable<string> inputVariables, int start, int end, int minTimeOffset, int maxTimeOffset) { 13 int rowCount = end - start; 11 14 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 } 15 var targetVector = (from row in Enumerable.Range(start, rowCount) 16 let val = dataset.GetValue(row, targetVariableIndex) 17 where !double.IsNaN(val) 18 select val).ToArray(); 15 19 16 public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end, int minTimeOffset, int maxTimeOffset) {17 int rowCount = end - start;18 List<int> skippedFeatures = new List<int>();19 for (int i = 0; i < dataset.Columns; i++) {20 if (i != targetVariable) {21 if (dataset.GetRange(i, start, end) == 0)22 skippedFeatures.Add(i);23 }24 }25 26 int maxColumns = 0;27 28 double[] targetVector = new double[rowCount];29 for (int i = 0; i < rowCount; i++) {30 double value = dataset.GetValue(start + i, targetVariable);31 targetVector[i] = value;32 }33 targetVector = targetVector.Where(x => !double.IsNaN(x)).ToArray();34 20 35 21 SVM.Node[][] nodes = new SVM.Node[targetVector.Length][]; 36 22 List<SVM.Node> tempRow; 37 23 int addedRows = 0; 38 int timeOffsetBase = columnMapping.Count;24 int maxColumns = 0; 39 25 for (int row = 0; row < rowCount; row++) { 40 26 tempRow = new List<SVM.Node>(); 41 for (int col = 0; col < dataset.Columns; col++) { 42 if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) { 27 int nodeIndex = 0; 28 foreach (var inputVariable in inputVariables) { 29 ++nodeIndex; 30 int col = dataset.GetVariableIndex(inputVariable); 31 if (IsUsefulColumn(dataset, col, start, end)) { 43 32 for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) { 44 int actualColumn = columnMapping[col] * (maxTimeOffset - minTimeOffset + 1) + (timeOffset - minTimeOffset); 45 double value = dataset.GetValue(start + row + timeOffset, col); 46 if (!double.IsNaN(value)) { 47 tempRow.Add(new SVM.Node(actualColumn, value)); 48 if (actualColumn > maxColumns) maxColumns = actualColumn; 33 int actualColumn = nodeIndex * (maxTimeOffset - minTimeOffset + 1) + (timeOffset - minTimeOffset); 34 if (start + row + timeOffset >= 0 && start + row + timeOffset < dataset.Rows) { 35 double value = dataset.GetValue(start + row + timeOffset, col); 36 if (!double.IsNaN(value)) { 37 tempRow.Add(new SVM.Node(actualColumn, value)); 38 if (actualColumn > maxColumns) maxColumns = actualColumn; 39 } 49 40 } 50 41 } 51 42 } 52 43 } 53 if (!double.IsNaN(dataset.GetValue(start + row, targetVariable ))) {54 nodes[addedRows] = tempRow. OrderBy(x => x.Index).ToArray();44 if (!double.IsNaN(dataset.GetValue(start + row, targetVariableIndex))) { 45 nodes[addedRows] = tempRow.ToArray(); 55 46 addedRows++; 56 47 } … … 59 50 return new SVM.Problem(targetVector.Length, targetVector, nodes, maxColumns); 60 51 } 52 53 // checks if the column has at least two different non-NaN and non-Infinity values 54 private static bool IsUsefulColumn(Dataset dataset, int col, int start, int end) { 55 double min = double.PositiveInfinity; 56 double max = double.NegativeInfinity; 57 for (int i = start; i < end; i++) { 58 double x = dataset.GetValue(i, col); 59 if (!double.IsNaN(x) && !double.IsInfinity(x)) { 60 min = Math.Min(min, x); 61 max = Math.Max(max, x); 62 } 63 if (min != max) return true; 64 } 65 return false; 66 } 61 67 } 62 68 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs
r2415 r2421 41 41 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In)); 42 42 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("InputVariables", "List of allowed input variable names", typeof(ItemList), VariableKind.In)); 43 44 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 44 45 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); … … 70 71 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 71 72 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 73 ItemList inputVaribales = GetVariableValue<ItemList>("InputVariables", scope, true); 74 var inputVariableNames = from x in inputVaribales 75 select ((StringData)x).Data; 72 76 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 73 77 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; … … 89 93 parameter.Probability = false; 90 94 91 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset);95 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, inputVariableNames, start, end, minTimeOffset, maxTimeOffset); 92 96 SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem); 93 97 SVM.Problem scaledProblem = rangeTransform.Scale(problem); -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs
r2415 r2421 37 37 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In)); 38 38 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); 39 AddVariableInfo(new VariableInfo("InputVariables", "List of allowed input variable names", typeof(ItemList), VariableKind.In)); 39 40 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 40 41 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); … … 48 49 public override IOperation Apply(IScope scope) { 49 50 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 51 ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true); 52 var inputVariableNames = from x in inputVariables 53 select ((StringData)x).Data; 50 54 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 51 55 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; … … 56 60 int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data; 57 61 SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true); 58 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset); 62 63 SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, inputVariableNames, start, end, minTimeOffset, maxTimeOffset); 59 64 SVM.Problem scaledProblem = modelData.RangeTransform.Scale(problem); 60 65
Note: See TracChangeset
for help on using the changeset viewer.