Skip to content

Commit

Permalink
Added makeModel call to R/H2O to allow for hand made glm models with …
Browse files Browse the repository at this point in the history
…efficient scoring.
  • Loading branch information
tomasnykodym committed Jan 28, 2015
1 parent 5980d62 commit 4603531
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
8 changes: 8 additions & 0 deletions R/h2o-package/R/Algorithms.R
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,14 @@ h2o.predict <- function(object, newdata, ...) {
stop(paste("Prediction has not yet been implemented for", class(object)))
}


h2o.makeGLMModel <- function(model, beta) {
if( missing(model) || class(model) != "H2OGLMModel")
stop('Must specify source glm model')
res = .h2o.__remoteSend(model@data@h2o, .h2o.__GLMMakeModel, model=model@key, names = paste(names(beta),sep=","), beta = as.vector(beta))
.h2o.get.glm(model@data@h2o, as.character(res$destination_key), FALSE)
}

h2o.confusionMatrix <- function(data, reference) {
if(class(data) != "H2OParsedData") stop("data must be an H2O parsed dataset")
if(class(reference) != "H2OParsedData") stop("reference must be an H2O parsed dataset")
Expand Down
2 changes: 2 additions & 0 deletions R/h2o-package/R/Internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ h2o.setLogPath <- function(path, type) {
.h2o.__PAGE_RemoveVec = "2/RemoveVec.json"
.h2o.__PAGE_Order = "2/Order.json"

.h2o.__GLMMakeModel = "2/GLMMakeModel.json"

# client -- Connection object returned from h2o.init().
# page -- URL to access within the H2O server.
# parms -- List of parameters to send to the server.
Expand Down
4 changes: 2 additions & 2 deletions R/h2o-package/R/models.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ function(h2o, key, lambda_idx = -1, return_all_lambda = TRUE, pre = "", data = N
names(result$coefficients) <- pre$json$glm_model$coefficients_names[idxes]
if (!is.null(result$normalized_coefficients))
names(result$normalized_coefficients) <- pre$json$glm_model$coefficients_names[idxes]
if(.isBinomial(pre)) { # build and set the confusion matrix
if(.isBinomial(pre) && !is.null(valid) && !is.null(valid$cms)) { # build and set the confusion matrix
cm_ind <- trunc(100*result$best_threshold) + 1
if ( trunc(100 * result$best_threshold) + 1 > length(valid$cms)) {
if (trunc(100 * result$best_threshold) + 1 > length(valid$cms)) {
threshs <- pre$json$glm_model$submodels[[lambda_idx]]$validation$thresholds
cm_ind <- which(threshs == result$best_threshold)
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/hex/glm/GLMModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class GLMModel extends Model implements Comparable<GLMModel> {
@API(help="index of lambda_value giving best results")
int best_lambda_idx;

public DataInfo dinfo(){return data_info;}
public Key [] xvalModels() {
if(submodels == null)return null;
for(Submodel sm:submodels)
Expand Down Expand Up @@ -151,9 +152,7 @@ public int compareTo(GLMModel m){
// fully expanded beta used for scoring
private double [] global_beta;

public void setBestSubmodel(double lambda){

}

@API(help="Validation of the null model")
public GLMValidation null_validation;
Expand Down Expand Up @@ -220,6 +219,25 @@ public Submodel(double lambda , double [] beta, double [] norm_beta, long run_ti
@API(help = "Variable importances", json=true)
VarImp variable_importances;

public GLMModel(GLM2 parameters, Key selfKey, Key dataKey, GLMParams glmp, String [] coefficients_names, double [] beta, DataInfo dinfo, double threshold) {
super(selfKey,dataKey,dinfo._adaptedFrame,null);
this.parameters = (GLM2)parameters.clone();
submodels = new Submodel[]{new Submodel(0,beta,null,-1,-1,false)};
this.coefficients_names = coefficients_names;
alpha = 0;
lambda_max = Double.NaN;
this.threshold = threshold;
useAllFactorLevels = dinfo._useAllFactorLevels;
global_beta = submodels[0].beta.clone();
this.glm = glmp;
this.ymu = Double.NaN;
this.prior = Double.NaN;
this.warnings = new String[]{"Hand made model."};
this.data_info = dinfo;
this.beta_eps = Double.NaN;
this.job_key = null;
best_lambda_idx = 0;
}
public GLMModel(GLM2 job, Key selfKey, DataInfo dinfo, GLMParams glm, GLMValidation nullVal, double beta_eps, double alpha, double lambda_max, double ymu, double prior) {
super(selfKey,job.source._key == null ? dinfo._frameKey : job.source._key,dinfo._adaptedFrame, /* priorClassDistribution */ null);
parameters = Job.hygiene((GLM2) job.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/water/api/RequestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public enum API_VERSION {
// Register Inspect2 just for viewing frames
registerRequest(new Inspect2());
registerRequest(new MMStats());

registerRequest(new GLMMakeModel());
// FVec models
Request.addToNavbar(registerRequest(new DeepLearning()),"Deep Learning", "Model");
Request.addToNavbar(registerRequest(new GLM2()), "Generalized Linear Model", "Model");
Expand Down

0 comments on commit 4603531

Please sign in to comment.