Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
A Multiple Local Predictive Model.
This module defines a Multiple Model to make predictions locally using multiple
local models.
This module cannot only save you a few credits, but also enormously
reduce the latency for each prediction and let you use your models
offline.
import org.bigml.binding.BigMLClient;
import org.bigml.binding.MultiModel;
BigMLClient bigmlClient = new BigMLClient();
JSONObject models = bigmlClient.listModels("my_tag");
MultiModel model = new MultiModel(models);
model.predict({"petal length": 3, "petal width": 1})
*/
package org.bigml.binding;
import java.io.*;
import java.util.*;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.bigml.binding.localmodel.Prediction;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MultiModel implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Logging
*/
static Logger logger = LoggerFactory.getLogger(MultiModel.class.getName());
private static String PREDICTIONS_FILE_SUFFIX = "_predictions.csv";
private JSONArray models;
protected JSONObject fields = null;
private List classNames = new ArrayList();
private MultiVote votes;
private List localModels =
new ArrayList();
/**
* Constructor
*
* @param models the json representation for the remote models
*/
public MultiModel(Object models)
throws Exception {
this(models, null, null);
}
/**
* Constructor
*
* @param models the json representation for the remote models
*/
public MultiModel(Object models, JSONObject fields, List classNames)
throws Exception {
super();
if (models instanceof JSONArray) {
this.models = (JSONArray) models;
} else if( models instanceof List ) {
this.models = new JSONArray();
this.models.addAll((List) models);
} else {
this.models = new JSONArray();
this.models.add(models);
}
this.classNames = classNames;
for (Object model: this.models) {
LocalPredictiveModel localModel =
new LocalPredictiveModel((JSONObject) model);
if (fields != null) {
localModel.setFields(fields);
}
localModels.add(localModel);
}
}
/**
* Lists all the model/ids that compound the multi model.
*/
public JSONArray listModels() {
return this.models;
}
/**
* Generates a MultiVote object that contains the predictions made
* by each of the models.
*/
public MultiVote generateVotes(final JSONObject inputData,
MissingStrategy strategy, List unusedFields)
throws Exception {
if (strategy == null) {
strategy = MissingStrategy.LAST_PREDICTION;
}
MultiVote votes = new MultiVote();
for (int i = 0; i < localModels.size(); i++) {
LocalPredictiveModel localModel = (LocalPredictiveModel) localModels.get(i);
Prediction predictionInfo = localModel.predict(
inputData, strategy, null, null, true, unusedFields);
if (localModel.isBoosting()) {
votes.boosting = true;
predictionInfo.put("weight", localModel.getBoosting().get("weight"));
String objectiveClass = (String)
localModel.getBoosting().get("objective_class");
if (objectiveClass != null) {
predictionInfo.put("class", objectiveClass);
}
}
votes.append(predictionInfo);
}
return votes;
}
/**
* Generates a MultiVote object that contains the predictions made by each
* of the models.
*/
public MultiVoteList generateVotesDistribution(
final JSONObject inputData, MissingStrategy strategy,
PredictionMethod method) throws Exception {
if (strategy == null) {
strategy = MissingStrategy.LAST_PREDICTION;
}
if (method == null) {
method = PredictionMethod.PROBABILITY;
}
MultiVoteList votes = new MultiVoteList(null);
for (int i = 0; i < localModels.size(); i++) {
LocalPredictiveModel localModel = (LocalPredictiveModel) localModels.get(i);
localModel.setClassNames(classNames);
if (method == PredictionMethod.PLURALITY) {
double total = 0.0;
List predictionList = new ArrayList();
for (int j=0; j predictionList = new ArrayList();
for (Object pred : predictionInfo) {
Prediction prediction = (Prediction) pred;
predictionList.add((Double) prediction.get(key));
}
votes.append(predictionList);
}
}
return votes;
}
/**
* Makes a prediction based on the prediction made by every model.
*
* The method parameter is a numeric key to the following combination
* methods in classifications/regressions:
* 0 - majority vote (plurality)/ average: PLURALITY_CODE
* 1 - confidence weighted majority vote / error weighted:
* CONFIDENCE_CODE
* 2 - probability weighted majority vote / average:
* PROBABILITY_CODE
* 3 - threshold filtered vote / doesn't apply:
* THRESHOLD_COD
*/
public HashMap