简体   繁体   English

Apache Math 3.6.1的多项式回归

[英]Polynomial Regression with Apache Maths 3.6.1

Can someone let me know how I can do Polynomial Regression with Apache Maths 3.6.1 有人可以让我知道如何使用Apache Maths 3.6.1进行多项式回归

Below are the data points I used for my testing 以下是我用于测试的数据点

60735214881.391304  1520254800000.000000
60697824142.469570  1520258400000.000000
60651182200.208694  1520262000000.000000
60684367132.939130  1520265600000.000000
60676588613.008700  1520269200000.000000
60641816564.869570  1520272800000.000000
60604714824.233510  1520276400000.000000
60580042814.330440  1520280000000.000000
60536134542.469570  1520283600000.000000
60566323732.034780  1520287200000.000000
60578775249.252174  1520290800000.000000
60547382844.104350  1520294400000.000000
60536776546.802160  1520298000000.000000
60474342718.330440  1520301600000.000000
60452725477.286960  1520305200000.000000
60486821569.669560  1520308800000.000000
60247997139.995674  1520312400000.000000
60248432181.426090  1520316000000.000000
60217476247.373920  1520319600000.000000
60170744493.634780  1520323200000.000000

My code looks like below 我的代码如下所示

private void polynomialFitter(List<List<Double>> pointlist) {
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (List<Double> point : pointlist) {
            obs.add(point.get(1), point.get(0));
        }
        double[] fit = fitter.fit(obs.toList());
        System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]);
    }

The coefficients are reported as 系数报告为

Coefficient 12.910025, 0.000000, 0.000000

But these does not seem to be quite correct. 但是这些似乎并不完全正确。 If I use the same dataset in Online Polynimal Regression and in archanoid online regression - both reports same value as 654623237474.68250993904929103762, 28.75921919628759991574, -0.00000000023885199278 如果我在在线多项式回归拟真在线回归中使用相同的数据集-两者都报告与654623237474.68250993904929103762, 28.75921919628759991574, -0.00000000023885199278相同的值

Can someone let me know what is going wrong? 有人可以让我知道怎么了吗? I have seen this question but that is not helping me. 我已经看到了这个问题,但这无济于事。

This has been answered in apache-commons mailing list apache-commons邮件列表中已回答此问题

Polynomial regression is not the same as curve fitting. 多项式回归与曲线拟合不同。 To do polynomial regression in Commons Math, use the OLSMultipleLinearRegression class, using, X, X^2 etc as the independent variables (as your second reference above shows). 要在Commons Math中进行多项式回归,请使用OLSMultipleLinearRegression类,将X,X ^ 2等用作自变量(如上面的第二个参考所示)。

A sample code is like below 示例代码如下

private OLSMultipleLinearRegression getMultipleLinearRegression(List<List<Double>> pointlist) {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    double y[] = new double[pointlist.size()];
    double x[][] = new double[pointlist.size()][2];
    int c = 0;
    for (List<Double> point : pointlist) {
        y[c] = point.get(0);
        x[c][0] = point.get(1);
        x[c][1] = Math.pow(point.get(1), 2);
        regression.newSampleData(y, x);
        c++;
    }
    System.out.printf("\tR2 = %f", regression.calculateRSquared());
    return regression;
}

With Commons Math 3.6.1, I get almost identical parameters when fitting a cubic to data using PolynomialCurveFitter and tuk's OLSMultipleLinearRegression suggestion. 使用Commons Math 3.6.1,当使用PolynomialCurveFitter和tuk的OLSMultipleLinearRegression建议将立方拟合到数据时,我得到几乎相同的参数。

The curve gives correct interpolations in the range of the data, but you've got to be careful about extrapolations. 曲线可以在数据范围内提供正确的插值,但是您必须小心外推。

The PolynomialCurveFitter code was half the number of lines of the OLSMultipleLinearRegression code. PolynomialCurveFitter代码是OLSMultipleLinearRegression代码的行数的一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Apache Maths进行多项式回归(Java) - Polynomial regression with Apache Maths (Java) 使用Apache Maths(Java)使用多项式回归在(0,0)处强制拦截 - Force intercept at (0,0) using polynomial regression with Apache Maths (Java) 使用Java中的Apache Maths对时间序列进行简单回归 - Simple Regression of Time Series with Apache Maths in Java 使用 apache 中的数学计算斜率的简单回归 Java - Simple Regression for calculating slope using apache maths in Java Apache Common Maths-单变量函数求解器 - Apache Common Maths - Univariate Function Solver 生成的多项式回归值离坐标太远 - Polynomial Regression values generated too far from the coordinates 多维多项式回归(最好是 C/C++、Java 或 Scala) - Multidimensional polynomial regression (preferably C/C++, Java or Scala) 将 Apache Common Math 3.6.1 项目从 Maven 存储库添加到 NetBeans 12.x 中的 Maven 项目 - Adding the Apache Common Math 3.6.1 project from Maven Repository to a Maven project in NetBeans 12.x Java:使用多项式样条函数 (Apache) 将数据集加倍 - Java: Using Polynomial Spline Function (Apache) to Double a Dataset 如何使用apache-common-math获取函数在最小间隔上的最小值/最大值 - How to get function's min/max on spesific interval with apache-common-maths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM