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

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

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...

Thanks for your kind help

Dom

解决方案

All your data points are at x = 1.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值