本文介绍了使用Apache Maths(Java)进行多项式回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用Apache Math库帮助我进行多项式回归(第2阶)吗?

Could anybody help me make a polynomial regression (order 2) with the Apache Math library.

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

The following data should give this equation: 39.79 x^2 - 497.66 x + 997.45(computed by Excel with 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));

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

Here are the regression coefficients provided by the previous code:

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

很明显,我缺少一些东西...

Obviously, there's something I'm missing...

感谢您的帮助

Dom

推荐答案

所有数据点都位于x = 1.

All your data points are at x = 1.

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

如果x值与零之间的间隔均匀,而不是1.0,则应该使用x值,而不是用于循环和ix而不是1.0.

instead of 1.0 there should be x value, if they are evenly spaced from zero than use for loop and ix instead of 1.0.

这篇关于使用Apache Maths(Java)进行多项式回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 00:12