请解决stdout错误 [英] Please resolve stdout error
本文介绍了请解决stdout错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的BTech最后一年项目编写poly回归代码。
我有一个代码,但是有一个错误stdout。
请解决。
我尝试过:
I want to code poly regression for my BTech final year project.
I have a code but there is an error of "stdout ".
please resolve.
What I have tried:
import Jama.Matrix;
import Jama.QRDecomposition;
public class PolynomialRegression {
private final String variableName; // name of the predictor variable
private int degree; // degree of the polynomial regression
private Matrix beta; // the polynomial regression coefficients
private double sse; // sum of squares due to error
private double sst; // total sum of squares
/**
* Performs a polynomial reggression on the data points {@code (y[i], x[i])}.
* Uses n as the name of the predictor variable.
*
* @param x the values of the predictor variable
* @param y the corresponding values of the response variable
* @param degree the degree of the polynomial to fit
* @throws IllegalArgumentException if the lengths of the two arrays are not equal
*/
public PolynomialRegression(double[] x, double[] y, int degree) {
this(x, y, degree, "n");
}
/**
* Performs a polynomial reggression on the data points {@code (y[i], x[i])}.
*
* @param x the values of the predictor variable
* @param y the corresponding values of the response variable
* @param degree the degree of the polynomial to fit
* @param variableName the name of the predictor variable
* @throws IllegalArgumentException if the lengths of the two arrays are not equal
*/
public PolynomialRegression(double[] x, double[] y, int degree, String variableName) {
this.degree = degree;
this.variableName = variableName;
int n = x.length;
QRDecomposition qr = null;
Matrix matrixX = null;
// in case Vandermonde matrix does not have full rank, reduce degree until it does
while (true) {
// build Vandermonde matrix
double[][] vandermonde = new double[n][this.degree+1];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= this.degree; j++) {
vandermonde[i][j] = Math.pow(x[i], j);
}
}
matrixX = new Matrix(vandermonde);
// find least squares solution
qr = new QRDecomposition(matrixX);
if (qr.isFullRank()) break;
// decrease degree and try again
this.degree--;
}
// create matrix from vector
Matrix matrixY = new Matrix(y, n);
// linear regression coefficients
beta = qr.solve(matrixY);
// mean of y[] values
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += y[i];
double mean = sum / n;
// total variation to be accounted for
for (int i = 0; i < n; i++) {
double dev = y[i] - mean;
sst += dev*dev;
}
// variation not accounted for
Matrix residuals = matrixX.times(beta).minus(matrixY);
sse = residuals.norm2() * residuals.norm2();
}
/**
* Returns the {@code j}th regression coefficient.
*
* @param j the index
* @return the {@code j}th regression coefficient
*/
public double beta(int j) {
// to make -0.0 print as 0.0
if (Math.abs(beta.get(j, 0)) < 1E-4) return 0.0;
return beta.get(j, 0);
}
/**
* Returns the degree of the polynomial to fit.
*
* @return the degree of the polynomial to fit
*/
public int degree() {
return degree;
}
/**
* Returns the coefficient of determination R<sup>2</sup>.
*
* @return the coefficient of determination R<sup>2</sup>,
* which is a real number between 0 and 1
*/
public double R2() {
if (sst == 0.0) return 1.0; // constant function
return 1.0 - sse/sst;
}
/**
* Returns the expected response {@code y} given the value of the predictor
* variable {@code x}.
*
* @param x the value of the predictor variable
* @return the expected response {@code y} given the value of the predictor
* variable {@code x}
*/
public double predict(double x) {
// horner's method
double y = 0.0;
for (int j = degree; j >= 0; j--)
y = beta(j) + (x * y);
return y;
}
/**
* Returns a string representation of the polynomial regression model.
*
* @return a string representation of the polynomial regression model,
* including the best-fit polynomial and the coefficient of
* determination R<sup>2</sup>
*/
public String toString() {
StringBuilder s = new StringBuilder();
int j = degree;
// ignoring leading zero coefficients
while (j >= 0 && Math.abs(beta(j)) < 1E-5)
j--;
// create remaining terms
while (j >= 0) {
if (j == 0) s.append(String.format("%.2f ", beta(j)));
else if (j == 1) s.append(String.format("%.2f %s + ", beta(j), variableName));
else s.append(String.format("%.2f %s^%d + ", beta(j), variableName, j));
j--;
}
s = s.append(" (R^2 = " + String.format("%.3f", R2()) + ")");
return s.toString();
}
/**
* Unit tests the {@code PolynomialRegression} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
double[] x = { 10, 20, 40, 80, 160, 200 };
double[] y = { 100, 350, 1500, 6700, 20160, 40000 };
PolynomialRegression regression = new PolynomialRegression(x, y, 3);
StdOut.println(regression);
}
}
推荐答案
为什么不直接使用System.out.println?如果你想使用StdOut.java,你必须下载它,使用它,并在你的工作中引用它
StdOut [ ^ ]
Why not just use System.out.println ? if you wish to use StdOut.java, you'll have to download it, use it, and reference it in your work
StdOut[^]
这篇关于请解决stdout错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文