在 java 中使用通用数学库
use common math library in java
我是 java 的新手,现在我想将 普通线性回归 应用于两个系列,比如 [1, 2, 3, 4, 5]和 [2, 3, 4, 5, 6].
我了解到有一个名为 common math
的库。但是,文档很难看懂,有没有例子在java中做简单的普通线性回归?
与math3 library you can do the way below. Sample is based on 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 的新手,现在我想将 普通线性回归 应用于两个系列,比如 [1, 2, 3, 4, 5]和 [2, 3, 4, 5, 6].
我了解到有一个名为 common math
的库。但是,文档很难看懂,有没有例子在java中做简单的普通线性回归?
与math3 library you can do the way below. Sample is based on 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));
}
}