在java中使用通用数学库

epx*_*epx 2 java linear-regression

我是java的新手,现在我想将普通的线性回归应用于两个系列,比如说[1,2,3,4,5]和[2,3,4,5,6].

我了解到有一个名为common的库math.但是,文档很难理解,有没有例子可以在java中做简单的普通线性回归?

Dim*_*ims 7

使用math3库,您可以执行以下操作.样本基于SimpleRegression类:

import org.apache.commons.math3.stat.regression.SimpleRegression;

public class Try_Regression {

    public static void main(String[] args) {

        // creating regression object, passing true to have intercept term
        SimpleRegression simpleRegression = new SimpleRegression(true);

        // passing data to the model
        // model will be fitted automatically by the class 
        simpleRegression.addData(new double[][] {
                {1, 2},
                {2, 3},
                {3, 4},
                {4, 5},
                {5, 6}
        });

        // querying for model parameters
        System.out.println("slope = " + simpleRegression.getSlope());
        System.out.println("intercept = " + simpleRegression.getIntercept());

        // trying to run model for unknown data
        System.out.println("prediction for 1.5 = " + simpleRegression.predict(1.5));

    }

}
Run Code Online (Sandbox Code Playgroud)