Skip to content

Commit

Permalink
Get LR2 hooked up into the API
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffclick committed Aug 8, 2013
1 parent 5ed6988 commit e9b31a8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion prj.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'(jde-run-option-debug nil)
'(jde-run-option-vm-args nil)
'(jde-compile-option-directory "./target/classes")
'(jde-run-option-application-args (quote ("-mainClass" "org.junit.runner.JUnitCore" "hex.gbm.GBMTest" "water.fvec.FVecTest" "water.fvec.NATest" "water.fvec.NewVectorTest")))
'(jde-run-option-application-args (quote ("-mainClassX" "org.junit.runner.JUnitCore" "hex.gbm.GBMTest" "water.fvec.FVecTest" "water.fvec.NATest" "water.fvec.NewVectorTest")))
'(jde-debugger (quote ("JDEbug")))
'(jde-compile-option-source (quote ("1.6")))
'(jde-compile-option-classpath (quote ("./target/classes" "./lib/javassist.jar" "./lib/hadoop/cdh4/hadoop-common.jar" "./lib/hadoop/cdh4/hadoop-auth.jar" "./lib/hadoop/cdh4/slf4j-api-1.6.1.jar" "./lib/hadoop/cdh4/slf4j-nop-1.6.1.jar" "./lib/hadoop/cdh4/hadoop-hdfs.jar" "./lib/hadoop/cdh4/protobuf-java-2.4.0a.jar" "./lib/apache/commons-codec-1.4.jar" "./lib/apache/commons-configuration-1.6.jar" "./lib/apache/commons-lang-2.4.jar" "./lib/apache/commons-logging-1.1.1.jar" "./lib/apache/httpclient-4.1.1.jar" "./lib/apache/httpcore-4.1.jar" "./lib/junit/junit-4.11.jar" "./lib/apache/guava-12.0.1.jar" "./lib/gson/gson-2.2.2.jar" "./lib/poi/poi-3.8-20120326.jar" "./lib/poi/poi-ooxml-3.8-20120326.jar" "./lib/poi/poi-ooxml-schemas-3.8-20120326.jar" "./lib/poi/dom4j-1.6.1.jar" "./lib/Jama/Jama.jar" "./lib/s3/aws-java-sdk-1.3.27.jar" "./lib/log4j/log4j-1.2.15.jar")))
Expand Down
89 changes: 62 additions & 27 deletions src/main/java/hex/LR2.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,77 @@
package hex;

import com.google.gson.JsonObject;
import water.fvec.*;
import water.*;
import water.api.*;
import water.fvec.*;
import water.util.RString;

public class LR2 extends Request {
static final int API_WEAVER = 1; // This file has auto-gen'd doc & json fields
static public DocGen.FieldDoc[] DOC_FIELDS; // Initialized from Auto-Gen code.

// This Request supports the HTML 'GET' command, and this is the help text
// for GET.
static final String DOC_GET = "Linear Regression between 2 columns";

@API(help="Data Frame")
final FrameKey data_key = new FrameKey("data_key");

@API(help="Column X")
final FrameKeyVec vec_x = new FrameKeyVec("vec_x",data_key);

public abstract class LR2 {
@API(help="Column Y")
final FrameKeyVec vec_y = new FrameKeyVec("vec_y",data_key);

@API(help="Pass 1 msec") public long pass1time;
@API(help="Pass 2 msec") public long pass2time;
@API(help="Pass 3 msec") public long pass3time;
@API(help="nrows") public long nrows;
@API(help="beta0") public double beta0;
@API(help="beta1") public double beta1;
@API(help="r-squared") public double r2;
@API(help="SSTO") public double ssto;
@API(help="SSE") public double sse;
@API(help="SSR") public double ssr;
@API(help="beta0 Std Error") public double beta0stderr;
@API(help="beta1 Std Error") public double beta1stderr;

@Override public Response serve() {
Vec X = vec_x.value();
Vec Y = vec_y.value();

public static JsonObject run( Vec X, Vec Y ) {
// Pass 1: compute sums & sums-of-squares
long start = System.currentTimeMillis();
CalcSumsTask lr1 = new CalcSumsTask().doAll(X,Y);
long pass1 = System.currentTimeMillis();
final long n = lr1._n;
pass1time = pass1 - start;
nrows = lr1._n;

// Pass 2: Compute squared errors
final double meanX = lr1._sumX/n;
final double meanY = lr1._sumY/n;
final double meanX = lr1._sumX/nrows;
final double meanY = lr1._sumY/nrows;
CalcSquareErrorsTasks lr2 = new CalcSquareErrorsTasks(meanX, meanY).doAll(X,Y);
long pass2 = System.currentTimeMillis();
pass2time = pass2 - pass1;
ssto = lr2._YYbar;

// Compute the regression
double beta1 = lr2._XYbar / lr2._XXbar;
double beta0 = meanY - beta1 * meanX;
beta1 = lr2._XYbar / lr2._XXbar;
beta0 = meanY - beta1 * meanX;
CalcRegressionTask lr3 = new CalcRegressionTask(beta0,beta1,meanY).doAll(X,Y);
long pass3 = System.currentTimeMillis();
pass3time = pass3 - pass2;

long df = n - 2;
double R2 = lr3._ssr / lr2._YYbar;
long df = nrows - 2;
r2 = lr3._ssr / lr2._YYbar;
double svar = lr3._rss / df;
double svar1 = svar / lr2._XXbar;
double svar0 = svar/n + meanX*meanX*svar1;

JsonObject res = new JsonObject();
res.addProperty("Pass1Msecs", pass1 - start);
res.addProperty("Pass2Msecs", pass2-pass1);
res.addProperty("Pass3Msecs", pass3-pass2);
res.addProperty("Rows", n);
res.addProperty("Beta0", lr3._beta0);
res.addProperty("Beta1", lr3._beta1);
res.addProperty("RSquared", R2);
res.addProperty("Beta0StdErr", Math.sqrt(svar0));
res.addProperty("Beta1StdErr", Math.sqrt(svar1));
res.addProperty("SSTO", lr2._YYbar);
res.addProperty("SSE", lr3._rss);
res.addProperty("SSR", lr3._ssr);
return res;
double svar0 = svar/nrows + meanX*meanX*svar1;
beta0stderr = Math.sqrt(svar0);
beta1stderr = Math.sqrt(svar1);
sse = lr3._rss;
ssr = lr3._ssr;

return new Response(Response.Status.done, this, -1, -1, null);
}

public static class CalcSumsTask extends MRTask2<CalcSumsTask> {
Expand Down Expand Up @@ -119,4 +146,12 @@ public static class CalcRegressionTask extends MRTask2<CalcRegressionTask> {
_ssr += lr3._ssr;
}
}

/** Return the query link to this page */
public static String link(Key k, String content) {
RString rs = new RString("<a href='LR2.query?data_key=%$key'>%content</a>");
rs.replace("key", k.toString());
rs.replace("content", content);
return rs.toString();
}
}
4 changes: 3 additions & 1 deletion src/main/java/water/api/Inspect2.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public static Response redirect(Request req, String src_key) {

sb.append("<div class='alert'>" +
//"View " + SummaryPage2.link(key, "Summary") +
"<br/>Build models using " + DRF2.link(src_key.value(), "Distributed Random Forest") +
"<br/>Build models using " +
DRF2.link(src_key.value(), "Distributed Random Forest") +
hex.LR2.link(src_key.value(), "Linear Regression") +
"</div>");

// Start of where the pagination table goes. For now, just the info button.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/water/api/RequestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static HashMap<String,Request> requests() {
Request.addToNavbar(registerRequest(new Inspect2()), "Inspect", "Beta (FluidVecs!)");
Request.addToNavbar(registerRequest(new KMeans2()), "KMeans2" ,"Beta (FluidVecs!)");
Request.addToNavbar(registerRequest(new DRF2()), "DRF2" ,"Beta (FluidVecs!)");
Request.addToNavbar(registerRequest(new hex.LR2()), "Linear Regression2" ,"Beta (FluidVecs!)");

// internal handlers
//registerRequest(new StaticHTMLPage("/h2o/CoefficientChart.html","chart"));
Expand Down

0 comments on commit e9b31a8

Please sign in to comment.