Changeset 2360 for trunk/sources/HeuristicLab.LinearRegression
- Timestamp:
- 09/15/09 16:35:54 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.LinearRegression/3.2
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.LinearRegression/3.2/HeuristicLab.LinearRegression-3.2.csproj
r2353 r2360 95 95 <Compile Include="LinearRegression.cs" /> 96 96 <Compile Include="LinearRegressionOperator.cs" /> 97 <Compile Include="LinearTimeSeriesPrognosis.cs" /> 97 98 <Compile Include="Properties\AssemblyInfo.cs" /> 98 99 </ItemGroup> -
trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegression.cs
r2356 r2360 60 60 get { 61 61 IOperator main = GetMainOperator(); 62 return main.SubOperators[ 1];62 return main.SubOperators[2]; 63 63 } 64 64 set { 65 65 IOperator main = GetMainOperator(); 66 main.RemoveSubOperator( 1);67 main.AddSubOperator(value, 1);66 main.RemoveSubOperator(2); 67 main.AddSubOperator(value, 2); 68 68 } 69 69 } … … 197 197 } 198 198 199 protected virtual IOperator GetVariableInjector() { 200 return GetMainOperator().SubOperators[0]; 201 } 202 199 203 public override IView CreateView() { 200 204 return engine.CreateView(); -
trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs
r2324 r2360 41 41 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 42 42 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("MaxTimeOffset", "(optional) Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In)); 44 AddVariableInfo(new VariableInfo("MinTimeOffset", "(optional) Minimal time offset for time-series prognosis", typeof(IntData), VariableKind.In)); 43 45 AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IGeneticProgrammingModel), VariableKind.Out | VariableKind.New)); 44 46 } … … 49 51 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 50 52 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 51 List<int> allowedRows = CalculateAllowedRows(dataset, targetVariable, start, end); 53 IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false); 54 int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data; 55 IntData minTimeOffsetData = GetVariableValue<IntData>("MinTimeOffset", scope, true, false); 56 int minTimeOffset = minTimeOffsetData == null ? 0 : minTimeOffsetData.Data; 57 58 List<int> allowedRows = CalculateAllowedRows(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset); 52 59 List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariable, start, end); 53 60 54 double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows );61 double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset); 55 62 double[] targetVector = PrepareTargetVector(dataset, targetVariable, allowedRows); 56 63 double[] coefficients = CalculateCoefficients(inputMatrix, targetVector); 57 IFunctionTree tree = CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList() );64 IFunctionTree tree = CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList(), minTimeOffset, maxTimeOffset); 58 65 59 66 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), new GeneticProgrammingModel(tree))); … … 61 68 } 62 69 63 private IFunctionTree CreateModel(double[] coefficients, List<string> allowedVariables ) {70 private IFunctionTree CreateModel(double[] coefficients, List<string> allowedVariables, int minTimeOffset, int maxTimeOffset) { 64 71 IFunctionTree root = new Addition().GetTreeNode(); 65 72 IFunctionTree actNode = root; 73 int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1); 66 74 67 75 Queue<IFunctionTree> nodes = new Queue<IFunctionTree>(); 68 GP.StructureIdentification.Variable v; 69 for (int i = 0; i < coefficients.Length - 1; i++) { 70 var vNode = (VariableFunctionTree)new GP.StructureIdentification.Variable().GetTreeNode(); 71 vNode.VariableName = allowedVariables[i]; 72 vNode.Weight = coefficients[i]; 73 vNode.SampleOffset = 0; 74 nodes.Enqueue(vNode); 76 for (int i = 0; i < allowedVariables.Count; i++) { 77 for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) { 78 var vNode = (VariableFunctionTree)new GP.StructureIdentification.Variable().GetTreeNode(); 79 vNode.VariableName = allowedVariables[i]; 80 vNode.Weight = coefficients[(i * timeOffsetRange) + (timeOffset - minTimeOffset)]; 81 vNode.SampleOffset = timeOffset; 82 nodes.Enqueue(vNode); 83 } 75 84 } 76 85 var cNode = (ConstantFunctionTree)new Constant().GetTreeNode(); … … 101 110 102 111 //returns list of valid row indexes (rows without NaN values) 103 private List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, int start, int end ) {112 private List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, int start, int end, int minTimeOffset, int maxTimeOffset) { 104 113 List<int> allowedRows = new List<int>(); 105 114 bool add; … … 107 116 add = true; 108 117 for (int col = 0; col < dataset.Columns && add == true; col++) { 109 if (double.IsNaN(dataset.GetValue(row, col)) || 110 double.IsNaN(dataset.GetValue(row, targetVariable))) 111 add = false; 118 for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) { 119 if ( 120 row + timeOffset < 0 || 121 row + timeOffset > dataset.Rows || 122 double.IsNaN(dataset.GetValue(row + timeOffset, col)) || 123 double.IsNaN(dataset.GetValue(row + timeOffset, targetVariable))) { 124 add = false; 125 } 126 } 112 127 } 113 128 if (add) … … 130 145 } 131 146 132 private double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows ) {147 private double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows, int minTimeOffset, int maxTimeOffset) { 133 148 int rowCount = allowedRows.Count; 134 double[,] matrix = new double[rowCount, allowedColumns.Count + 1]; 135 for (int col = 0; col < allowedColumns.Count; col++) { 136 for (int row = 0; row < allowedRows.Count; row++) 137 matrix[row, col] = dataset.GetValue(allowedRows[row], allowedColumns[col]); 138 } 149 int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1); 150 double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange) + 1]; 151 for (int row = 0; row < allowedRows.Count; row++) 152 for (int col = 0; col < allowedColumns.Count; col++) { 153 for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) 154 matrix[row, (col * timeOffsetRange) + (timeOffset - minTimeOffset)] = dataset.GetValue(allowedRows[row] + timeOffset, allowedColumns[col]); 155 } 139 156 //add constant 1.0 in last column 140 157 for (int i = 0; i < rowCount; i++) 141 matrix[i, allowedColumns.Count ] = constant;158 matrix[i, allowedColumns.Count * timeOffsetRange] = constant; 142 159 return matrix; 143 160 } -
trunk/sources/HeuristicLab.LinearRegression/3.2/LinearTimeSeriesPrognosis.cs
r2356 r2360 38 38 39 39 namespace HeuristicLab.LinearRegression { 40 public class Linear Classification : LinearRegression, IClassificationAlgorithm {40 public class LinearTimeSeriesPrognosis : LinearRegression, ITimeSeriesAlgorithm { 41 41 42 public override string Name { get { return "LinearClassification"; } } 42 public override string Name { get { return "LinearTimeSeriesPrognosis"; } } 43 public int MaxTimeOffset { 44 get { return GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data; } 45 set { GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data = value; } 46 } 43 47 44 public LinearClassification() 48 public int MinTimeOffset { 49 get { return GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data; } 50 set { GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data = value; } 51 } 52 53 54 public LinearTimeSeriesPrognosis() 45 55 : base() { 56 MaxTimeOffset = -1; 57 MinTimeOffset = -1; 46 58 } 47 59 48 60 protected override IOperator CreateProblemInjector() { 49 return DefaultClassificationOperators.CreateProblemInjector(); 61 return DefaultTimeSeriesOperators.CreateProblemInjector(); 62 } 63 64 protected override VariableInjector CreateGlobalInjector() { 65 VariableInjector injector = base.CreateGlobalInjector(); 66 injector.AddVariable(new HeuristicLab.Core.Variable("MinTimeOffset", new IntData())); 67 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTimeOffset", new IntData())); 68 return injector; 50 69 } 51 70 52 71 protected override IOperator CreateModelAnalyzerOperator() { 53 return Default ClassificationOperators.CreatePostProcessingOperator();72 return DefaultTimeSeriesOperators.CreatePostProcessingOperator(); 54 73 } 55 74 56 75 protected internal virtual IAnalyzerModel CreateLRModel(IScope bestModelScope) { 57 76 var model = new AnalyzerModel(); 58 Default ClassificationOperators.PopulateAnalyzerModel(bestModelScope, model);77 DefaultTimeSeriesOperators.PopulateAnalyzerModel(bestModelScope, model); 59 78 return model; 60 79 }
Note: See TracChangeset
for help on using the changeset viewer.