private PolynomialRegression PRLearning(double[] independentVariables, double[] dependentVariables) { var polyTeacher = new PolynomialLeastSquares() { Degree = 6 }; PolynomialRegression objRegressionLocal = polyTeacher.Learn(independentVariables, dependentVariables); double[] prediction = objRegressionLocal.Transform(independentVariables); predictionPL[indexPredictionPL] = prediction; //PredictedData[1] = prediction; errorPR += new SquareLoss(independentVariables).Loss(prediction); double[] coefOfFunction = new double[objRegressionLocal.Weights.Length + 1]; coefOfFunction[0] = objRegressionLocal.Intercept; int index = objRegressionLocal.Weights.Length - 1; for (int i = 1; i <= objRegressionLocal.Weights.Length; i++) { coefOfFunction[i] = objRegressionLocal.Weights[index]; index--; } double func(double x) { double y = 0; for (int i = 0; i <= polyTeacher.Degree; i++) { y += coefOfFunction[i] * Math.Pow(x, i); } return(y); } double min = NormalizedInputData.GetColumn(indexPredictionPL).Min(), max = NormalizedInputData.GetColumn(indexPredictionPL).Max(); XYpairPL[indexPredictionPL] = new double[2][]; XYpairPL[indexPredictionPL][0] = new double[100]; XYpairPL[indexPredictionPL][1] = new double[100]; index = 0; for (double i = min; i <= max; i += 0.01) { XYpairPL[indexPredictionPL][0][index] = i; XYpairPL[indexPredictionPL][1][index] = func(i); index++; } indexPredictionPL++; return(objRegressionLocal); }
public void PolynomialRegressionLearning() { int n = LearningData.Length; double[] dependentVariables = LearningData.GetColumn(3); double[] independentVariables = new double[n]; //InputDataNormalization(); for (int i = 0; i < 3; i++) { independentVariables = NormalizedInputData.GetColumn(i); objRegression[i] = PRLearning(independentVariables, dependentVariables); } PredictedData[1] = new double[predictionPL[0].Length]; for (int i = 0; i < predictionPL[0].Length; i++) { PredictedData[1][i] = (predictionPL[0][i] + predictionPL[1][i] + predictionPL[2][i]) / 3; } }