使用Apache Maths进行多项式回归(Java)

Dom*_*que 3 java apache regression

任何人都可以帮我用Apache Math库进行多项式回归(2阶).

以下数据应该给出这个等式:39.79 x ^ 2 - 497.66 x + 997.45(由Excel计算,r2 = 0.9998)

// coding style from http://commons.apache.org/proper/commons-math/userguide/fitting.html    

double[] y = { 540.0, 160.0, -140.0, -360.0, -480.0, -560.0, -540.0, -440.0, -260.0, 0.0, 340.0};              
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (double figure:y){
            obs.add(1.0, figure);
        }
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
        final double[] coeff = fitter.fit(obs.toList());
        System.out.println("coef="+Arrays.toString(coeff));
Run Code Online (Sandbox Code Playgroud)

以下是前一代码提供的回归系数:

coef=[-53.73522460839947, -52.22329678670934, -52.22329678670934]
Run Code Online (Sandbox Code Playgroud)

显然,我有些遗漏......

谢谢你的帮助

大教堂

Zie*_*elu 6

您的所有数据点都是x = 1.

obs.add(1.0, figure);!!!!
Run Code Online (Sandbox Code Playgroud)

而不是1.0应该有x值,如果它们从零均匀间隔而不是用于循环和ix而不是1.0.

  • 好.得到它了.你是绝对正确的.得出:coef = [997.45,-497.66,39.79].x ^ 2的最后一个,x的中间一个,常量的第一个.非常感谢Zielu的耐心等待.我很感激. (2认同)