public void ToStringTest() { // Issue 51: PolynomialRegression poly = new PolynomialRegression(2); 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 }; poly.Regress(x, y); { string expected = "y(x) = 3x^2 + 1.99999999999999x^1 + 1.00000000000006x^0"; expected = expected.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); string actual = poly.ToString(); Assert.AreEqual(expected, actual); } { string expected = "y(x) = 3x^2 + 1.99999999999999x^1 + 1.00000000000006x^0"; 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.0x^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,00x^0"; string actual = poly.ToString("N2", System.Globalization.CultureInfo.GetCultureInfo("pt-BR")); Assert.AreEqual(expected, actual); } }
public void PolynomialRegressionRegressTest() { double[] inputs = { 15.2, 229.7, 3500 }; double[] outputs = { 0.51, 105.66, 1800 }; int degree = 2; PolynomialRegression target = new PolynomialRegression(degree); double[] expected = { 8.003175717e-6, 4.882498125e-1, -6.913246203 }; double[] actual; target.Regress(inputs, outputs); actual = target.Coefficients; Assert.AreEqual(expected[0], actual[0], 000.1); Assert.AreEqual(expected[1], actual[1], 000.1); Assert.AreEqual(expected[2], actual[2], 000.1); }
/// <summary> /// Creates a new polynomial regression directly from data points. /// </summary> /// /// <param name="degree">The polynomial degree to use.</param> /// <param name="x">The input vectors <c>x</c>.</param> /// <param name="y">The output vectors <c>y</c>.</param> /// /// <returns>A polynomial regression f(x) that most approximates y.</returns> /// public static PolynomialRegression FromData(int degree, double[] x, double[] y) { PolynomialRegression regression = new PolynomialRegression(degree); regression.Regress(x, y); return regression; }
/// <summary> /// Creates a new polynomial regression directly from data points. /// </summary> /// /// <param name="degree">The polynomial degree to use.</param> /// <param name="x">The input vectors <c>x</c>.</param> /// <param name="y">The output vectors <c>y</c>.</param> /// /// <returns>A polynomial regression f(x) that most approximates y.</returns> /// public static PolynomialRegression FromData(int degree, double[] x, double[] y) { PolynomialRegression regression = new PolynomialRegression(degree); regression.Regress(x, y); return(regression); }