Changeset 14891 for branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs
- Timestamp:
- 04/26/17 11:06:51 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs
r14888 r14891 32 32 [Item("KernelRidgeRegressionModel", "A kernel ridge regression model")] 33 33 public sealed class KernelRidgeRegressionModel : RegressionModel { 34 public override IEnumerable<string> VariablesUsedForPrediction { 34 public override IEnumerable<string> VariablesUsedForPrediction 35 { 35 36 get { return allowedInputVariables; } 36 37 } … … 38 39 [Storable] 39 40 private readonly string[] allowedInputVariables; 40 public string[] AllowedInputVariables { 41 public string[] AllowedInputVariables 42 { 41 43 get { return allowedInputVariables; } 42 44 } … … 114 116 var l = new double[n, n]; Array.Copy(gram, l, l.Length); 115 117 118 double[,] invG; 116 119 // cholesky decomposition 117 120 var res = alglib.trfac.spdmatrixcholesky(ref l, n, false); 118 if (res == false) throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?"); 119 120 alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha); 121 if (info != 1) throw new ArgumentException("Could not create model."); 122 123 124 { 121 if (res == false) { //throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?"); 122 int[] pivots; 123 var lua = new double[n, n]; 124 Array.Copy(gram, lua, lua.Length); 125 alglib.rmatrixlu(ref lua, n, n, out pivots); 126 alglib.rmatrixlusolve(lua, pivots, n, y, out info, out denseSolveRep, out alpha); 127 if (info != 1) throw new ArgumentException("Could not create model."); 128 alglib.matinvreport rep; 129 invG = lua; // rename 130 alglib.rmatrixluinverse(ref invG, pivots, n, out info, out rep); 131 if (info != 1) throw new ArgumentException("Could not invert Gram matrix."); 132 } else { 133 alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha); 134 if (info != 1) throw new ArgumentException("Could not create model."); 125 135 // for LOO-CV we need to build the inverse of the gram matrix 126 136 alglib.matinvreport rep; 127 varinvG = l; // rename128 alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep); 137 invG = l; // rename 138 alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep); 129 139 if (info != 1) throw new ArgumentException("Could not invert Gram matrix."); 130 var ssqLooError = 0.0; 131 for (int i = 0; i < n; i++) { 132 var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha); 133 var looPred_i = pred_i - alpha[i] / invG[i, i]; 134 var error = (y[i] - looPred_i) / yScale; 135 ssqLooError += error * error; 136 } 137 LooCvRMSE = Math.Sqrt(ssqLooError / n); 138 } 139 } catch (alglib.alglibexception ae) { 140 } 141 142 var ssqLooError = 0.0; 143 for (int i = 0; i < n; i++) { 144 var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha); 145 var looPred_i = pred_i - alpha[i] / invG[i, i]; 146 var error = (y[i] - looPred_i) / yScale; 147 ssqLooError += error * error; 148 } 149 LooCvRMSE = Math.Sqrt(ssqLooError / n); 150 } 151 catch (alglib.alglibexception ae) { 140 152 // wrap exception so that calling code doesn't have to know about alglib implementation 141 153 throw new ArgumentException("There was a problem in the calculation of the kernel ridge regression model", ae);
Note: See TracChangeset
for help on using the changeset viewer.