Changeset 15280 for branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4
- Timestamp:
- 07/23/17 00:52:14 (7 years ago)
- Location:
- branches/Async
- Files:
-
- 71 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/Async
- Property svn:mergeinfo changed
-
branches/Async/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneR.cs
r13092 r15280 139 139 } 140 140 141 var model = new OneRClassificationModel( bestVariable, bestSplits.Select(s => s.thresholdValue).ToArray(), bestSplits.Select(s => s.classValue).ToArray(), bestMissingValuesClass);141 var model = new OneRClassificationModel(problemData.TargetVariable, bestVariable, bestSplits.Select(s => s.thresholdValue).ToArray(), bestSplits.Select(s => s.classValue).ToArray(), bestMissingValuesClass); 142 142 var solution = new OneRClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 143 143 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationModel.cs
r13098 r15280 31 31 [StorableClass] 32 32 [Item("OneR Classification Model", "A model that uses intervals for one variable to determine the class.")] 33 public class OneRClassificationModel : NamedItem, IClassificationModel { 33 public class OneRClassificationModel : ClassificationModel { 34 public override IEnumerable<string> VariablesUsedForPrediction { 35 get { return new[] { Variable }; } 36 } 37 34 38 [Storable] 35 39 protected string variable; … … 66 70 public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); } 67 71 68 public OneRClassificationModel(string variable, double[] splits, double[] classes, double missingValuesClass = double.NaN)69 : base( ) {72 public OneRClassificationModel(string targetVariable, string variable, double[] splits, double[] classes, double missingValuesClass = double.NaN) 73 : base(targetVariable) { 70 74 if (splits.Length != classes.Length) { 71 75 throw new ArgumentException("Number of splits and classes has to be equal."); … … 84 88 // uses sorting to return the values in the order of rows, instead of using nested for loops 85 89 // to avoid O(n²) runtime 86 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {90 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 87 91 var values = dataset.GetDoubleValues(Variable, rows).ToArray(); 88 92 var rowsArray = rows.ToArray(); … … 108 112 } 109 113 110 public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {114 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 111 115 return new OneRClassificationSolution(this, new ClassificationProblemData(problemData)); 112 116 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/ZeroR.cs
r13098 r15280 64 64 .MaxItems(kvp => kvp.Value).Select(x => x.Key).First(); 65 65 66 var model = new ConstantModel(dominantClass );66 var model = new ConstantModel(dominantClass, target); 67 67 var solution = model.CreateClassificationSolution(problemData); 68 68 return solution; -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceConst.cs
r12012 r15280 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 83 81 } 84 82 85 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 86 84 double scale; 87 85 GetParameterValues(p, out scale); … … 91 89 cov.CrossCovariance = (x, xt, i, j) => scale; 92 90 if (HasFixedScaleParameter) { 93 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();91 cov.CovarianceGradient = (x, i, j) => new double[0]; 94 92 } else { 95 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices);93 cov.CovarianceGradient = (x, i, j) => new[] { 2.0 * scale }; 96 94 } 97 95 return cov; 98 96 } 99 100 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double scale, IEnumerable<int> columnIndices) {101 yield return 2.0 * scale;102 }103 97 } 104 98 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinear.cs
r12012 r15280 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 52 50 } 53 51 54 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {52 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 55 53 if (p.Length > 0) throw new ArgumentException("No parameters are allowed for the linear covariance function."); 56 54 // create functions 57 55 var cov = new ParameterizedCovarianceFunction(); 58 cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, 1, columnIndices);59 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, 1.0 , columnIndices);60 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();56 cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, columnIndices, 1.0); 57 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, columnIndices, 1.0); 58 cov.CovarianceGradient = (x, i, j) => new double[0]; 61 59 return cov; 62 60 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinearArd.cs
r12012 r15280 81 81 } 82 82 83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {83 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 84 84 double[] inverseLength; 85 85 GetParameterValues(p, out inverseLength); … … 90 90 cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices); 91 91 if (fixedInverseLength) 92 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();92 cov.CovarianceGradient = (x, i, j) => new double[0]; 93 93 else 94 94 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices); … … 96 96 } 97 97 98 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, IEnumerable<int>columnIndices) {98 private static IList<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, int[] columnIndices) { 99 99 int k = 0; 100 foreach (int columnIndex in columnIndices) { 101 yield return -2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k]; 100 var g = new List<double>(columnIndices.Length); 101 for (int c = 0; c < columnIndices.Length; c++) { 102 var columnIndex = columnIndices[c]; 103 g.Add(-2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k]); 102 104 k++; 103 105 } 106 return g; 104 107 } 105 108 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMask.cs
r12012 r15280 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using System.Linq; 25 using System.Linq.Expressions;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; … … 73 70 } 74 71 75 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {72 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 76 73 var cov = CovarianceFunctionParameter.Value; 77 74 var selectedDimensions = SelectedDimensionsParameter.Value; 78 75 79 return cov.GetParameterizedCovarianceFunction(p, selectedDimensions.Intersect(columnIndices) );76 return cov.GetParameterizedCovarianceFunction(p, selectedDimensions.Intersect(columnIndices).ToArray()); 80 77 } 81 78 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMaternIso.cs
r12012 r15280 111 111 } 112 112 113 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {113 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 114 114 double inverseLength, scale; 115 115 int d = DParameter.Value.Value; … … 122 122 double dist = i == j 123 123 ? 0.0 124 : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));124 : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength)); 125 125 return scale * m(d, dist); 126 126 }; 127 127 cov.CrossCovariance = (x, xt, i, j) => { 128 double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength, columnIndices));128 double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, Math.Sqrt(d) * inverseLength)); 129 129 return scale * m(d, dist); 130 130 }; … … 155 155 } 156 156 157 158 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, IEnumerable<int> columnIndices, 157 private static IList<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, int[] columnIndices, 159 158 bool fixedInverseLength, bool fixedScale) { 160 159 double dist = i == j 161 160 ? 0.0 162 : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));161 : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength)); 163 162 164 if (!fixedInverseLength) yield return scale * dm(d, dist); 165 if (!fixedScale) yield return 2 * scale * m(d, dist); 163 var g = new List<double>(2); 164 if (!fixedInverseLength) g.Add(scale * dm(d, dist)); 165 if (!fixedScale) g.Add(2 * scale * m(d, dist)); 166 return g; 166 167 } 167 168 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs
r12012 r15280 102 102 } 103 103 104 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {104 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 105 105 double length, scale; 106 106 GetParameterValues(p, out scale, out length); … … 113 113 double s1 = 1.0; 114 114 double s2 = 1.0; 115 foreach (var col in columnIndices) { 115 for (int c = 0; c < columnIndices.Length; c++) { 116 var col = columnIndices[c]; 116 117 sx += x[i, col] * x[j, col]; 117 118 s1 += x[i, col] * x[i, col]; … … 125 126 double s1 = 1.0; 126 127 double s2 = 1.0; 127 foreach (var col in columnIndices) { 128 for (int c = 0; c < columnIndices.Length; c++) { 129 var col = columnIndices[c]; 128 130 sx += x[i, col] * xt[j, col]; 129 131 s1 += x[i, col] * x[i, col]; … … 138 140 139 141 // order of returned gradients must match the order in GetParameterValues! 140 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, IEnumerable<int>columnIndices,142 private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int[] columnIndices, 141 143 bool fixedLength, bool fixedScale) { 142 { 143 double sx = 1.0; 144 double s1 = 1.0; 145 double s2 = 1.0; 146 foreach (var col in columnIndices) { 147 sx += x[i, col] * x[j, col]; 148 s1 += x[i, col] * x[i, col]; 149 s2 += x[j, col] * x[j, col]; 150 } 151 var h = (length + s1) * (length + s2); 152 var f = sx / Math.Sqrt(h); 153 if (!fixedLength) { 154 yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0)); 155 } 156 if (!fixedScale) { 157 yield return 2.0 * scale * Math.Asin(f); 158 } 144 double sx = 1.0; 145 double s1 = 1.0; 146 double s2 = 1.0; 147 for (int c = 0; c < columnIndices.Length; c++) { 148 var col = columnIndices[c]; 149 sx += x[i, col] * x[j, col]; 150 s1 += x[i, col] * x[i, col]; 151 s2 += x[j, col] * x[j, col]; 159 152 } 153 var h = (length + s1) * (length + s2); 154 var f = sx / Math.Sqrt(h); 155 156 var g = new List<double>(2); 157 if (!fixedLength) g.Add(-scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0))); 158 if (!fixedScale) g.Add(2.0 * scale * Math.Asin(f)); 159 return g; 160 160 } 161 161 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNoise.cs
r12012 r15280 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; … … 84 82 } 85 83 86 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {84 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 87 85 double scale; 88 86 GetParameterValues(p, out scale); … … 91 89 var cov = new ParameterizedCovarianceFunction(); 92 90 cov.Covariance = (x, i, j) => i == j ? scale : 0.0; 93 cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, 1.0, columnIndices) < 1e-9 ? scale : 0.0;91 cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, columnIndices, 1.0) < 1e-9 ? scale : 0.0; 94 92 if (fixedScale) 95 cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();93 cov.CovarianceGradient = (x, i, j) => new double[0]; 96 94 else 97 cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1);95 cov.CovarianceGradient = (x, i, j) => new double[1] { i == j ? 2.0 * scale : 0.0 }; 98 96 return cov; 99 97 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs
r12012 r15280 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 117 116 } 118 117 119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {118 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 120 119 double inverseLength, period, scale; 121 120 GetParameterValues(p, out scale, out period, out inverseLength); … … 145 144 } 146 145 147 148 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength, 146 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double period, double inverseLength, 149 147 bool fixedInverseLength, bool fixedPeriod, bool fixedScale) { 150 148 double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period; 151 149 double gradient = Math.Sin(k) * inverseLength; 152 150 gradient *= gradient; 153 if (!fixedInverseLength) yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient; 151 var g = new List<double>(3); 152 if (!fixedInverseLength) 153 g.Add(4.0 * scale * Math.Exp(-2.0 * gradient) * gradient); 154 154 if (!fixedPeriod) { 155 155 double r = Math.Sin(k) * inverseLength; 156 yield return 2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength;156 g.Add(2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength); 157 157 } 158 158 if (!fixedScale) 159 yield return 2.0 * scale * Math.Exp(-2 * gradient);160 159 g.Add(2.0 * scale * Math.Exp(-2 * gradient)); 160 return g; 161 161 } 162 162 163 private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int>columnIndices) {164 return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));163 private static double GetDistance(double[,] x, double[,] xt, int i, int j, int[] columnIndices) { 164 return Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1)); 165 165 } 166 166 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePiecewisePolynomial.cs
r12012 r15280 69 69 Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the piecewise polynomial covariance function.")); 70 70 71 var validValues = new ItemSet<IntValue>(new IntValue[] { 72 (IntValue)(new IntValue().AsReadOnly()), 73 (IntValue)(new IntValue(1).AsReadOnly()), 74 (IntValue)(new IntValue(2).AsReadOnly()), 71 var validValues = new ItemSet<IntValue>(new IntValue[] { 72 (IntValue)(new IntValue().AsReadOnly()), 73 (IntValue)(new IntValue(1).AsReadOnly()), 74 (IntValue)(new IntValue(2).AsReadOnly()), 75 75 (IntValue)(new IntValue(3).AsReadOnly()) }); 76 76 Parameters.Add(new ConstrainedValueParameter<IntValue>("V", "The v parameter of the piecewise polynomial function (allowed values 0, 1, 2, 3).", validValues, validValues.First())); … … 113 113 } 114 114 115 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {115 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 116 116 double length, scale; 117 117 int v = VParameter.Value.Value; … … 148 148 var cov = new ParameterizedCovarianceFunction(); 149 149 cov.Covariance = (x, i, j) => { 150 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices));150 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length)); 151 151 return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 152 152 }; 153 153 cov.CrossCovariance = (x, xt, i, j) => { 154 double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, 1.0 / length, columnIndices));154 double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1.0 / length)); 155 155 return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 156 156 }; … … 159 159 } 160 160 161 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, IEnumerable<int>columnIndices,161 private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, int[] columnIndices, 162 162 bool fixedLength, bool fixedScale) { 163 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices)); 164 if (!fixedLength) yield return scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k)); 165 if (!fixedScale) yield return 2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k); 163 double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length)); 164 var g = new List<double>(2); 165 if (!fixedLength) g.Add(scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k))); 166 if (!fixedScale) g.Add(2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k)); 167 return g; 166 168 } 167 169 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePolynomial.cs
r12012 r15280 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 107 106 } 108 107 109 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {108 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 110 109 double @const, scale; 111 110 int degree = DegreeParameter.Value.Value; … … 116 115 // create functions 117 116 var cov = new ParameterizedCovarianceFunction(); 118 cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, 1.0, columnIndices), degree);119 cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, 1.0, columnIndices), degree);117 cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, columnIndices, 1.0), degree); 118 cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, columnIndices, 1.0), degree); 120 119 cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale); 121 120 return cov; 122 121 } 123 122 124 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, IEnumerable<int>columnIndices,123 private static IList<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, int[] columnIndices, 125 124 bool fixedConst, bool fixedScale) { 126 double s = Util.ScalarProd(x, i, j, 1.0, columnIndices); 127 if (!fixedConst) yield return c * degree * scale * Math.Pow(c + s, degree - 1); 128 if (!fixedScale) yield return 2 * scale * Math.Pow(c + s, degree); 125 double s = Util.ScalarProd(x, i, j, columnIndices, 1.0); 126 var g = new List<double>(2); 127 if (!fixedConst) g.Add(c * degree * scale * Math.Pow(c + s, degree - 1)); 128 if (!fixedScale) g.Add(2 * scale * Math.Pow(c + s, degree)); 129 return g; 129 130 } 130 131 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceProduct.cs
r12012 r15280 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 76 75 } 77 76 78 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {77 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 79 78 if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function."); 80 79 var functions = new List<ParameterizedCovarianceFunction>(); … … 93 92 } 94 93 95 public static I Enumerable<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) {94 public static IList<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) { 96 95 var covariances = factorFunctions.Select(f => f.Covariance(x, i, j)).ToArray(); 96 var gr = new List<double>(); 97 97 for (int ii = 0; ii < factorFunctions.Count; ii++) { 98 98 foreach (var g in factorFunctions[ii].CovarianceGradient(x, i, j)) { … … 100 100 for (int jj = 0; jj < covariances.Length; jj++) 101 101 if (ii != jj) res *= covariances[jj]; 102 yield return res;102 gr.Add(res); 103 103 } 104 104 } 105 return gr; 105 106 } 106 107 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticArd.cs
r12012 r15280 121 121 } 122 122 123 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {123 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 124 124 double scale, shape; 125 125 double[] inverseLength; … … 144 144 } 145 145 146 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double shape, double[] inverseLength,146 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double[] inverseLength, 147 147 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 148 148 double d = i == j … … 151 151 double b = 1 + 0.5 * d / shape; 152 152 int k = 0; 153 var g = new List<double>(columnIndices.Length + 2); 153 154 if (!fixedInverseLength) { 154 155 foreach (var columnIndex in columnIndices) { 155 yield return156 g.Add( 156 157 scale * Math.Pow(b, -shape - 1) * 157 Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]) ;158 Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k])); 158 159 k++; 159 160 } 160 161 } 161 if (!fixedScale) yield return 2 * scale * Math.Pow(b, -shape); 162 if (!fixedShape) yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 162 if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape)); 163 if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b))); 164 return g; 163 165 } 164 166 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs
r12012 r15280 117 117 } 118 118 119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {119 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 120 120 double scale, shape, inverseLength; 121 121 GetParameterValues(p, out scale, out shape, out inverseLength); … … 128 128 double d = i == j 129 129 ? 0.0 130 : Util.SqrDist(x, i, j, inverseLength, columnIndices);130 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 131 131 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 132 132 }; 133 133 cov.CrossCovariance = (x, xt, i, j) => { 134 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);134 double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength); 135 135 return scale * Math.Pow(1 + 0.5 * d / shape, -shape); 136 136 }; … … 139 139 } 140 140 141 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double shape, double inverseLength,141 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength, 142 142 bool fixedInverseLength, bool fixedScale, bool fixedShape) { 143 143 double d = i == j 144 144 ? 0.0 145 : Util.SqrDist(x, i, j, inverseLength, columnIndices);145 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 146 146 147 147 double b = 1 + 0.5 * d / shape; 148 if (!fixedInverseLength) yield return scale * Math.Pow(b, -shape - 1) * d; 149 if (!fixedScale) yield return 2 * scale * Math.Pow(b, -shape); 150 if (!fixedShape) yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)); 148 var g = new List<double>(3); 149 if (!fixedInverseLength) g.Add(scale * Math.Pow(b, -shape - 1) * d); 150 if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape)); 151 if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b))); 152 return g; 151 153 } 152 154 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs
r12012 r15280 87 87 } 88 88 89 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {89 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 90 90 double scale; 91 91 GetParameterValues(p, out scale); … … 100 100 } 101 101 102 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, ParameterizedCovarianceFunction cov,102 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov, 103 103 bool fixedScale) { 104 var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count); 104 105 if (!fixedScale) { 105 yield return 2 * scale * cov.Covariance(x, i, j);106 gr.Add(2 * scale * cov.Covariance(x, i, j)); 106 107 } 107 108 foreach (var g in cov.CovarianceGradient(x, i, j)) 108 yield return scale * g; 109 gr.Add(scale * g); 110 return gr; 109 111 } 110 112 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSpectralMixture.cs
r12012 r15280 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 131 130 } 132 131 133 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {132 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 134 133 double[] weight, frequency, lengthScale; 135 134 GetParameterValues(p, out weight, out frequency, out lengthScale); … … 152 151 } 153 152 154 private static double GetCovariance(double[,] x, double[,] xt, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int>columnIndices) {153 private static double GetCovariance(double[,] x, double[,] xt, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, int[] columnIndices) { 155 154 // tau = x - x' (only for selected variables) 156 155 double[] tau = … … 164 163 int idx = 0; // helper index for tau 165 164 // for each selected variable 166 foreach (var c in columnIndices) { 167 kc *= f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 165 for (int c = 0; c < columnIndices.Length; c++) { 166 var col = columnIndices[c]; 167 kc *= f1(tau[idx], lengthScale[q * numberOfVariables + col]) * f2(tau[idx], frequency[q * numberOfVariables + col]); 168 168 idx++; 169 169 } … … 181 181 182 182 // order of returned gradients must match the order in GetParameterValues! 183 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, IEnumerable<int>columnIndices,183 private static IList<double> GetGradient(double[,] x, int i, int j, int maxQ, double[] weight, double[] frequency, double[] lengthScale, int[] columnIndices, 184 184 bool fixedWeight, bool fixedFrequency, bool fixedLengthScale) { 185 185 double[] tau = Util.GetRow(x, i, columnIndices).Zip(Util.GetRow(x, j, columnIndices), (xi, xj) => xi - xj).ToArray(); 186 186 int numberOfVariables = lengthScale.Length / maxQ; 187 187 188 var g = new List<double>((!fixedWeight ? maxQ : 0) + (!fixedFrequency ? maxQ * columnIndices.Length : 0) + (!fixedLengthScale ? maxQ * columnIndices.Length : 0)); 188 189 if (!fixedWeight) { 189 190 // weight … … 193 194 int idx = 0; // helper index for tau 194 195 // for each selected variable 195 foreach (var c in columnIndices) { 196 k *= f1(tau[idx], lengthScale[q * numberOfVariables + c]) * f2(tau[idx], frequency[q * numberOfVariables + c]); 196 for (int c = 0; c < columnIndices.Length; c++) { 197 var col = columnIndices[c]; 198 k *= f1(tau[idx], lengthScale[q * numberOfVariables + col]) * f2(tau[idx], frequency[q * numberOfVariables + col]); 197 199 idx++; 198 200 } 199 yield return k;201 g.Add(k); 200 202 } 201 203 } … … 212 214 Math.Sin(2 * Math.PI * tau[idx] * frequency[q * numberOfVariables + c]); 213 215 idx++; 214 yield return weight[q] * k;216 g.Add(weight[q] * k); 215 217 } 216 218 } … … 228 230 f2(tau[idx], frequency[q * numberOfVariables + c]); 229 231 idx++; 230 yield return weight[q] * k;232 g.Add(weight[q] * k); 231 233 } 232 234 } 233 235 } 236 237 return g; 234 238 } 235 239 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs
r12012 r15280 99 99 } 100 100 101 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {101 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 102 102 double scale; 103 103 double[] inverseLength; … … 122 122 123 123 // order of returned gradients must match the order in GetParameterValues! 124 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int>columnIndices, double scale, double[] inverseLength,124 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double[] inverseLength, 125 125 bool fixedInverseLength, bool fixedScale) { 126 126 double d = i == j … … 129 129 130 130 int k = 0; 131 var g = new List<double>((!fixedInverseLength ? columnIndices.Length : 0) + (!fixedScale ? 1 : 0)); 131 132 if (!fixedInverseLength) { 132 foreach (var columnIndex in columnIndices) { 133 for (int c = 0; c < columnIndices.Length; c++) { 134 var columnIndex = columnIndices[c]; 133 135 double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]); 134 yield return scale * Math.Exp(-d / 2.0) * sqrDist;136 g.Add(scale * Math.Exp(-d / 2.0) * sqrDist); 135 137 k++; 136 138 } 137 139 } 138 if (!fixedScale) yield return 2.0 * scale * Math.Exp(-d / 2.0); 140 if (!fixedScale) g.Add(2.0 * scale * Math.Exp(-d / 2.0)); 141 return g; 139 142 } 140 143 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs
r12012 r15280 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq.Expressions;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 104 103 } 105 104 106 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {105 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 107 106 double inverseLength, scale; 108 107 GetParameterValues(p, out scale, out inverseLength); … … 114 113 double d = i == j 115 114 ? 0.0 116 : Util.SqrDist(x, i, j, inverseLength, columnIndices);115 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 117 116 return scale * Math.Exp(-d / 2.0); 118 117 }; 119 118 cov.CrossCovariance = (x, xt, i, j) => { 120 double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);119 double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength); 121 120 return scale * Math.Exp(-d / 2.0); 122 121 }; … … 127 126 128 127 // order of returned gradients must match the order in GetParameterValues! 129 private static I Enumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices,128 private static IList<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices, 130 129 bool fixedInverseLength, bool fixedScale) { 131 130 double d = i == j 132 131 ? 0.0 133 : Util.SqrDist(x, i, j, inverseLength, columnIndices);132 : Util.SqrDist(x, i, j, columnIndices, inverseLength); 134 133 double g = Math.Exp(-d / 2.0); 135 if (!fixedInverseLength) yield return sf2 * g * d; 136 if (!fixedScale) yield return 2.0 * sf2 * g; 134 var gr = new List<double>(2); 135 if (!fixedInverseLength) gr.Add(sf2 * g * d); 136 if (!fixedScale) gr.Add(2.0 * sf2 * g); 137 return gr; 137 138 } 138 139 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSum.cs
r12012 r15280 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Linq.Expressions;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 76 75 } 77 76 78 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices) {77 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) { 79 78 if (terms.Count == 0) throw new ArgumentException("at least one term is necessary for the product covariance function."); 80 79 var functions = new List<ParameterizedCovarianceFunction>(); … … 88 87 sum.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Sum(); 89 88 sum.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Sum(); 90 sum.CovarianceGradient = (x, i, j) => functions.Select(e => e.CovarianceGradient(x, i, j)).Aggregate(Enumerable.Concat); 89 sum.CovarianceGradient = (x, i, j) => { 90 var g = new List<double>(); 91 foreach (var e in functions) 92 g.AddRange(e.CovarianceGradient(x, i, j)); 93 return g; 94 }; 91 95 return sum; 92 96 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r13160 r15280 34 34 [StorableClass] 35 35 [Item("GaussianProcessModel", "Represents a Gaussian process posterior.")] 36 public sealed class GaussianProcessModel : NamedItem, IGaussianProcessModel { 36 public sealed class GaussianProcessModel : RegressionModel, IGaussianProcessModel { 37 public override IEnumerable<string> VariablesUsedForPrediction { 38 get { return allowedInputVariables; } 39 } 40 37 41 [Storable] 38 42 private double negativeLogLikelihood; … … 61 65 get { return meanFunction; } 62 66 } 63 [Storable] 64 private string targetVariable; 65 public string TargetVariable { 66 get { return targetVariable; } 67 } 67 68 68 [Storable] 69 69 private string[] allowedInputVariables; … … 128 128 this.trainingDataset = cloner.Clone(original.trainingDataset); 129 129 this.negativeLogLikelihood = original.negativeLogLikelihood; 130 this.targetVariable = original.targetVariable;131 130 this.sqrSigmaNoise = original.sqrSigmaNoise; 132 131 if (original.meanParameter != null) { … … 147 146 IEnumerable<double> hyp, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction, 148 147 bool scaleInputs = true) 149 : base( ) {148 : base(targetVariable) { 150 149 this.name = ItemName; 151 150 this.description = ItemDescription; 152 151 this.meanFunction = (IMeanFunction)meanFunction.Clone(); 153 152 this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone(); 154 this.targetVariable = targetVariable;155 153 this.allowedInputVariables = allowedInputVariables.ToArray(); 156 154 … … 167 165 try { 168 166 CalculateModel(ds, rows, scaleInputs); 169 } catch (alglib.alglibexception ae) { 167 } 168 catch (alglib.alglibexception ae) { 170 169 // wrap exception so that calling code doesn't have to know about alglib implementation 171 170 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 181 180 182 181 IEnumerable<double> y; 183 y = ds.GetDoubleValues( targetVariable, rows);182 y = ds.GetDoubleValues(TargetVariable, rows); 184 183 185 184 int n = x.GetLength(0); 186 185 186 var columns = Enumerable.Range(0, x.GetLength(1)).ToArray(); 187 187 // calculate cholesky decomposed (lower triangular) covariance matrix 188 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));188 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 189 189 this.l = CalculateL(x, cov, sqrSigmaNoise); 190 190 191 191 // calculate mean 192 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, x.GetLength(1)));192 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 193 193 double[] m = Enumerable.Range(0, x.GetLength(0)) 194 194 .Select(r => mean.Mean(x, r)) … … 227 227 double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)]; 228 228 for (int k = 0; k < meanGradients.Length; k++) { 229 var meanGrad = Enumerable.Range(0, alpha.Length) 230 .Select(r => mean.Gradient(x, r, k)); 229 var meanGrad = new double[alpha.Length]; 230 for (int g = 0; g < meanGrad.Length; g++) 231 meanGrad[g] = mean.Gradient(x, g, k); 231 232 meanGradients[k] = -Util.ScalarProd(meanGrad, alpha); 232 233 } … … 236 237 for (int i = 0; i < n; i++) { 237 238 for (int j = 0; j < i; j++) { 238 var g = cov.CovarianceGradient(x, i, j) .ToArray();239 var g = cov.CovarianceGradient(x, i, j); 239 240 for (int k = 0; k < covGradients.Length; k++) { 240 241 covGradients[k] += lCopy[i, j] * g[k]; … … 242 243 } 243 244 244 var gDiag = cov.CovarianceGradient(x, i, i) .ToArray();245 var gDiag = cov.CovarianceGradient(x, i, i); 245 246 for (int k = 0; k < covGradients.Length; k++) { 246 247 // diag … … 298 299 299 300 #region IRegressionModel Members 300 public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {301 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 301 302 return GetEstimatedValuesHelper(dataset, rows); 302 303 } 303 public GaussianProcessRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {304 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 304 305 return new GaussianProcessRegressionSolution(this, new RegressionProblemData(problemData)); 305 }306 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {307 return CreateRegressionSolution(problemData);308 306 } 309 307 #endregion … … 320 318 int newN = newX.GetLength(0); 321 319 322 var Ks = new double[newN, n]; 323 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1))); 320 var Ks = new double[newN][]; 321 var columns = Enumerable.Range(0, newX.GetLength(1)).ToArray(); 322 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, columns); 324 323 var ms = Enumerable.Range(0, newX.GetLength(0)) 325 324 .Select(r => mean.Mean(newX, r)) 326 325 .ToArray(); 327 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1)));326 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 328 327 for (int i = 0; i < newN; i++) { 328 Ks[i] = new double[n]; 329 329 for (int j = 0; j < n; j++) { 330 Ks[i ,j] = cov.CrossCovariance(x, newX, j, i);330 Ks[i][j] = cov.CrossCovariance(x, newX, j, i); 331 331 } 332 332 } 333 333 334 334 return Enumerable.Range(0, newN) 335 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 336 } catch (alglib.alglibexception ae) { 335 .Select(i => ms[i] + Util.ScalarProd(Ks[i], alpha)); 336 } 337 catch (alglib.alglibexception ae) { 337 338 // wrap exception so that calling code doesn't have to know about alglib implementation 338 339 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); … … 352 353 var kss = new double[newN]; 353 354 double[,] sWKs = new double[n, newN]; 354 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1))); 355 var columns = Enumerable.Range(0, newX.GetLength(1)).ToArray(); 356 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, columns); 355 357 356 358 if (l == null) { … … 372 374 373 375 for (int i = 0; i < newN; i++) { 374 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 376 var col = Util.GetCol(sWKs, i).ToArray(); 377 var sumV = Util.ScalarProd(col, col); 375 378 kss[i] += sqrSigmaNoise; // kss is V(f), add noise variance of predictive distibution to get V(y) 376 379 kss[i] -= sumV; … … 378 381 } 379 382 return kss; 380 } catch (alglib.alglibexception ae) { 383 } 384 catch (alglib.alglibexception ae) { 381 385 // wrap exception so that calling code doesn't have to know about alglib implementation 382 386 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); 383 387 } 384 388 } 389 385 390 } 386 391 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/ICovarianceFunction.cs
r12012 r15280 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using System.Linq.Expressions;25 23 using HeuristicLab.Core; 26 24 … … 29 27 public delegate double CovarianceFunctionDelegate(double[,] x, int i, int j); 30 28 public delegate double CrossCovarianceFunctionDelegate(double[,] x, double[,] xt, int i, int j); 31 public delegate I Enumerable<double> CovarianceGradientFunctionDelegate(double[,] x, int i, int j);29 public delegate IList<double> CovarianceGradientFunctionDelegate(double[,] x, int i, int j); 32 30 33 31 public class ParameterizedCovarianceFunction { … … 40 38 int GetNumberOfParameters(int numberOfVariables); 41 39 void SetParameter(double[] p); 42 ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int>columnIndices);40 ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices); 43 41 } 44 42 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/IMeanFunction.cs
r12012 r15280 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 22 using HeuristicLab.Core; 25 23 … … 36 34 int GetNumberOfParameters(int numberOfVariables); 37 35 void SetParameter(double[] p); 38 ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices);36 ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices); 39 37 } 40 38 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs
r12012 r15280 76 76 } 77 77 78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 79 79 double c; 80 80 GetParameters(p, out c); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanLinear.cs
r12012 r15280 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Common; … … 70 69 } 71 70 72 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {71 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 73 72 double[] weights; 74 int[] columns = columnIndices .ToArray();73 int[] columns = columnIndices; 75 74 GetParameter(p, out weights); 76 75 var mf = new ParameterizedMeanFunction(); … … 78 77 // sanity check 79 78 if (weights.Length != columns.Length) throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function."); 80 return Util.ScalarProd(weights, Util.GetRow(x, i, columns) );79 return Util.ScalarProd(weights, Util.GetRow(x, i, columns).ToArray()); 81 80 }; 82 81 mf.Gradient = (x, i, k) => { -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanModel.cs
r13136 r15280 73 73 } 74 74 75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 76 76 if (p.Length > 0) throw new ArgumentException("No parameters allowed for model-based mean function.", "p"); 77 77 var solution = RegressionSolution; 78 78 var variableNames = solution.ProblemData.AllowedInputVariables.ToArray(); 79 if (variableNames.Length != columnIndices. Count())79 if (variableNames.Length != columnIndices.Length) 80 80 throw new ArgumentException("The number of input variables does not match in MeanModel"); 81 81 var variableValues = variableNames.Select(_ => new List<double>() { 0.0 }).ToArray(); // or of zeros -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanProduct.cs
r12012 r15280 73 73 74 74 75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {75 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 76 76 var factorMf = new List<ParameterizedMeanFunction>(); 77 77 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanSum.cs
r12012 r15280 68 68 } 69 69 70 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {70 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 71 71 var termMf = new List<ParameterizedMeanFunction>(); 72 72 int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanZero.cs
r12012 r15280 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Core; … … 50 48 } 51 49 52 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int>columnIndices) {50 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) { 53 51 if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p"); 54 52 var mf = new ParameterizedMeanFunction(); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r12012 r15280 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Core;26 using HeuristicLab.Data;27 25 28 26 namespace HeuristicLab.Algorithms.DataAnalysis { 29 27 internal static class Util { 30 public static double ScalarProd(IEnumerable<double> v, IEnumerable<double> u) { 31 return v.Zip(u, (vi, ui) => vi * ui).Sum(); 28 public static double ScalarProd(double[] v, double[] u) { 29 if (v.Length != u.Length) throw new InvalidOperationException(); 30 double prod = 0.0; 31 for (int i = 0; i < v.Length; i++) 32 prod += v[i] * u[i]; 33 return prod; 32 34 } 33 35 … … 41 43 } 42 44 43 public static double SqrDist(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {44 return SqrDist(x, i, x, j, scale, columnIndices);45 public static double SqrDist(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 46 return SqrDist(x, i, x, j, columnIndices, scale); 45 47 } 46 48 47 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {49 public static double SqrDist(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 48 50 double ss = 0.0; 49 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)); 50 foreach (int columnIndex in columnIndices) { 51 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1)).ToArray(); 52 for (int c = 0; c < columnIndices.Length; c++) { 53 var columnIndex = columnIndices[c]; 51 54 double d = x[i, columnIndex] - xt[j, columnIndex]; 52 55 ss += d * d; … … 55 58 } 56 59 57 public static double SqrDist(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {60 public static double SqrDist(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 58 61 return SqrDist(x, i, x, j, scale, columnIndices); 59 62 } 60 63 61 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {64 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 62 65 double ss = 0.0; 63 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));64 66 int scaleIndex = 0; 65 foreach (int columnIndex in columnIndices) { 67 for (int c = 0; c < columnIndices.Length; c++) { 68 var columnIndex = columnIndices[c]; 66 69 double d = x[i, columnIndex] - xt[j, columnIndex]; 67 70 ss += d * d * scale[scaleIndex] * scale[scaleIndex]; … … 73 76 return ss; 74 77 } 75 public static double ScalarProd(double[,] x, int i, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {76 return ScalarProd(x, i, x, j, scale, columnIndices);78 public static double ScalarProd(double[,] x, int i, int j, int[] columnIndices, double scale = 1.0) { 79 return ScalarProd(x, i, x, j, columnIndices, scale); 77 80 } 78 81 79 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double scale = 1.0, IEnumerable<int> columnIndices = null) {82 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, int[] columnIndices, double scale = 1.0) { 80 83 double sum = 0.0; 81 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));82 foreach (int columnIndex in columnIndices) {84 for (int c = 0; c < columnIndices.Length; c++) { 85 var columnIndex = columnIndices[c]; 83 86 sum += x[i, columnIndex] * xt[j, columnIndex]; 84 87 } 85 88 return scale * scale * sum; 86 89 } 87 public static double ScalarProd(double[,] x, int i, int j, double[] scale, IEnumerable<int> columnIndices = null) {90 public static double ScalarProd(double[,] x, int i, int j, double[] scale, int[] columnIndices) { 88 91 return ScalarProd(x, i, x, j, scale, columnIndices); 89 92 } 90 93 91 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, IEnumerable<int> columnIndices = null) {94 public static double ScalarProd(double[,] x, int i, double[,] xt, int j, double[] scale, int[] columnIndices) { 92 95 double sum = 0.0; 93 if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));94 96 int scaleIndex = 0; 95 foreach (int columnIndex in columnIndices) { 97 for (int c = 0; c < columnIndices.Length; c++, scaleIndex++) { 98 var columnIndex = columnIndices[c]; 96 99 sum += x[i, columnIndex] * scale[scaleIndex] * xt[j, columnIndex] * scale[scaleIndex]; 97 scaleIndex++;98 100 } 99 101 // must be at the end of scale after iterating over columnIndices -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs
r13238 r15280 35 35 36 36 namespace HeuristicLab.Algorithms.DataAnalysis { 37 [Item("Gradient Boosted Trees (GBT)", "Gradient boosted trees algorithm. Friedman, J. \"Greedy Function Approximation: A Gradient Boosting Machine\", IMS 1999 Reitz Lecture.")]37 [Item("Gradient Boosted Trees (GBT)", "Gradient boosted trees algorithm. Specific implementation of gradient boosting for regression trees. Friedman, J. \"Greedy Function Approximation: A Gradient Boosting Machine\", IMS 1999 Reitz Lecture.")] 38 38 [StorableClass] 39 39 [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 125)] -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithmStatic.cs
r13157 r15280 96 96 weights = new List<double>(); 97 97 // add constant model 98 models.Add(new ConstantModel(f0 ));98 models.Add(new ConstantModel(f0, problemData.TargetVariable)); 99 99 weights.Add(1.0); 100 100 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModel.cs
r13157 r15280 33 33 [Item("Gradient boosted tree model", "")] 34 34 // this is essentially a collection of weighted regression models 35 public sealed class GradientBoostedTreesModel : NamedItem, IGradientBoostedTreesModel {35 public sealed class GradientBoostedTreesModel : RegressionModel, IGradientBoostedTreesModel { 36 36 // BackwardsCompatibility3.4 for allowing deserialization & serialization of old models 37 37 #region Backwards compatible code, remove with 3.5 … … 58 58 #endregion 59 59 60 public override IEnumerable<string> VariablesUsedForPrediction { 61 get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); } 62 } 63 60 64 private readonly IList<IRegressionModel> models; 61 65 public IEnumerable<IRegressionModel> Models { get { return models; } } … … 77 81 } 78 82 [Obsolete("The constructor of GBTModel should not be used directly anymore (use GBTModelSurrogate instead)")] 79 publicGradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights)80 : base( "Gradient boosted tree model", string.Empty) {83 internal GradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights) 84 : base(string.Empty, "Gradient boosted tree model", string.Empty) { 81 85 this.models = new List<IRegressionModel>(models); 82 86 this.weights = new List<double>(weights); … … 89 93 } 90 94 91 public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {95 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 92 96 // allocate target array go over all models and add up weighted estimation for each row 93 97 if (!rows.Any()) return Enumerable.Empty<double>(); // return immediately if rows is empty. This prevents multiple iteration over lazy rows enumerable. … … 105 109 } 106 110 107 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {111 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 108 112 return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone()); 109 113 } 114 110 115 } 111 116 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r13157 r15280 22 22 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 33 34 // recalculate the actual GBT model on demand 34 35 [Item("Gradient boosted tree model", "")] 35 public sealed class GradientBoostedTreesModelSurrogate : NamedItem, IGradientBoostedTreesModel {36 public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel { 36 37 // don't store the actual model! 37 38 private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary … … 55 56 56 57 58 public override IEnumerable<string> VariablesUsedForPrediction { 59 get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); } 60 } 61 57 62 [StorableConstructor] 58 63 private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { } … … 73 78 74 79 // create only the surrogate model without an actual model 75 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu) 76 : base("Gradient boosted tree model", string.Empty) { 80 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, 81 ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu) 82 : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) { 77 83 this.trainingProblemData = trainingProblemData; 78 84 this.seed = seed; … … 86 92 87 93 // wrap an actual model in a surrograte 88 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu, IGradientBoostedTreesModel model) 94 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, 95 ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu, 96 IGradientBoostedTreesModel model) 89 97 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 90 98 this.actualModel = model; … … 96 104 97 105 // forward message to actual model (recalculate model first if necessary) 98 public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {106 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 99 107 if (actualModel == null) actualModel = RecalculateModel(); 100 108 return actualModel.GetEstimatedValues(dataset, rows); 101 109 } 102 110 103 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {111 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 104 112 return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone()); 105 113 } 106 107 114 108 115 private IGradientBoostedTreesModel RecalculateModel() { -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeBuilder.cs
r13065 r15280 137 137 int nRows = idx.Count(); 138 138 139 // shuffle variable idx139 // shuffle variable names 140 140 HeuristicLab.Random.ListExtensions.ShuffleInPlace(allowedVariables, random); 141 141 … … 176 176 CreateRegressionTreeFromQueue(maxSize, lossFunction); 177 177 178 return new RegressionTreeModel(tree.ToArray() );179 } 180 181 182 // processes potential splits from the queue as long as splits are leftand the maximum size of the tree is not reached178 return new RegressionTreeModel(tree.ToArray(), problemData.TargetVariable); 179 } 180 181 182 // processes potential splits from the queue as long as splits are remaining and the maximum size of the tree is not reached 183 183 private void CreateRegressionTreeFromQueue(int maxNodes, ILossFunction lossFunction) { 184 184 while (queue.Any() && curTreeNodeIdx + 1 < maxNodes) { // two nodes are created in each loop … … 204 204 205 205 // overwrite existing leaf node with an internal node 206 tree[f.ParentNodeIdx] = new RegressionTreeModel.TreeNode(f.SplittingVariable, f.SplittingThreshold, leftTreeIdx, rightTreeIdx );206 tree[f.ParentNodeIdx] = new RegressionTreeModel.TreeNode(f.SplittingVariable, f.SplittingThreshold, leftTreeIdx, rightTreeIdx, weightLeft: (splitIdx - startIdx + 1) / (double)(endIdx - startIdx + 1)); 207 207 } 208 208 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeModel.cs
r13030 r15280 34 34 [StorableClass] 35 35 [Item("RegressionTreeModel", "Represents a decision tree for regression.")] 36 public sealed class RegressionTreeModel : NamedItem, IRegressionModel { 36 public sealed class RegressionTreeModel : RegressionModel { 37 public override IEnumerable<string> VariablesUsedForPrediction { 38 get { return tree.Select(t => t.VarName).Where(v => v != TreeNode.NO_VARIABLE); } 39 } 37 40 38 41 // trees are represented as a flat array … … 40 43 public readonly static string NO_VARIABLE = null; 41 44 42 public TreeNode(string varName, double val, int leftIdx = -1, int rightIdx = -1 )45 public TreeNode(string varName, double val, int leftIdx = -1, int rightIdx = -1, double weightLeft = -1.0) 43 46 : this() { 44 47 VarName = varName; … … 46 49 LeftIdx = leftIdx; 47 50 RightIdx = rightIdx; 48 } 49 50 public string VarName { get; private set; } // name of the variable for splitting or NO_VARIABLE if terminal node 51 public double Val { get; private set; } // threshold 52 public int LeftIdx { get; private set; } 53 public int RightIdx { get; private set; } 51 WeightLeft = weightLeft; 52 } 53 54 public string VarName { get; internal set; } // name of the variable for splitting or NO_VARIABLE if terminal node 55 public double Val { get; internal set; } // threshold 56 public int LeftIdx { get; internal set; } 57 public int RightIdx { get; internal set; } 58 public double WeightLeft { get; internal set; } // for partial dependence plots (value in range [0..1] describes the fraction of training samples for the left sub-tree 59 54 60 55 61 // necessary because the default implementation of GetHashCode for structs in .NET would only return the hashcode of val here … … 64 70 LeftIdx.Equals(other.LeftIdx) && 65 71 RightIdx.Equals(other.RightIdx) && 72 WeightLeft.Equals(other.WeightLeft) && 66 73 EqualStrings(VarName, other.VarName); 67 74 } else { … … 79 86 private TreeNode[] tree; 80 87 81 [Storable] 88 #region old storable format 89 // remove with HL 3.4 90 [Storable(AllowOneWay = true)] 82 91 // to prevent storing the references to data caches in nodes 83 // TODO seeminglyit is bad (performance-wise) to persist tuples (tuples are used as keys in a dictionary)92 // seemingly, it is bad (performance-wise) to persist tuples (tuples are used as keys in a dictionary) 84 93 private Tuple<string, double, int, int>[] SerializedTree { 85 get { return tree.Select(t => Tuple.Create(t.VarName, t.Val, t.LeftIdx, t.RightIdx)).ToArray(); } 86 set { this.tree = value.Select(t => new TreeNode(t.Item1, t.Item2, t.Item3, t.Item4)).ToArray(); } 87 } 94 // get { return tree.Select(t => Tuple.Create(t.VarName, t.Val, t.LeftIdx, t.RightIdx)).ToArray(); } 95 set { this.tree = value.Select(t => new TreeNode(t.Item1, t.Item2, t.Item3, t.Item4, -1.0)).ToArray(); } // use a weight of -1.0 to indicate that partial dependence cannot be calculated for old models 96 } 97 #endregion 98 #region new storable format 99 [Storable] 100 private string[] SerializedTreeVarNames { 101 get { return tree.Select(t => t.VarName).ToArray(); } 102 set { 103 if (tree == null) tree = new TreeNode[value.Length]; 104 for (int i = 0; i < value.Length; i++) { 105 tree[i].VarName = value[i]; 106 } 107 } 108 } 109 [Storable] 110 private double[] SerializedTreeValues { 111 get { return tree.Select(t => t.Val).ToArray(); } 112 set { 113 if (tree == null) tree = new TreeNode[value.Length]; 114 for (int i = 0; i < value.Length; i++) { 115 tree[i].Val = value[i]; 116 } 117 } 118 } 119 [Storable] 120 private int[] SerializedTreeLeftIdx { 121 get { return tree.Select(t => t.LeftIdx).ToArray(); } 122 set { 123 if (tree == null) tree = new TreeNode[value.Length]; 124 for (int i = 0; i < value.Length; i++) { 125 tree[i].LeftIdx = value[i]; 126 } 127 } 128 } 129 [Storable] 130 private int[] SerializedTreeRightIdx { 131 get { return tree.Select(t => t.RightIdx).ToArray(); } 132 set { 133 if (tree == null) tree = new TreeNode[value.Length]; 134 for (int i = 0; i < value.Length; i++) { 135 tree[i].RightIdx = value[i]; 136 } 137 } 138 } 139 [Storable] 140 private double[] SerializedTreeWeightLeft { 141 get { return tree.Select(t => t.WeightLeft).ToArray(); } 142 set { 143 if (tree == null) tree = new TreeNode[value.Length]; 144 for (int i = 0; i < value.Length; i++) { 145 tree[i].WeightLeft = value[i]; 146 } 147 } 148 } 149 #endregion 88 150 89 151 [StorableConstructor] … … 98 160 } 99 161 100 internal RegressionTreeModel(TreeNode[] tree )101 : base( "RegressionTreeModel", "Represents a decision tree for regression.") {162 internal RegressionTreeModel(TreeNode[] tree, string targetVariable) 163 : base(targetVariable, "RegressionTreeModel", "Represents a decision tree for regression.") { 102 164 this.tree = tree; 103 165 } … … 108 170 if (node.VarName == TreeNode.NO_VARIABLE) 109 171 return node.Val; 110 111 if (columnCache[nodeIdx][row] <= node.Val) 172 if (columnCache[nodeIdx] == null) { 173 if (node.WeightLeft.IsAlmost(-1.0)) throw new InvalidOperationException("Cannot calculate partial dependence for trees loaded from older versions of HeuristicLab."); 174 // weighted average for partial dependence plot (recursive here because we need to calculate both sub-trees) 175 return node.WeightLeft * GetPredictionForRow(t, columnCache, node.LeftIdx, row) + 176 (1.0 - node.WeightLeft) * GetPredictionForRow(t, columnCache, node.RightIdx, row); 177 } else if (columnCache[nodeIdx][row] <= node.Val) 112 178 nodeIdx = node.LeftIdx; 113 179 else … … 121 187 } 122 188 123 public IEnumerable<double> GetEstimatedValues(IDataset ds, IEnumerable<int> rows) {189 public override IEnumerable<double> GetEstimatedValues(IDataset ds, IEnumerable<int> rows) { 124 190 // lookup columns for variableNames in one pass over the tree to speed up evaluation later on 125 191 ReadOnlyCollection<double>[] columnCache = new ReadOnlyCollection<double>[tree.Length]; … … 127 193 for (int i = 0; i < tree.Length; i++) { 128 194 if (tree[i].VarName != TreeNode.NO_VARIABLE) { 129 columnCache[i] = ds.GetReadOnlyDoubleValues(tree[i].VarName); 195 // tree models also support calculating estimations if not all variables used for training are available in the dataset 196 if (ds.ColumnNames.Contains(tree[i].VarName)) 197 columnCache[i] = ds.GetReadOnlyDoubleValues(tree[i].VarName); 130 198 } 131 199 } … … 133 201 } 134 202 135 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {203 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 136 204 return new RegressionSolution(this, new RegressionProblemData(problemData)); 137 205 } … … 148 216 } else { 149 217 return 150 TreeToString(n.LeftIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} <= {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val)) 151 + TreeToString(n.RightIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} > {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val)); 152 } 153 } 218 TreeToString(n.LeftIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} <= {3:F} ({4:N3})", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val, n.WeightLeft)) 219 + TreeToString(n.RightIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} > {3:F} ({4:N3}))", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val, 1.0 - n.WeightLeft)); 220 } 221 } 222 154 223 } 155 224 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r13157 r15280 201 201 <Compile Include="GaussianProcess\GaussianProcessRegressionSolution.cs" /> 202 202 <Compile Include="GaussianProcess\ICovarianceFunction.cs" /> 203 <Compile Include="GBM\GradientBoostingRegressionAlgorithm.cs" /> 203 204 <Compile Include="GradientBoostedTrees\IGradientBoostedTreesModel.cs" /> 204 205 <Compile Include="GradientBoostedTrees\GradientBoostedTreesModelSurrogate.cs" /> … … 252 253 <Compile Include="Linear\MultinomialLogitClassificationSolution.cs" /> 253 254 <Compile Include="Linear\MultinomialLogitModel.cs" /> 255 <Compile Include="MctsSymbolicRegression\Automaton.cs" /> 256 <Compile Include="MctsSymbolicRegression\CodeGenerator.cs" /> 257 <Compile Include="MctsSymbolicRegression\ConstraintHandler.cs" /> 258 <Compile Include="MctsSymbolicRegression\Disassembler.cs" /> 259 <Compile Include="MctsSymbolicRegression\ExpressionEvaluator.cs" /> 260 <Compile Include="MctsSymbolicRegression\MctsSymbolicRegressionAlgorithm.cs" /> 261 <Compile Include="MctsSymbolicRegression\MctsSymbolicRegressionStatic.cs" /> 262 <Compile Include="MctsSymbolicRegression\OpCodes.cs" /> 263 <Compile Include="MctsSymbolicRegression\Policies\EpsGreedy.cs" /> 264 <Compile Include="MctsSymbolicRegression\Policies\UcbTuned.cs" /> 265 <Compile Include="MctsSymbolicRegression\Policies\IActionStatistics.cs" /> 266 <Compile Include="MctsSymbolicRegression\Policies\IPolicy.cs" /> 267 <Compile Include="MctsSymbolicRegression\Policies\PolicyBase.cs" /> 268 <Compile Include="MctsSymbolicRegression\Policies\Ucb.cs" /> 269 <Compile Include="MctsSymbolicRegression\SymbolicExpressionGenerator.cs" /> 270 <Compile Include="MctsSymbolicRegression\Tree.cs" /> 254 271 <Compile Include="Nca\Initialization\INcaInitializer.cs" /> 255 272 <Compile Include="Nca\Initialization\LdaInitializer.cs" /> … … 309 326 <Private>False</Private> 310 327 </ProjectReference> 328 <ProjectReference Include="..\..\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm\3.3\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.csproj"> 329 <Project>{F409DD9E-1E9C-4EB1-AA3A-9F6E987C6E58}</Project> 330 <Name>HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3</Name> 331 </ProjectReference> 311 332 <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj"> 312 333 <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project> … … 407 428 <Name>HeuristicLab.Random-3.3</Name> 408 429 <Private>False</Private> 430 </ProjectReference> 431 <ProjectReference Include="..\..\HeuristicLab.Selection\3.3\HeuristicLab.Selection-3.3.csproj"> 432 <Project>{2C36CD4F-E5F5-43A4-801A-201EA895FE17}</Project> 433 <Name>HeuristicLab.Selection-3.3</Name> 409 434 </ProjectReference> 410 435 </ItemGroup> -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs
r12509 r15280 111 111 IClassificationProblemData problemData, 112 112 IEnumerable<int> rows) { 113 var model = new SymbolicDiscriminantFunctionClassificationModel( tree, interpreter, new AccuracyMaximizationThresholdCalculator());113 var model = new SymbolicDiscriminantFunctionClassificationModel(problemData.TargetVariable, tree, interpreter, new AccuracyMaximizationThresholdCalculator()); 114 114 model.RecalculateModelParameters(problemData, rows); 115 115 return model; -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs
r13238 r15280 110 110 addition.AddSubtree(cNode); 111 111 112 SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel( tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (IRegressionProblemData)problemData.Clone());112 SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (IRegressionProblemData)problemData.Clone()); 113 113 solution.Model.Name = "Linear Regression Model"; 114 114 solution.Name = "Linear Regression Solution"; -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitClassification.cs
r13238 r15280 95 95 relClassError = alglib.mnlrelclserror(lm, inputMatrix, nRows); 96 96 97 MultinomialLogitClassificationSolution solution = new MultinomialLogitClassificationSolution( (IClassificationProblemData)problemData.Clone(), new MultinomialLogitModel(lm, targetVariable, allowedInputVariables, classValues));97 MultinomialLogitClassificationSolution solution = new MultinomialLogitClassificationSolution(new MultinomialLogitModel(lm, targetVariable, allowedInputVariables, classValues), (IClassificationProblemData)problemData.Clone()); 98 98 return solution; 99 99 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitClassificationSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public MultinomialLogitClassificationSolution( IClassificationProblemData problemData, MultinomialLogitModel logitModel)45 public MultinomialLogitClassificationSolution( MultinomialLogitModel logitModel,IClassificationProblemData problemData) 46 46 : base(logitModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitModel.cs
r12509 r15280 34 34 [StorableClass] 35 35 [Item("Multinomial Logit Model", "Represents a multinomial logit model for classification.")] 36 public sealed class MultinomialLogitModel : NamedItem, IClassificationModel {36 public sealed class MultinomialLogitModel : ClassificationModel { 37 37 38 38 private alglib.logitmodel logitModel; … … 48 48 } 49 49 50 [Storable] 51 private string targetVariable; 50 public override IEnumerable<string> VariablesUsedForPrediction { 51 get { return allowedInputVariables; } 52 } 53 52 54 [Storable] 53 55 private string[] allowedInputVariables; … … 64 66 logitModel = new alglib.logitmodel(); 65 67 logitModel.innerobj.w = (double[])original.logitModel.innerobj.w.Clone(); 66 targetVariable = original.targetVariable;67 68 allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 68 69 classValues = (double[])original.classValues.Clone(); 69 70 } 70 71 public MultinomialLogitModel(alglib.logitmodel logitModel, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) 71 : base( ) {72 : base(targetVariable) { 72 73 this.name = ItemName; 73 74 this.description = ItemDescription; 74 75 this.logitModel = logitModel; 75 this.targetVariable = targetVariable;76 76 this.allowedInputVariables = allowedInputVariables.ToArray(); 77 77 this.classValues = (double[])classValues.Clone(); … … 82 82 } 83 83 84 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {84 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 85 85 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 86 86 … … 108 108 } 109 109 110 public MultinomialLogitClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 111 return new MultinomialLogitClassificationSolution(new ClassificationProblemData(problemData), this); 112 } 113 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 114 return CreateClassificationSolution(problemData); 110 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 111 return new MultinomialLogitClassificationSolution(this, new ClassificationProblemData(problemData)); 115 112 } 116 113 … … 135 132 } 136 133 #endregion 134 137 135 } 138 136 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaClassificationSolution.cs
r12012 r15280 40 40 : base(original, cloner) { 41 41 } 42 public NcaClassificationSolution(I ClassificationProblemData problemData, INcaModel ncaModel)42 public NcaClassificationSolution(INcaModel ncaModel, IClassificationProblemData problemData) 43 43 : base(ncaModel, problemData) { 44 44 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaModel.cs
r12509 r15280 30 30 [Item("NCA Model", "")] 31 31 [StorableClass] 32 public class NcaModel : NamedItem, INcaModel { 32 public class NcaModel : ClassificationModel, INcaModel { 33 public override IEnumerable<string> VariablesUsedForPrediction { 34 get { return allowedInputVariables; } 35 } 33 36 34 37 [Storable] … … 39 42 [Storable] 40 43 private string[] allowedInputVariables; 41 [Storable]42 private string targetVariable;43 44 [Storable] 44 45 private INearestNeighbourModel nnModel; … … 52 53 this.transformationMatrix = (double[,])original.transformationMatrix.Clone(); 53 54 this.allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 54 this.targetVariable = original.targetVariable;55 55 this.nnModel = cloner.Clone(original.nnModel); 56 56 this.classValues = (double[])original.classValues.Clone(); 57 57 } 58 public NcaModel(int k, double[,] transformationMatrix, IDataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) { 58 public NcaModel(int k, double[,] transformationMatrix, IDataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) 59 : base(targetVariable) { 59 60 Name = ItemName; 60 61 Description = ItemDescription; 61 62 this.transformationMatrix = (double[,])transformationMatrix.Clone(); 62 63 this.allowedInputVariables = allowedInputVariables.ToArray(); 63 this.targetVariable = targetVariable;64 64 this.classValues = (double[])classValues.Clone(); 65 65 … … 72 72 } 73 73 74 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {74 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 75 75 var ds = ReduceDataset(dataset, rows); 76 76 return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows)); 77 77 } 78 78 79 public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {80 return new NcaClassificationSolution( new ClassificationProblemData(problemData), this);79 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 80 return new NcaClassificationSolution(this, new ClassificationProblemData(problemData)); 81 81 } 82 82 83 I ClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {84 return CreateClassificationSolution(problemData);83 INcaClassificationSolution INcaModel.CreateClassificationSolution(IClassificationProblemData problemData) { 84 return new NcaClassificationSolution(this, new ClassificationProblemData(problemData)); 85 85 } 86 86 … … 88 88 var data = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 89 89 90 var targets = dataset.GetDoubleValues( targetVariable, rows).ToArray();90 var targets = dataset.GetDoubleValues(TargetVariable, rows).ToArray(); 91 91 var result = new double[data.GetLength(0), transformationMatrix.GetLength(1) + 1]; 92 92 for (int i = 0; i < data.GetLength(0); i++) … … 104 104 .Range(0, transformationMatrix.GetLength(1)) 105 105 .Select(x => "X" + x.ToString()) 106 .Concat( targetVariable.ToEnumerable()),106 .Concat(TargetVariable.ToEnumerable()), 107 107 Reduce(dataset, rows)); 108 108 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs
r13238 r15280 81 81 public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k) { 82 82 var problemDataClone = (IClassificationProblemData)problemData.Clone(); 83 return new NearestNeighbourClassificationSolution( problemDataClone, Train(problemDataClone, k));83 return new NearestNeighbourClassificationSolution(Train(problemDataClone, k), problemDataClone); 84 84 } 85 85 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassificationSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NearestNeighbourClassificationSolution(I ClassificationProblemData problemData, INearestNeighbourModel nnModel)45 public NearestNeighbourClassificationSolution(INearestNeighbourModel nnModel, IClassificationProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r12509 r15280 34 34 [StorableClass] 35 35 [Item("NearestNeighbourModel", "Represents a nearest neighbour model for regression and classification.")] 36 public sealed class NearestNeighbourModel : NamedItem, INearestNeighbourModel {36 public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel { 37 37 38 38 private alglib.nearestneighbor.kdtree kdTree; … … 48 48 } 49 49 50 [Storable] 51 private string targetVariable; 50 public override IEnumerable<string> VariablesUsedForPrediction { 51 get { return allowedInputVariables; } 52 } 53 52 54 [Storable] 53 55 private string[] allowedInputVariables; … … 91 93 92 94 k = original.k; 93 targetVariable = original.targetVariable;94 95 allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 95 96 if (original.classValues != null) 96 97 this.classValues = (double[])original.classValues.Clone(); 97 98 } 98 public NearestNeighbourModel(IDataset dataset, IEnumerable<int> rows, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) { 99 public NearestNeighbourModel(IDataset dataset, IEnumerable<int> rows, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 100 : base(targetVariable) { 99 101 Name = ItemName; 100 102 Description = ItemDescription; 101 103 this.k = k; 102 this.targetVariable = targetVariable;103 104 this.allowedInputVariables = allowedInputVariables.ToArray(); 104 105 … … 163 164 } 164 165 165 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {166 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 166 167 if (classValues == null) throw new InvalidOperationException("No class values are defined."); 167 168 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); … … 201 202 } 202 203 203 public INearestNeighbourRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 204 return new NearestNeighbourRegressionSolution(new RegressionProblemData(problemData), this); 205 } 204 206 205 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { 207 return CreateRegressionSolution(problemData); 208 } 209 public INearestNeighbourClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 210 return new NearestNeighbourClassificationSolution(new ClassificationProblemData(problemData), this); 211 } 212 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 213 return CreateClassificationSolution(problemData); 206 return new NearestNeighbourRegressionSolution(this, new RegressionProblemData(problemData)); 207 } 208 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 209 return new NearestNeighbourClassificationSolution(this, new ClassificationProblemData(problemData)); 214 210 } 215 211 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs
r13238 r15280 80 80 public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) { 81 81 var clonedProblemData = (IRegressionProblemData)problemData.Clone(); 82 return new NearestNeighbourRegressionSolution( clonedProblemData, Train(problemData, k));82 return new NearestNeighbourRegressionSolution(Train(problemData, k), clonedProblemData); 83 83 } 84 84 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegressionSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NearestNeighbourRegressionSolution(I RegressionProblemData problemData, INearestNeighbourModel nnModel)45 public NearestNeighbourRegressionSolution(INearestNeighbourModel nnModel, IRegressionProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassification.cs
r13238 r15280 220 220 221 221 var problemDataClone = (IClassificationProblemData)problemData.Clone(); 222 return new NeuralNetworkClassificationSolution( problemDataClone, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray()));222 return new NeuralNetworkClassificationSolution(new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray()), problemDataClone); 223 223 } 224 224 #endregion -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassificationSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NeuralNetworkClassificationSolution(I ClassificationProblemData problemData, INeuralNetworkModel nnModel)45 public NeuralNetworkClassificationSolution(INeuralNetworkModel nnModel, IClassificationProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleClassification.cs
r13238 r15280 204 204 relClassError = alglib.mlperelclserror(mlpEnsemble, inputMatrix, nRows); 205 205 var problemDataClone = (IClassificationProblemData)problemData.Clone(); 206 return new NeuralNetworkEnsembleClassificationSolution( problemDataClone, new NeuralNetworkEnsembleModel(mlpEnsemble, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray()));206 return new NeuralNetworkEnsembleClassificationSolution(new NeuralNetworkEnsembleModel(mlpEnsemble, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray()), problemDataClone); 207 207 } 208 208 #endregion -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleClassificationSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NeuralNetworkEnsembleClassificationSolution(I ClassificationProblemData problemData, INeuralNetworkEnsembleModel nnModel)45 public NeuralNetworkEnsembleClassificationSolution(INeuralNetworkEnsembleModel nnModel, IClassificationProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs
r12509 r15280 34 34 [StorableClass] 35 35 [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")] 36 public sealed class NeuralNetworkEnsembleModel : NamedItem, INeuralNetworkEnsembleModel {36 public sealed class NeuralNetworkEnsembleModel : ClassificationModel, INeuralNetworkEnsembleModel { 37 37 38 38 private alglib.mlpensemble mlpEnsemble; … … 46 46 } 47 47 } 48 } 49 50 public override IEnumerable<string> VariablesUsedForPrediction { 51 get { return allowedInputVariables; } 48 52 } 49 53 … … 72 76 } 73 77 public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 74 : base( ) {78 : base(targetVariable) { 75 79 this.name = ItemName; 76 80 this.description = ItemDescription; … … 103 107 } 104 108 105 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {109 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 106 110 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 107 111 … … 129 133 } 130 134 131 public INeuralNetworkEnsembleRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 132 return new NeuralNetworkEnsembleRegressionSolution(new RegressionEnsembleProblemData(problemData), this); 133 } 134 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { 135 return CreateRegressionSolution(problemData); 136 } 137 public INeuralNetworkEnsembleClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 138 return new NeuralNetworkEnsembleClassificationSolution(new ClassificationEnsembleProblemData(problemData), this); 139 } 140 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 141 return CreateClassificationSolution(problemData); 135 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 136 return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData)); 137 } 138 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 139 return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData)); 142 140 } 143 141 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleRegression.cs
r13238 r15280 190 190 avgRelError = alglib.mlpeavgrelerror(mlpEnsemble, inputMatrix, nRows); 191 191 192 return new NeuralNetworkEnsembleRegressionSolution( (IRegressionProblemData)problemData.Clone(), new NeuralNetworkEnsembleModel(mlpEnsemble, targetVariable, allowedInputVariables));192 return new NeuralNetworkEnsembleRegressionSolution(new NeuralNetworkEnsembleModel(mlpEnsemble, targetVariable, allowedInputVariables), (IRegressionProblemData)problemData.Clone()); 193 193 } 194 194 #endregion -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleRegressionSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NeuralNetworkEnsembleRegressionSolution(I RegressionProblemData problemData, INeuralNetworkEnsembleModel nnModel)45 public NeuralNetworkEnsembleRegressionSolution(INeuralNetworkEnsembleModel nnModel, IRegressionProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 RecalculateResults(); -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs
r12817 r15280 34 34 [StorableClass] 35 35 [Item("NeuralNetworkModel", "Represents a neural network for regression and classification.")] 36 public sealed class NeuralNetworkModel : NamedItem, INeuralNetworkModel {36 public sealed class NeuralNetworkModel : ClassificationModel, INeuralNetworkModel { 37 37 38 38 private alglib.multilayerperceptron multiLayerPerceptron; … … 48 48 } 49 49 50 [Storable] 51 private string targetVariable; 50 public override IEnumerable<string> VariablesUsedForPrediction { 51 get { return allowedInputVariables; } 52 } 53 52 54 [Storable] 53 55 private string[] allowedInputVariables; … … 74 76 multiLayerPerceptron.innerobj.x = (double[])original.multiLayerPerceptron.innerobj.x.Clone(); 75 77 multiLayerPerceptron.innerobj.y = (double[])original.multiLayerPerceptron.innerobj.y.Clone(); 76 targetVariable = original.targetVariable;77 78 allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 78 79 if (original.classValues != null) … … 80 81 } 81 82 public NeuralNetworkModel(alglib.multilayerperceptron multiLayerPerceptron, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 82 : base( ) {83 : base(targetVariable) { 83 84 this.name = ItemName; 84 85 this.description = ItemDescription; 85 86 this.multiLayerPerceptron = multiLayerPerceptron; 86 this.targetVariable = targetVariable;87 87 this.allowedInputVariables = allowedInputVariables.ToArray(); 88 88 if (classValues != null) … … 111 111 } 112 112 113 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {113 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 114 114 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 115 115 … … 137 137 } 138 138 139 public INeuralNetworkRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 140 return new NeuralNetworkRegressionSolution(new RegressionProblemData(problemData), this); 141 } 142 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { 143 return CreateRegressionSolution(problemData); 144 } 145 public INeuralNetworkClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 146 return new NeuralNetworkClassificationSolution(new ClassificationProblemData(problemData), this); 147 } 148 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 149 return CreateClassificationSolution(problemData); 139 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 140 return new NeuralNetworkRegressionSolution(this, new RegressionProblemData(problemData)); 141 } 142 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 143 return new NeuralNetworkClassificationSolution(this, new ClassificationProblemData(problemData)); 150 144 } 151 145 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegression.cs
r13238 r15280 207 207 avgRelError = alglib.mlpavgrelerror(multiLayerPerceptron, inputMatrix, nRows); 208 208 209 return new NeuralNetworkRegressionSolution( (IRegressionProblemData)problemData.Clone(), new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables));209 return new NeuralNetworkRegressionSolution(new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables), (IRegressionProblemData)problemData.Clone()); 210 210 } 211 211 #endregion -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegressionSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public NeuralNetworkRegressionSolution(I RegressionProblemData problemData, INeuralNetworkModel nnModel)45 public NeuralNetworkRegressionSolution(INeuralNetworkModel nnModel, IRegressionProblemData problemData) 46 46 : base(nnModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/Plugin.cs.frame
r13321 r15280 29 29 [PluginFile("HeuristicLab.Algorithms.DataAnalysis-3.4.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.ALGLIB", "3.7.0")] 31 [PluginDependency("HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm", "3.3")] // for GBM 31 32 [PluginDependency("HeuristicLab.Algorithms.GradientDescent", "3.3")] 32 33 [PluginDependency("HeuristicLab.Analysis", "3.3")] … … 50 51 [PluginDependency("HeuristicLab.LibSVM", "3.12")] 51 52 [PluginDependency("HeuristicLab.Random", "3.3")] 53 [PluginDependency("HeuristicLab.Selection", "3.3")] 52 54 public class HeuristicLabAlgorithmsDataAnalysisPlugin : PluginBase { 53 55 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
r13238 r15280 143 143 144 144 if (CreateSolution) { 145 var solution = new RandomForestClassificationSolution( (IClassificationProblemData)Problem.ProblemData.Clone(), model);145 var solution = new RandomForestClassificationSolution(model, (IClassificationProblemData)Problem.ProblemData.Clone()); 146 146 Results.Add(new Result(RandomForestClassificationModelResultName, "The random forest classification solution.", solution)); 147 147 } 148 148 } 149 149 150 150 // keep for compatibility with old API 151 151 public static RandomForestClassificationSolution CreateRandomForestClassificationSolution(IClassificationProblemData problemData, int nTrees, double r, double m, int seed, 152 152 out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError) { 153 153 var model = CreateRandomForestClassificationModel(problemData, nTrees, r, m, seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError); 154 return new RandomForestClassificationSolution( (IClassificationProblemData)problemData.Clone(), model);154 return new RandomForestClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 155 155 } 156 156 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public RandomForestClassificationSolution(I ClassificationProblemData problemData, IRandomForestModel randomForestModel)45 public RandomForestClassificationSolution(IRandomForestModel randomForestModel, IClassificationProblemData problemData) 46 46 : base(randomForestModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r12509 r15280 34 34 [StorableClass] 35 35 [Item("RandomForestModel", "Represents a random forest for regression and classification.")] 36 public sealed class RandomForestModel : NamedItem, IRandomForestModel {36 public sealed class RandomForestModel : ClassificationModel, IRandomForestModel { 37 37 // not persisted 38 38 private alglib.decisionforest randomForest; … … 44 44 } 45 45 } 46 47 public override IEnumerable<string> VariablesUsedForPrediction { 48 get { return originalTrainingData.AllowedInputVariables; } 49 } 50 46 51 47 52 // instead of storing the data of the model itself … … 91 96 92 97 // random forest models can only be created through the static factory methods CreateRegressionModel and CreateClassificationModel 93 private RandomForestModel( alglib.decisionforest randomForest,98 private RandomForestModel(string targetVariable, alglib.decisionforest randomForest, 94 99 int seed, IDataAnalysisProblemData originalTrainingData, 95 100 int nTrees, double r, double m, double[] classValues = null) 96 : base( ) {101 : base(targetVariable) { 97 102 this.name = ItemName; 98 103 this.description = ItemDescription; … … 147 152 } 148 153 149 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {154 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 150 155 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, AllowedInputVariables, rows); 151 156 AssertInputMatrix(inputData); … … 174 179 } 175 180 176 public IRandomForestRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 177 return new RandomForestRegressionSolution(new RegressionProblemData(problemData), this); 178 } 179 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { 180 return CreateRegressionSolution(problemData); 181 } 182 public IRandomForestClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 183 return new RandomForestClassificationSolution(new ClassificationProblemData(problemData), this); 184 } 185 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) { 186 return CreateClassificationSolution(problemData); 181 182 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 183 return new RandomForestRegressionSolution(this, new RegressionProblemData(problemData)); 184 } 185 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 186 return new RandomForestClassificationSolution(this, new ClassificationProblemData(problemData)); 187 187 } 188 188 … … 205 205 outOfBagRmsError = rep.oobrmserror; 206 206 207 return new RandomForestModel( dForest, seed, problemData, nTrees, r, m);207 return new RandomForestModel(problemData.TargetVariable, dForest, seed, problemData, nTrees, r, m); 208 208 } 209 209 … … 242 242 outOfBagRelClassificationError = rep.oobrelclserror; 243 243 244 return new RandomForestModel( dForest, seed, problemData, nTrees, r, m, classValues);244 return new RandomForestModel(problemData.TargetVariable, dForest, seed, problemData, nTrees, r, m, classValues); 245 245 } 246 246 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs
r13238 r15280 143 143 144 144 if (CreateSolution) { 145 var solution = new RandomForestRegressionSolution( (IRegressionProblemData)Problem.ProblemData.Clone(), model);145 var solution = new RandomForestRegressionSolution(model, (IRegressionProblemData)Problem.ProblemData.Clone()); 146 146 Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution)); 147 147 } … … 153 153 var model = CreateRandomForestRegressionModel(problemData, nTrees, r, m, seed, 154 154 out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError); 155 return new RandomForestRegressionSolution( (IRegressionProblemData)problemData.Clone(), model);155 return new RandomForestRegressionSolution(model, (IRegressionProblemData)problemData.Clone()); 156 156 } 157 157 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs
r12012 r15280 43 43 : base(original, cloner) { 44 44 } 45 public RandomForestRegressionSolution(IR egressionProblemData problemData, IRandomForestModel randomForestModel)45 public RandomForestRegressionSolution(IRandomForestModel randomForestModel, IRegressionProblemData problemData) 46 46 : base(randomForestModel, problemData) { 47 47 } -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs
r12509 r15280 37 37 [StorableClass] 38 38 [Item("SupportVectorMachineModel", "Represents a support vector machine model.")] 39 public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel { 39 public sealed class SupportVectorMachineModel : ClassificationModel, ISupportVectorMachineModel { 40 public override IEnumerable<string> VariablesUsedForPrediction { 41 get { return allowedInputVariables; } 42 } 43 40 44 41 45 private svm_model model; … … 83 87 84 88 [Storable] 85 private string targetVariable;86 [Storable]87 89 private string[] allowedInputVariables; 88 90 [Storable] … … 96 98 this.model = original.model; 97 99 this.rangeTransform = original.rangeTransform; 98 this.targetVariable = original.targetVariable;99 100 this.allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 100 101 if (original.classValues != null) … … 106 107 } 107 108 public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables) 108 : base( ) {109 : base(targetVariable) { 109 110 this.name = ItemName; 110 111 this.description = ItemDescription; 111 112 this.model = model; 112 113 this.rangeTransform = rangeTransform; 113 this.targetVariable = targetVariable;114 114 this.allowedInputVariables = allowedInputVariables.ToArray(); 115 115 } … … 123 123 return GetEstimatedValuesHelper(dataset, rows); 124 124 } 125 public SupportVectorRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {125 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 126 126 return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData)); 127 127 } 128 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {129 return CreateRegressionSolution(problemData);130 }131 128 #endregion 132 129 133 130 #region IClassificationModel Members 134 public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {131 public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) { 135 132 if (classValues == null) throw new NotSupportedException(); 136 133 // return the original class value instead of the predicted value of the model … … 152 149 } 153 150 154 public SupportVectorClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {151 public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 155 152 return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData)); 156 }157 IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {158 return CreateClassificationSolution(problemData);159 153 } 160 154 #endregion 161 155 private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) { 162 156 // calculate predictions for the currently requested rows 163 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);157 svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, TargetVariable, allowedInputVariables, rows); 164 158 svm_problem scaledProblem = rangeTransform.Scale(problem); 165 159 -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/TimeSeries/AutoregressiveModeling.cs
r13238 r15280 134 134 135 135 var interpreter = new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(problemData.TargetVariable); 136 var model = new SymbolicTimeSeriesPrognosisModel( tree, interpreter);136 var model = new SymbolicTimeSeriesPrognosisModel(problemData.TargetVariable, tree, interpreter); 137 137 var solution = model.CreateTimeSeriesPrognosisSolution((ITimeSeriesPrognosisProblemData)problemData.Clone()); 138 138 return solution; -
branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClusteringModel.cs
r12509 r15280 37 37 public static new Image StaticItemImage { 38 38 get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; } 39 } 40 41 public IEnumerable<string> VariablesUsedForPrediction { 42 get { return allowedInputVariables; } 39 43 } 40 44
Note: See TracChangeset
for help on using the changeset viewer.