java - 在java中使用通用数学库

标签 java linear-regression

我是java的新手,现在我想申请普通线性回归到两个系列,比如说 [1, 2, 3, 4, 5] 和 [2, 3, 4, 5, 6]。

我了解到有一个库叫 common math .但是文档很难看懂,有没有示例 在java中做简单的普通线性回归?

最佳答案

math3 library你可以按照下面的方法做。样本基于 SimpleRegression 类(class):

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));

    }

}

关于java - 在java中使用通用数学库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859029/

相关文章:

java.lang.ClassNotFoundException : org. apache.poi.POIXMLTypeLoader 错误

java - 使用静态初始化 block

r - 综上.lm(P.for.trend) : essentially perfect fit: summary may be unreliable; How to deal with this?

python - 如何使用sklearn拟合线性回归后获得残差的方差

r - 分类变量的多重共线性

machine-learning - 从梯度下降更新规则中获得直觉

java - 将 JButton 文本绑定(bind)到属性

java - 反射可以改变字段的声明类型吗

machine-learning - Python 中的梯度下降实现返回 Nan

java - Android Message.sendToTarget() 在没有设置 getTarget() 的情况下工作