在 Apache commons-math3 中计算 PolynomialCurveFitter 的 R 方
Calculate R-Square for PolynomialCurveFitter in Apache commons-math3
Apache commons-math3
(版本3.6.1
)类像OLSMultipleLinearRegression
,SimpleRegression
提供了一种计算RSquare
的方法(即calculateRSquared()
、getRSquare()
)。但是我找不到 PolynomialCurveFitter
?
的任何此类方法
现在我自己做如下。 common-math 中有这样的方法吗?
private PolynomialFunction getPolynomialFitter(List<List<Double>> pointlist) {
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (List<Double> point : pointlist) {
obs.add(point.get(0), point.get(1));
}
double[] fit = fitter.fit(obs.toList());
System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]);
final PolynomialFunction fitted = new PolynomialFunction(fit);
return fitted;
}
private double getRSquare(PolynomialFunction fitter, List<List<Double>> pointList) {
final double[] coefficients = fitter.getCoefficients();
double[] predictedValues = new double[pointList.size()];
double residualSumOfSquares = 0;
final DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
for (int i=0; i< pointList.size(); i++) {
predictedValues[i] = predict(coefficients, pointList.get(i).get(0));
double actualVal = pointList.get(i).get(1);
double t = Math.pow((predictedValues[i] - actualVal), 2);
residualSumOfSquares += t;
descriptiveStatistics.addValue(actualVal);
}
final double avgActualValues = descriptiveStatistics.getMean();
double totalSumOfSquares = 0;
for (int i=0; i<pointList.size(); i++) {
totalSumOfSquares += Math.pow( (predictedValues[i] - avgActualValues),2);
}
return 1.0 - (residualSumOfSquares/totalSumOfSquares);
}
final PolynomialFunction polynomial = getPolynomialFitter(trainData);
System.out.printf("\nPolynimailCurveFitter R-Square %f", getRSquare(polynomial, trainData));
这已在 apache-commons mailing list 中得到解答。 Cross-posting答案
OLSMultipleLinearRegression, SimpleRegression provide a method that
returns calculateRSquared(),
getRSquare(). But I am not able to find any such method for
PolynomialCurveFitter ?
Right now I am doing it myself like below :-
Is there any such method in common-math which does this?
"PolynomialCurveFitter" 是句法之一 sugar/wrapper
围绕 least-squares 优化器。
在(不可变的)实例中不维护任何状态。
private PolynomialFunction getPolynomialFitter(List<List<Double>>pointlist) {
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (List<Double> point : pointlist) {
obs.add(point.get(0), point.get(1));
}
double[] fit = fitter.fit(obs.toList());
System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]);
final PolynomialFunction fitted = new PolynomialFunction(fit);
return fitted;
}
这确实是预期的 use-cases。
private double getRSquare(PolynomialFunction fitter, List<List<Double>> pointList) {
final double[] coefficients = fitter.getCoefficients();
double[] predictedValues = new double[pointList.size()];
double residualSumOfSquares = 0;
final DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
for (int i=0; i< pointList.size(); i++) {
predictedValues[i] = predict(coefficients, pointList.get(i).get(0));
double actualVal = pointList.get(i).get(1);
double t = Math.pow((predictedValues[i] - actualVal), 2);
residualSumOfSquares += t;
descriptiveStatistics.addValue(actualVal);
}
final double avgActualValues = descriptiveStatistics.getMean();
double totalSumOfSquares = 0;
for (int i=0; i<pointList.size(); i++) {
totalSumOfSquares += Math.pow( (predictedValues[i] - avgActualValues),2);
}
return 1.0 - (residualSumOfSquares/totalSumOfSquares);
}
这里没有显示"predict"方法,但是注意参数
你在上面称为 "fitter" 实际上是一个多项式
功能:
因此:
predictedValues[i] = fitter.value(pointList.get(i).get(0));
但除此之外,是的,调用者负责选择他的
评估模型的质量。
可以直接使用类的least-squares套件;然后
"Evaluation" 对象将允许检索各种度量
适合度:
但是,它们可能仍然不是您要找的...
Apache commons-math3
(版本3.6.1
)类像OLSMultipleLinearRegression
,SimpleRegression
提供了一种计算RSquare
的方法(即calculateRSquared()
、getRSquare()
)。但是我找不到 PolynomialCurveFitter
?
现在我自己做如下。 common-math 中有这样的方法吗?
private PolynomialFunction getPolynomialFitter(List<List<Double>> pointlist) {
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (List<Double> point : pointlist) {
obs.add(point.get(0), point.get(1));
}
double[] fit = fitter.fit(obs.toList());
System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]);
final PolynomialFunction fitted = new PolynomialFunction(fit);
return fitted;
}
private double getRSquare(PolynomialFunction fitter, List<List<Double>> pointList) {
final double[] coefficients = fitter.getCoefficients();
double[] predictedValues = new double[pointList.size()];
double residualSumOfSquares = 0;
final DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
for (int i=0; i< pointList.size(); i++) {
predictedValues[i] = predict(coefficients, pointList.get(i).get(0));
double actualVal = pointList.get(i).get(1);
double t = Math.pow((predictedValues[i] - actualVal), 2);
residualSumOfSquares += t;
descriptiveStatistics.addValue(actualVal);
}
final double avgActualValues = descriptiveStatistics.getMean();
double totalSumOfSquares = 0;
for (int i=0; i<pointList.size(); i++) {
totalSumOfSquares += Math.pow( (predictedValues[i] - avgActualValues),2);
}
return 1.0 - (residualSumOfSquares/totalSumOfSquares);
}
final PolynomialFunction polynomial = getPolynomialFitter(trainData);
System.out.printf("\nPolynimailCurveFitter R-Square %f", getRSquare(polynomial, trainData));
这已在 apache-commons mailing list 中得到解答。 Cross-posting答案
OLSMultipleLinearRegression, SimpleRegression provide a method that returns calculateRSquared(), getRSquare(). But I am not able to find any such method for PolynomialCurveFitter ?
Right now I am doing it myself like below :-
Is there any such method in common-math which does this?
"PolynomialCurveFitter" 是句法之一 sugar/wrapper 围绕 least-squares 优化器。 在(不可变的)实例中不维护任何状态。
private PolynomialFunction getPolynomialFitter(List<List<Double>>pointlist) { final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2); final WeightedObservedPoints obs = new WeightedObservedPoints(); for (List<Double> point : pointlist) { obs.add(point.get(0), point.get(1)); } double[] fit = fitter.fit(obs.toList()); System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]); final PolynomialFunction fitted = new PolynomialFunction(fit); return fitted; }
这确实是预期的 use-cases。
private double getRSquare(PolynomialFunction fitter, List<List<Double>> pointList) { final double[] coefficients = fitter.getCoefficients(); double[] predictedValues = new double[pointList.size()]; double residualSumOfSquares = 0; final DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(); for (int i=0; i< pointList.size(); i++) { predictedValues[i] = predict(coefficients, pointList.get(i).get(0)); double actualVal = pointList.get(i).get(1); double t = Math.pow((predictedValues[i] - actualVal), 2); residualSumOfSquares += t; descriptiveStatistics.addValue(actualVal); } final double avgActualValues = descriptiveStatistics.getMean(); double totalSumOfSquares = 0; for (int i=0; i<pointList.size(); i++) { totalSumOfSquares += Math.pow( (predictedValues[i] - avgActualValues),2); } return 1.0 - (residualSumOfSquares/totalSumOfSquares); }
这里没有显示"predict"方法,但是注意参数 你在上面称为 "fitter" 实际上是一个多项式 功能:
因此:
predictedValues[i] = fitter.value(pointList.get(i).get(0));
但除此之外,是的,调用者负责选择他的 评估模型的质量。
可以直接使用类的least-squares套件;然后 "Evaluation" 对象将允许检索各种度量 适合度:
但是,它们可能仍然不是您要找的...