public PatternRecognition(GraphShowForm showForm) { this.showForm = showForm; this.pls = new PolynomialLeastSquares(); this.pls.Degree = 2; this.ols = new OrdinaryLeastSquares(); }
public void learn_ToStringTest_degree_is_0() { var pls = new PolynomialLeastSquares() { Degree = 0 }; }
public void learn_test_2() { #if NETCORE var culture = CultureInfo.CreateSpecificCulture("en-US"); CultureInfo.CurrentCulture = culture; #endif #region doc_learn // Let's say we would like to learn 2nd degree polynomial that // can map the first column X into its second column Y. We have // 5 examples of those (x,y) pairs that we can use to learn this // function: double[,] data = { // X Y { 12, 144 }, // example #1 { 15, 225 }, // example #2 { 20, 400 }, // example #3 { 25, 625 }, // example #4 { 35, 1225 }, // example #5 }; // Let's retrieve the input and output data: double[] inputs = data.GetColumn(0); // X double[] outputs = data.GetColumn(1); // Y // We can create a learning algorithm var ls = new PolynomialLeastSquares() { Degree = 2 }; // Now, we can use the algorithm to learn a polynomial PolynomialRegression poly = ls.Learn(inputs, outputs); // The learned polynomial will be given by string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0" // Where its weights can be accessed using double[] weights = poly.Weights; // { 1.0000000000000024, -1.2407665029287351E-13 } double intercept = poly.Intercept; // 1.5652369518855253E-12 // Finally, we can use this polynomial // to predict values for the input data double[] pred = poly.Transform(inputs); // Where the mean-squared-error (MSE) should be double error = new SquareLoss(outputs).Loss(pred); // 0.0 #endregion Assert.AreEqual(0, error, 1e-10); string ex = weights.ToCSharp(); double[] expected = { 1, 0 }; Assert.AreEqual("y(x) = 1.0x^2 + 0.0x^1 + 0.0", str); Assert.IsTrue(weights.IsEqual(expected, 1e-6)); Assert.AreEqual(0, intercept, 1e-6); }
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 Learn(IList <XtoY> dsLearn) { double [] inputs = dsLearn.Select(i => i.X).ToArray(); double [] outputs = dsLearn.Select(i => i.Y).ToArray(); var pls = new PolynomialLeastSquares() { Degree = _degree, IsRobust = _isRobust }; _polynomialRegression = pls.Learn(inputs, outputs); }
public void PolyRegression() { if (dgvTestingSource.DataSource == null) { MessageBox.Show("Please Select a data set"); return; } // Creates a matrix from the source data table double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(); double[,] data = table; // Let's retrieve the input and output data: double[] inputs = data.GetColumn(0); // X double[] outputs = data.GetColumn(1); // Y // We can create a learning algorithm var ls = new PolynomialLeastSquares() { Degree = 2 }; // Now, we can use the algorithm to learn a polynomial PolynomialRegression poly = ls.Learn(inputs, outputs); // The learned polynomial will be given by string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0" // Where its weights can be accessed using double[] weights = poly.Weights; // { 1.0000000000000024, -1.2407665029287351E-13 } double intercept = poly.Intercept; // 1.5652369518855253E-12 // Finally, we can use this polynomial // to predict values for the input data double[] pred = poly.Transform(inputs); // Where the mean-squared-error (MSE) should be double error = new SquareLoss(outputs).Loss(pred); // 0.0 double[][] tmpInputs = new double[inputs.Length][]; for (int i = 0; i < inputs.Length; i++) { tmpInputs[i] = new double[1] { inputs[i] }; } CreateResultScatterplot(zedGraphControl1, tmpInputs, outputs, pred); }
public void Learn(IList <XtoY> dsLearn) { List <double> data = new List <double>(mylist.Count); for (int i = 0; i < mylist.Count; i++) { data.Add(mylist[i].Cases); } double[] inputs = dsLearn.Select(i => i.X).ToArray(); var pls = new PolynomialLeastSquares() { Degree = _degree, IsRobust = _isRobust }; _polynomialRegression = pls.Learn(inputs, data.ToArray()); }
public static PointPairList PolynomialRegresion(Dictionary <double, double> variablePair, int degree) // y = DOUBLE_Array[1]*x + DOUBLE_Array[0]; { var polyTeacher = new PolynomialLeastSquares() { Degree = 3 }; PolynomialRegression objRegression = polyTeacher.Learn(variablePair.Keys.ToArray(), variablePair.Values.ToArray()); double[] coefOfFunction = new double[objRegression.Weights.Length + 1]; coefOfFunction[0] = objRegression.Intercept; int index = objRegression.Weights.Length - 1; for (int i = 1; i <= objRegression.Weights.Length; i++) { coefOfFunction[i] = objRegression.Weights[index]; index--; } double func(double x) { double y = 0; for (int i = 0; i <= degree; i++) { y += coefOfFunction[i] * Math.Pow(x, i); } return(y); } double[] independentValueArray = new double[variablePair.Count], dependentValueArray = new double[variablePair.Count]; index = 0; foreach (var pair in variablePair) { independentValueArray[index] = pair.Key; dependentValueArray[index] = func(pair.Key); index++; } return(new PointPairList(independentValueArray, dependentValueArray)); }
public static void test3() { var poly2 = CreateFunc(1, 1); Random rnd = new Random(); var pos = Enumerable.Range(0, 20).Select(x => new double [] { x, poly2(x) + rnd.NextDouble() }).ToArray(); double[] inputs = pos.Select(x => x[0]).ToArray(); double[] outputs = pos.Select(x => x[1]).ToArray(); var ls = new PolynomialLeastSquares() { Degree = 2 }; PolynomialRegression poly = ls.Learn(inputs, outputs); double a = poly.Weights[0]; // a = 0 double b = poly.Weights[1]; // b = 0 double c = poly.Intercept; // c = 1 double[] predicted = poly.Transform(inputs); double error = new SquareLoss(outputs).Loss(predicted); var ols = new OrdinaryLeastSquares(); SimpleLinearRegression mul = ols.Learn(inputs, outputs); double a1 = mul.Slope; // a = 0 double b1 = mul.Intercept; // b = 0 double[] simplepredict = mul.Transform(inputs); double erroe2 = new SquaredHingeLoss(outputs).Loss(simplepredict); Console.WriteLine("Done"); }
private PolynomialRegression GenerateRegressionFitting(SortedDictionary <double, double> values, char result) { // Extract inputs and outputs double[] inputs = values.Keys.ToArray(); double[] outputs = values.Values.ToArray(); // We can create a learning algorithm PolynomialLeastSquares ls = new PolynomialLeastSquares() { Degree = 2 }; // Now, we can use the algorithm to learn a polynomial PolynomialRegression poly = ls.Learn(inputs, outputs); // The learned polynomial will be given by #pragma warning disable IDE0059 // Unnecessary assignment of a value string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0" // Where its weights can be accessed using double[] weights = poly.Weights; // { 1.0000000000000024, -1.2407665029287351E-13 } double intercept = poly.Intercept; // 1.5652369518855253E-12 #pragma warning restore IDE0059 // Unnecessary assignment of a value // Finally, we can use this polynomial // to predict values for the input data double[] prediction = poly.Transform(inputs); double r2 = new RSquaredLoss(outputs.Length, outputs).Loss(prediction); // should be > 0.85 (close to 1 is ok) //LastGamesMetric: 0.77 0.81 0.08 //GoalsScoredMetric: 0.75 0.85 0.02 if (r2 == 1.0) { r2 = 0.0; } r2Values_.Add(result, r2); return(poly); }
public void learn_test() { double[] inputs = { 15.2, 229.7, 3500 }; double[] outputs = { 0.51, 105.66, 1800 }; var ls = new PolynomialLeastSquares() { Degree = 2 }; PolynomialRegression target = ls.Learn(inputs, outputs); double[] expected = { 8.003175717e-6, 4.882498125e-1, -6.913246203 }; double[] actual; actual = target.Weights; Assert.AreEqual(2, actual.Length); Assert.AreEqual(expected[0], actual[0], 1e-3); Assert.AreEqual(expected[1], actual[1], 1e-3); Assert.AreEqual(expected[2], target.Intercept, 1e-3); }
public void learn_ToStringTest() { var x = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var y = new double[] { 1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321 }; var pls = new PolynomialLeastSquares() { Degree = 2 }; PolynomialRegression poly = pls.Learn(x, y); { string expected = "y(x) = 3x^2 + 1.99999999999998x^1 + 1.00000000000005"; expected = expected.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); string actual = poly.ToString(); Assert.AreEqual(expected, actual); } { string expected = "y(x) = 3x^2 + 1.99999999999998x^1 + 1.00000000000005"; string actual = poly.ToString(null, System.Globalization.CultureInfo.GetCultureInfo("en-US")); Assert.AreEqual(expected, actual); } { string expected = "y(x) = 3.0x^2 + 2.0x^1 + 1.0"; string actual = poly.ToString("N1", System.Globalization.CultureInfo.GetCultureInfo("en-US")); Assert.AreEqual(expected, actual); } { string expected = "y(x) = 3,00x^2 + 2,00x^1 + 1,00"; string actual = poly.ToString("N2", System.Globalization.CultureInfo.GetCultureInfo("pt-BR")); Assert.AreEqual(expected, actual); } }
private void button1_Click(object sender, EventArgs e) { // Declare some sample test data. double[] inputs = { 80, 60, 10, 20, 30 }; double[] outputs = { 20, 40, 30, 50, 60 }; //// Use Ordinary Least Squares to learn the regression //OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); //// Use OLS to learn the simple linear regression //SimpleLinearRegression regression = ols.Learn(inputs, outputs); //// Compute the output for a given input: ////double y = regression.Transform(85); // The answer will be 28.088 //// We can also extract the slope and the intercept term //// for the line. Those will be -0.26 and 50.5, respectively. //double s = regression.Slope; // -0.264706 //double c = regression.Intercept; // 50.588235 //double[] x= new double[20]; //double[] y = new double[20]; //for(int i = 0; i < 20; i++) //{ // x[i] = 5 + (i * 5); // y[i] = s*x[i] + c; //} // We can create a learning algorithm var ls = new PolynomialLeastSquares() { Degree = 2 }; // Now, we can use the algorithm to learn a polynomial PolynomialRegression poly = ls.Learn(inputs, outputs); // The learned polynomial will be given by string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0" Console.WriteLine(str); // Where its weights can be accessed using double[] weights = poly.Weights; // { 1.0000000000000024, -1.2407665029287351E-13 } double intercept = poly.Intercept; // 1.5652369518855253E-12 Console.WriteLine("{0},{1}", weights[0], intercept); // Finally, we can use this polynomial // to predict values for the input data double[] pred = poly.Transform(inputs); double error = new SquareLoss(outputs).Loss(pred); Console.WriteLine(error); double[] x = new double[20]; double[] y = new double[20]; for (int i = 0; i < 20; i++) { x[i] = 5 + (i * 5); y[i] = weights[0] * x[i] * x[i] + weights[1] * x[i] + intercept; } GraphPane myPane = this.zedGraphControl1.GraphPane; myPane.CurveList.Clear(); // Set the titles myPane.Title.IsVisible = false; myPane.Chart.Border.IsVisible = false; myPane.XAxis.Title.Text = "X"; myPane.YAxis.Title.Text = "Y"; myPane.XAxis.IsAxisSegmentVisible = true; myPane.YAxis.IsAxisSegmentVisible = true; myPane.XAxis.MinorGrid.IsVisible = false; myPane.YAxis.MinorGrid.IsVisible = false; myPane.XAxis.MinorTic.IsOpposite = false; myPane.XAxis.MajorTic.IsOpposite = false; myPane.YAxis.MinorTic.IsOpposite = false; myPane.YAxis.MajorTic.IsOpposite = false; myPane.XAxis.Scale.MinGrace = 0; myPane.XAxis.Scale.MaxGrace = 0; myPane.XAxis.Scale.Max = 90; //myPane.XAxis.Scale.Min = -10; myPane.YAxis.Scale.MinGrace = 0; myPane.YAxis.Scale.MaxGrace = 0; //myPane.YAxis.Scale.Min = -10; myPane.YAxis.Scale.Max = 70; PointPairList list1 = new PointPairList(inputs, outputs); PointPairList list2 = new PointPairList(x, y); LineItem myCurve; // Add the curves myCurve = myPane.AddCurve("points", list1, Color.Blue, SymbolType.Circle); myCurve.Line.IsVisible = false; myCurve.Symbol.Fill = new Fill(Color.Blue); myCurve = myPane.AddCurve("Simple", list2, Color.Red, SymbolType.Circle); myCurve.Line.IsAntiAlias = true; myCurve.Line.IsVisible = true; myCurve.Symbol.IsVisible = false; this.zedGraphControl1.AxisChange(); this.zedGraphControl1.Invalidate(); }
public void weight_test_square() { PolynomialRegression reference; double referenceR2; { double[][] data = { new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // 5 times weight 1 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[] x = data.GetColumn(1); double[] y = data.GetColumn(2); var ols = new PolynomialLeastSquares() { Degree = 2, IsRobust = true }; reference = ols.Learn(x, y); Assert.AreEqual(1, reference.NumberOfInputs); Assert.AreEqual(1, reference.NumberOfOutputs); referenceR2 = reference.CoefficientOfDetermination(x, y); } PolynomialRegression target; double targetR2; { double[][] data = { new[] { 5.0, 10.7, 2.4 }, // 1 times weight 5 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[] weights = data.GetColumn(0); double[] x = data.GetColumn(1); double[] y = data.GetColumn(2); Assert.AreEqual(1, reference.NumberOfInputs); Assert.AreEqual(1, reference.NumberOfOutputs); var ols = new PolynomialLeastSquares() { Degree = 2, IsRobust = true }; target = ols.Learn(x, y, weights); targetR2 = target.CoefficientOfDetermination(x, y, weights); } Assert.IsTrue(reference.Weights.IsEqual(target.Weights, 1e-5)); Assert.AreEqual(reference.Intercept, target.Intercept, 1e-8); Assert.AreEqual(-0.023044161067521208, target.Weights[0], 1e-6); Assert.AreEqual(-10.192933942631839, target.Intercept, 1e-6); Assert.AreEqual(referenceR2, targetR2, 1e-8); Assert.AreEqual(0.97660035938038947, targetR2); }
public void weight_test_linear() { PolynomialRegression reference; double referenceR2; { double[][] data = { new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // 5 times weight 1 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[] x = data.GetColumn(1); double[] y = data.GetColumn(2); var ols = new PolynomialLeastSquares(); reference = ols.Learn(x, y); Assert.AreEqual(1, reference.NumberOfInputs); Assert.AreEqual(1, reference.NumberOfOutputs); referenceR2 = reference.CoefficientOfDetermination(x, y); } PolynomialRegression target; double targetR2; { double[][] data = { new[] { 5.0, 10.7, 2.4 }, // 1 times weight 5 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[] weights = data.GetColumn(0); double[] x = data.GetColumn(1); double[] y = data.GetColumn(2); Assert.AreEqual(1, reference.NumberOfInputs); Assert.AreEqual(1, reference.NumberOfOutputs); var ols = new PolynomialLeastSquares(); target = ols.Learn(x, y, weights); targetR2 = target.CoefficientOfDetermination(x, y, weights); } Assert.IsTrue(reference.Weights.IsEqual(target.Weights)); Assert.AreEqual(reference.Intercept, target.Intercept, 1e-8); Assert.AreEqual(0.16387475666214069, target.Weights[0], 1e-6); Assert.AreEqual(0.59166925681755056, target.Intercept, 1e-6); Assert.AreEqual(referenceR2, targetR2, 1e-8); Assert.AreEqual(0.91476129548901486, targetR2); }
/// <summary> /// Demonstrates polynomial least squares curve fitting /// </summary> void CurveFitting() { // Set up example description nRichDescription.Text = "A 4th degree polynomial is fitted to the noisy sampled data. \n\nFitting a polynomial of any degree is accomplished in one line of code specifing the polynomial degree, and arrays of the x and y values. "; // Build the data sets using some random normal noise DoubleVector x = new DoubleVector("[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5]"); DoubleVector y = new DoubleVector("[0 0.112462916018285 0.222702589210478 0.328626759459127 0.428392355046668 0.520499877813047 0.603856090847926 0.677801193837419 0.742100964707661 0.796908212422832 0.842700792949715 0.880205069574082 0.910313978229635 0.934007944940652 0.952285119762649 0.966105146475311 0.976348383344644 0.983790458590775 0.989090501635731 0.992790429235257 0.995322265018953 0.997020533343667 0.998137153702018 0.998856823402643 0.999311486103355 0.999593047982555]"); RandomNumberGenerator rand = new RandGenNormal(0.0, noiselevel_); y = y + 0.025 * (new DoubleVector(y.Length, rand)); // Build the least squares polynomial fit and make readable display label int polynomialdeg = 4; PolynomialLeastSquares pls = new PolynomialLeastSquares(polynomialdeg, x, y); // Build fitted polynomial DoubleVector xs = new DoubleVector(100, 0, 0.025); DoubleVector ys = pls.FittedPolynomial.Evaluate(xs); // Build the chart SetupChartLayout("Curve Fitting"); NChart chart = nChartControl1.Charts[0]; SetupChartAxes(chart); // Draw the raw data points NPointSeries point = new NPointSeries(); chart.Series.Add(point); point.UseXValues = true; point.DataLabelStyle.Visible = false; // set some appearance properties point.FillStyle = new NColorFillStyle(Color.SkyBlue); point.BorderStyle = new NStrokeStyle(1.0f, Color.DarkGray); point.PointShape = PointShape.Star; point.Size = new NLength(6.0f); // Points must fit in the chart area point.InflateMargins = true; // Name points data set point.Name = "Observations"; // Add data points from the NMath DoubleVectors to the point series point.Values.AddRange(y.DataBlock.Data); point.XValues.AddRange(x.DataBlock.Data); // Build polynomial line series and style it NLineSeries polyline = new NLineSeries(); chart.Series.Add(polyline); polyline.UseXValues = true; polyline.DataLabelStyle.Visible = false; polyline.BorderStyle = new NStrokeStyle(2.0f, Color.Tomato); // Name polynomial fit polyline.Name = polynomialdeg.ToString() + "th Degree Polynomial"; // Load the polynomial data into the line series polyline.XValues.AddRange(xs.DataBlock.Data); polyline.Values.AddRange(ys.DataBlock.Data); // Create a label to display the polynomial NLabel label = new NLabel(); label.BoundsMode = BoundsMode.None; label.ContentAlignment = ContentAlignment.MiddleLeft; label.Location = new NPointL( new NLength(92, NRelativeUnit.ParentPercentage), new NLength(70, NRelativeUnit.ParentPercentage)); label.TextStyle.TextFormat = TextFormat.XML; label.TextStyle.FontStyle = new NFontStyle("Arial", 9); label.TextStyle.StringFormatStyle.HorzAlign = Nevron.HorzAlign.Center; label.TextStyle.BackplaneStyle.Visible = true; label.TextStyle.BackplaneStyle.FillStyle = new NGradientFillStyle(GradientStyle.Horizontal, GradientVariant.Variant1, Color.FromArgb(180, 255, 255, 255), Color.FromArgb(180, 233, 233, 255)); label.TextStyle.BackplaneStyle.Shape = BackplaneShape.Rectangle; label.TextStyle.BackplaneStyle.StandardFrameStyle.InnerBorderColor = Color.FromArgb(200, 200, 255); label.Text = "Equation for fitted polynomial:<br /><font size='10' color = 'tomato'><b>"; label.Text += FormatPolymonial(pls.FittedPolynomial.Coeff.ToArray()); label.Text += "</b></font>"; chart.ChildPanels.Add(label); nChartControl1.Refresh(); }