Search in sources :

Example 26 with JsonObject

use of dontweave.gson.JsonObject in project h2o-2 by h2oai.

the class SpeeDRF method buildForest.

private void buildForest() {
    logStart();
    SpeeDRFModel model = null;
    try {
        Frame train = setTrain();
        Frame test = setTest();
        Vec resp = regression ? null : train.lastVec().toEnum();
        if (resp != null)
            gtrash(resp);
        float[] priorDist = setPriorDist(train);
        train = setStrat(train, test, resp);
        model = initModel(train, test, priorDist);
        model.start_training(null);
        model.write_lock(self());
        drfParams = DRFParams.create(train.find(resp), model.N, model.max_depth, (int) train.numRows(), model.nbins, model.statType, use_seed, model.weights, mtries, model.sampling_strategy, (float) sample_rate, model.strata_samples, model.verbose ? 100 : 1, _exclusiveSplitLimit, true, regression);
        DRFTask tsk = new DRFTask(self(), train, drfParams, model._key, model.src_key);
        tsk.validateInputData(train);
        tsk.invokeOnAllNodes();
        Log.info("Tree building complete. Scoring...");
        model = UKV.get(dest());
        model.scoreAllTrees(test == null ? train : test, resp);
        // Launch a Variable Importance Task
        if (importance && !regression) {
            Log.info("Scoring complete. Performing Variable Importance Calculations.");
            model.current_status = "Performing Variable Importance Calculation.";
            Timer VITimer = new Timer();
            model.variableImportanceCalc(train, resp);
            Log.info("Variable Importance on " + (train.numCols() - 1) + " variables and " + ntrees + " trees done in " + VITimer);
        }
        Log.info("Generating Tree Stats");
        JsonObject trees = new JsonObject();
        trees.addProperty(Constants.TREE_COUNT, model.size());
        if (model.size() > 0) {
            trees.add(Constants.TREE_DEPTH, model.depth().toJson());
            trees.add(Constants.TREE_LEAVES, model.leaves().toJson());
        }
        model.generateHTMLTreeStats(new StringBuilder(), trees);
        model.current_status = "Model Complete";
    } finally {
        if (model != null) {
            model.unlock(self());
            model.stop_training();
        }
    }
}
Also used : Frame(water.fvec.Frame) Timer(water.Timer) Vec(water.fvec.Vec) JsonObject(dontweave.gson.JsonObject)

Example 27 with JsonObject

use of dontweave.gson.JsonObject in project h2o-2 by h2oai.

the class SpeeDRFModel method generateHTML.

public void generateHTML(String title, StringBuilder sb) {
    String style = "<style>\n" + "td, th { min-width:60px;}\n" + "</style>\n";
    sb.append(style);
    DocGen.HTML.title(sb, title);
    sb.append("<div class=\"alert\">").append("Actions: ");
    sb.append(Inspect2.link("Inspect training data (" + _dataKey.toString() + ")", _dataKey)).append(", ");
    if (validation)
        sb.append(Inspect2.link("Inspect testing data (" + testKey.toString() + ")", testKey)).append(", ");
    sb.append(Predict.link(_key, "Score on dataset"));
    if (this.size() > 0 && this.size() < N && !Job.findJob(jobKey).isCancelledOrCrashed()) {
        sb.append(", ");
        sb.append("<i class=\"icon-stop\"></i>&nbsp;").append(Cancel.link(jobKey, "Cancel training"));
    }
    sb.append("</div>");
    DocGen.HTML.paragraph(sb, "Model Key: " + _key);
    DocGen.HTML.paragraph(sb, "Max max_depth: " + max_depth + ", Nbins: " + nbins + ", Trees: " + this.size());
    DocGen.HTML.paragraph(sb, "Sample Rate: " + sample + ", User Seed: " + get_params().seed + ", Internal Seed: " + zeed + ", mtry: " + mtry);
    sb.append("</pre>");
    if (this.size() > 0 && this.size() < N)
        sb.append("Current Status: ").append("Building Random Forest");
    else {
        if (this.size() == N && !this.current_status.equals("Performing Variable Importance Calculation.")) {
            sb.append("Current Status: ").append("Complete.");
        } else {
            if (Job.findJob(jobKey).isCancelledOrCrashed()) {
                sb.append("Current Status: ").append("Cancelled.");
            } else {
                sb.append("Current Status: ").append(this.current_status);
            }
        }
    }
    if (_have_cv_results) {
        sb.append("<div class=\"alert\">Scoring results reported for ").append(this.parameters.n_folds).append("-fold cross-validated training data ").append(Inspect2.link(_dataKey.toString(), _dataKey)).append("</div>");
    } else {
        if (testKey != null)
            sb.append("<div class=\"alert\">Reported on ").append(Inspect2.link(testKey.toString(), testKey)).append("</div>");
        else
            sb.append("<div class=\"alert\">Reported on ").append(oobee ? "OOB" : "training").append(" data</div>");
    }
    //build cm
    if (!regression) {
        //      } else {
        if (this.cms[this.cms.length - 1] != null && (this.N * .25 > 0 && classes() >= 2)) {
            this.cms[this.cms.length - 1].toHTML(sb, this.cmDomain);
        //        }
        }
    }
    sb.append("<br />");
    if (errsNotNull() && this.size() > 0) {
        DocGen.HTML.section(sb, "Mean Squared Error by Tree");
        DocGen.HTML.arrayHead(sb);
        sb.append("<tr style='min-width:60px'><th>Trees</th>");
        // + 1;
        int last = this.size();
        for (int i = last; i >= 0; i--) sb.append("<td style='min-width:60px'>").append(i).append("</td>");
        sb.append("</tr>");
        sb.append("<tr style='min-width: 60px;'><th style='min-width: 60px;' class='warning'>MSE</th>");
        for (int i = last; i >= 0; i--) sb.append((!(Double.isNaN(errs[i]) || errs[i] <= 0.0)) ? String.format("<td style='min-width:60px'>%5.5f</td>", errs[i]) : "<td style='min-width:60px'>---</td>");
        sb.append("</tr>");
        DocGen.HTML.arrayTail(sb);
    }
    sb.append("<br/>");
    JsonObject trees = new JsonObject();
    trees.addProperty(Constants.TREE_COUNT, this.size());
    if (this.size() > 0) {
        trees.add(Constants.TREE_DEPTH, this.depth().toJson());
        trees.add(Constants.TREE_LEAVES, this.leaves().toJson());
    }
    if (validAUC != null) {
        generateHTMLAUC(sb);
    }
    generateHTMLTreeStats(sb, trees);
    if (varimp != null) {
        generateHTMLVarImp(sb);
    }
    printCrossValidationModelsHTML(sb);
}
Also used : JsonObject(dontweave.gson.JsonObject)

Example 28 with JsonObject

use of dontweave.gson.JsonObject in project h2o-2 by h2oai.

the class Models method summarizeDeepLearningModel.

/**
   * Summarize fields which are specific to hex.deeplearning.DeepLearningModel.
   */
private static void summarizeDeepLearningModel(ModelSummary summary, hex.deeplearning.DeepLearningModel model) {
    // add generic fields such as column names
    summarizeModelCommonFields(summary, model);
    summary.model_algorithm = "DeepLearning";
    JsonObject all_params = (model.get_params()).toJSON();
    summary.critical_parameters = whitelistJsonObject(all_params, DL_critical_params);
    summary.secondary_parameters = whitelistJsonObject(all_params, DL_secondary_params);
    summary.expert_parameters = whitelistJsonObject(all_params, DL_expert_params);
}
Also used : JsonObject(dontweave.gson.JsonObject)

Example 29 with JsonObject

use of dontweave.gson.JsonObject in project h2o-2 by h2oai.

the class Models method serveOneOrAll.

/**
   * Fetch all the Models from the KV store, sumamrize and enhance them, and return a map of them.
   */
private Response serveOneOrAll(Map<String, Model> modelsMap) {
    // returns empty sets if !this.find_compatible_frames
    Pair<Map<String, Frame>, Map<String, Set<String>>> frames_info = fetchFrames();
    Map<String, Frame> all_frames = frames_info.getFirst();
    Map<String, Set<String>> all_frames_cols = frames_info.getSecond();
    Map<String, ModelSummary> modelSummaries = Models.generateModelSummaries(null, modelsMap, find_compatible_frames, all_frames, all_frames_cols);
    Map resultsMap = new LinkedHashMap();
    resultsMap.put("models", modelSummaries);
    // If find_compatible_frames then include a map of the Frame summaries.  Should we put this on a separate switch?
    if (this.find_compatible_frames) {
        Set<String> all_referenced_frames = new TreeSet<String>();
        for (Map.Entry<String, ModelSummary> entry : modelSummaries.entrySet()) {
            ModelSummary summary = entry.getValue();
            all_referenced_frames.addAll(summary.compatible_frames);
        }
        Map<String, FrameSummary> frameSummaries = Frames.generateFrameSummaries(all_referenced_frames, all_frames, false, null, null);
        resultsMap.put("frames", frameSummaries);
    }
    // TODO: temporary hack to get things going
    String json = gson.toJson(resultsMap);
    JsonObject result = gson.fromJson(json, JsonElement.class).getAsJsonObject();
    return Response.done(result);
}
Also used : Frame(water.fvec.Frame) JsonObject(dontweave.gson.JsonObject) FrameSummary(water.api.Frames.FrameSummary) JsonElement(dontweave.gson.JsonElement)

Example 30 with JsonObject

use of dontweave.gson.JsonObject in project h2o-2 by h2oai.

the class Models method summarizeNBModel.

/**
   * Summarize fields which are specific to hex.nb.NBModel.
   */
private static void summarizeNBModel(ModelSummary summary, hex.nb.NBModel model) {
    // add generic fields such as column names
    summarizeModelCommonFields(summary, model);
    summary.model_algorithm = "Naive Bayes";
    JsonObject all_params = (model.get_params()).toJSON();
    summary.critical_parameters = whitelistJsonObject(all_params, NB_critical_params);
    summary.secondary_parameters = whitelistJsonObject(all_params, NB_secondary_params);
    summary.expert_parameters = whitelistJsonObject(all_params, NB_expert_params);
}
Also used : JsonObject(dontweave.gson.JsonObject)

Aggregations

JsonObject (dontweave.gson.JsonObject)41 JsonElement (dontweave.gson.JsonElement)5 JsonParser (dontweave.gson.JsonParser)4 JsonArray (dontweave.gson.JsonArray)3 Frame (water.fvec.Frame)3 Job (water.Job)2 Value (water.Value)2 JsonPrimitive (dontweave.gson.JsonPrimitive)1 JsonWriter (dontweave.gson.stream.JsonWriter)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 TimerTask (java.util.TimerTask)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 Path (org.apache.hadoop.fs.Path)1 Key (water.Key)1 Timer (water.Timer)1 FrameSummary (water.api.Frames.FrameSummary)1