- Timestamp:
- 05/18/10 23:25:23 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/HeuristicLab.Problems.DataAnalysis.Regression-3.3.csproj
r3842 r3848 86 86 <Compile Include="HeuristicLabProblemsDataAnalysisRegressionPlugin.cs" /> 87 87 <Compile Include="LinearRegression\LinearRegressionSolutionCreator.cs" /> 88 <Compile Include="LinearRegression\LinearRegressionUtil.cs" /> 88 89 <Compile Include="Properties\AssemblyInfo.cs" /> 89 90 <Compile Include="SupportVectorRegression\BestSupportVectorRegressionSolutionAnalyzer.cs" /> -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/LinearRegression/LinearRegressionSolutionCreator.cs
r3846 r3848 33 33 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 34 34 using HeuristicLab.Parameters; 35 using HeuristicLab.Data; 35 36 36 37 namespace HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression { … … 43 44 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 44 45 private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData"; 46 private const string SamplesStartParameterName = "SamplesStart"; 47 private const string SamplesEndParameterName = "SamplesEnd"; 45 48 46 49 public LinearRegressionSolutionCreator() { 47 50 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The resulting solution encoded as a symbolic expression tree.")); 48 51 Parameters.Add(new LookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The problem data on which the linear regression should be calculated.")); 52 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName,"The start of the samples on which the linear regression should be applied.")); 53 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName,"The end of the samples on which the linear regression should be applied.")); 49 54 } 50 55 [StorableConstructor] … … 69 74 set { DataAnalysisProblemDataParameter.ActualValue = value; } 70 75 } 76 77 public IValueLookupParameter<IntValue> SamplesStartParameter { 78 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; } 79 } 80 public IntValue SamplesStart { 81 get { return SamplesStartParameter.ActualValue; } 82 set { SamplesStartParameter.ActualValue = value; } 83 } 84 85 public IValueLookupParameter<IntValue> SamplesEndParameter { 86 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; } 87 } 88 public IntValue SamplesEnd { 89 get { return SamplesEndParameter.ActualValue; } 90 set { SamplesEndParameter.ActualValue = value; } 91 } 71 92 #endregion 72 93 73 94 74 95 public override IOperation Apply() { 75 SymbolicExpressionTree = CreateSymbolicExpressionTree(DataAnalysisProblemData );96 SymbolicExpressionTree = CreateSymbolicExpressionTree(DataAnalysisProblemData.Dataset,DataAnalysisProblemData.TargetVariable.Value, DataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value), SamplesStart.Value, SamplesEnd.Value); 76 97 return base.Apply(); 77 98 } 78 99 79 public static SymbolicExpressionTree CreateSymbolicExpressionTree(DataAnalysisProblemData problem) { 80 List<int> allowedRows = CalculateAllowedRows(problem); 81 double[,] inputMatrix = PrepareInputMatrix(problem, allowedRows); 100 public static SymbolicExpressionTree CreateSymbolicExpressionTree(Dataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, int start, int end) { 101 double[,] inputMatrix = LinearRegressionUtil.PrepareInputMatrix(dataset, targetVariable, allowedInputVariables, start, end); 82 102 83 103 alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel(); 84 104 alglib.linreg.lrreport ar = new alglib.linreg.lrreport(); 85 105 int nRows = inputMatrix.GetLength(0); 86 int nFeatures = inputMatrix.GetLength(1) - 1;87 double[] coefficients = new double[nFeatures + 1]; //last coefficient is for the constant106 int nFeatures = inputMatrix.GetLength(1) - 1; 107 double[] coefficients = new double[nFeatures + 1]; //last coefficient is for the constant 88 108 89 109 int retVal = 1; … … 91 111 if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model"); 92 112 93 for (int i = 0; i < nFeatures +1; i++)113 for (int i = 0; i < nFeatures + 1; i++) 94 114 coefficients[i] = lm.w[i + 4]; 95 115 96 116 SymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode()); 97 SymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();98 tree.Root.AddSubTree(start );117 SymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode(); 118 tree.Root.AddSubTree(startNode); 99 119 SymbolicExpressionTreeNode addition = new Addition().CreateTreeNode(); 100 start .AddSubTree(addition);120 startNode.AddSubTree(addition); 101 121 102 122 int col = 0; 103 foreach (string column in problem.InputVariables.CheckedItems.Select(c => c.Value.Value)) {123 foreach (string column in allowedInputVariables) { 104 124 VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable().CreateTreeNode(); 105 125 vNode.VariableName = column; … … 111 131 ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); 112 132 cNode.Value = coefficients[coefficients.Length - 1]; 133 addition.AddSubTree(cNode); 113 134 114 135 return tree; 115 136 } 116 117 private static List<int> CalculateAllowedRows(DataAnalysisProblemData problem) {118 List<int> allowedRows = new List<int>();119 bool add = false;120 121 for (int row = problem.TrainingSamplesStart.Value; row < problem.TrainingSamplesEnd.Value; row++) {122 add = true;123 foreach (string column in problem.InputVariables.CheckedItems.Select(c => c.Value.Value)) {124 double value = problem.Dataset[column, row];125 if (double.IsInfinity(value) ||126 double.IsNaN(value))127 add = false;128 }129 if (double.IsNaN(problem.Dataset[problem.TargetVariable.Value, row]))130 add = false;131 if (add)132 allowedRows.Add(row);133 add = true;134 }135 return allowedRows;136 }137 private static double[,] PrepareInputMatrix(DataAnalysisProblemData problem, IList<int> allowedRows) {138 double[,] matrix = new double[allowedRows.Count, problem.InputVariables.CheckedItems.Count()+1];139 for (int row = 0; row < allowedRows.Count; row++) {140 int col = 0;141 foreach (string column in problem.InputVariables.CheckedItems.Select(c => c.Value.Value)) {142 matrix[row, col] = problem.Dataset[column, row];143 col++;144 }145 matrix[row, problem.InputVariables.CheckedItems.Count()] = problem.Dataset[problem.TargetVariable.Value, row];146 }147 return matrix;148 }149 137 } 150 138 }
Note: See TracChangeset
for help on using the changeset viewer.