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

2024-04-15

有人可以帮我用 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));

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

coef=[-53.73522460839947, -52.22329678670934, -52.22329678670934]

显然,我缺少一些东西......

感谢您的热心帮助

Dom


所有数据点均位于 x = 1 处。

obs.add(1.0, figure);!!!!

如果它们与零均匀间隔,则应该使用 x 值而不是 1.0,而不是使用 for 循环和 ix 而不是 1.0。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Apache Maths 进行多项式回归 (Java) 的相关文章

随机推荐