我是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/