public double[] eval(String[] context, double[] probs) { return eval(context, null, probs); }
private static double getProbability(double numerator, double denominator, double vocabulary, boolean isSmoothed) { if (isSmoothed) return getSmoothedProbability(numerator, denominator, vocabulary); else if (denominator == 0 || denominator < Double.MIN_VALUE) return 0; else return 1.0 * numerator / denominator; }
NaiveBayesModel(Context[] params, String[] predLabels, Map<String, Context> pmap, String[] outcomeNames) { super(params, predLabels, pmap, outcomeNames); outcomeTotals = initOutcomeTotals(outcomeNames, params); this.evalParams = new NaiveBayesEvalParameters(params, outcomeNames.length, outcomeTotals, predLabels.length); modelType = ModelType.NaiveBayes; }
private static String[] extractLabels(NaiveBayesModel model) { String[] labels = new String[model.getNumOutcomes()]; for (int i = 0; i < model.getNumOutcomes(); i++) { labels[i] = model.getOutcome(i); } return labels; } }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (model type identifier) * <br>1. # of parameters (int) * <br>2. # of outcomes (int) * <br> * list of outcome names (String) * <br>3. # of different types of outcome patterns (int) * <br> * list of (int int[]) * <br> [# of predicates for which outcome pattern is true] [outcome pattern] * <br>4. # of predicates (int) * <br> * list of predicate names (String) * * <p>If you are creating a reader for a format which won't work with this * (perhaps a database or xml file), override this method and ignore the * other methods provided in this abstract class. * * @return The NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (usually via its the constructor). */ public AbstractModel constructModel() throws IOException { String[] outcomeLabels = getOutcomes(); int[][] outcomePatterns = getOutcomePatterns(); String[] predLabels = getPredicates(); Context[] params = getParameters(outcomePatterns); return new NaiveBayesModel(params, predLabels, outcomeLabels); }
double numerator = oid == i ? activeParameters[ai++] * value : 0; double denominator = outcomeTotals[i]; probabilities.addIn(i, getProbability(numerator, denominator, vocabulary, true), 1);
public AbstractModel trainModel(DataIndexer di) { display("Incorporating indexed data for training... \n"); contexts = di.getContexts(); values = di.getValues(); numTimesEventsSeen = di.getNumTimesEventsSeen(); numEvents = di.getNumEvents(); numUniqueEvents = contexts.length; outcomeLabels = di.getOutcomeLabels(); outcomeList = di.getOutcomeList(); predLabels = di.getPredLabels(); numPreds = predLabels.length; numOutcomes = outcomeLabels.length; display("done.\n"); display("\tNumber of Event Tokens: " + numUniqueEvents + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); display("Computing model parameters...\n"); MutableContext[] finalParameters = findParameters(); display("...done.\n"); /* Create and return the model ****/ return new NaiveBayesModel(finalParameters, predLabels, outcomeLabels); }
double numerator = oid == i ? activeParameters[ai++] * value : 0; double denominator = outcomeTotals[i]; probabilities.addIn(i, getProbability(numerator, denominator, vocabulary, true), 1);
public static double[] eval(int[] context, double[] prior, EvalParameters model) { return eval(context, null, prior, model, true); }
public NaiveBayesModel(Context[] params, String[] predLabels, String[] outcomeNames) { super(params, predLabels, outcomeNames); outcomeTotals = initOutcomeTotals(outcomeNames, params); this.evalParams = new NaiveBayesEvalParameters(params, outcomeNames.length, outcomeTotals, predLabels.length); modelType = ModelType.NaiveBayes; }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (model type identifier) * <br>1. # of parameters (int) * <br>2. # of outcomes (int) * <br> * list of outcome names (String) * <br>3. # of different types of outcome patterns (int) * <br> * list of (int int[]) * <br> [# of predicates for which outcome pattern is true] [outcome pattern] * <br>4. # of predicates (int) * <br> * list of predicate names (String) * * <p>If you are creating a reader for a format which won't work with this * (perhaps a database or xml file), override this method and ignore the * other methods provided in this abstract class. * * @return The NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (usually via its the constructor). */ public AbstractModel constructModel() throws IOException { String[] outcomeLabels = getOutcomes(); int[][] outcomePatterns = getOutcomePatterns(); String[] predLabels = getPredicates(); Context[] params = getParameters(outcomePatterns); return new NaiveBayesModel(params, predLabels, outcomeLabels); }
private static double getProbability(double numerator, double denominator, double vocabulary, boolean isSmoothed) { if (isSmoothed) return getSmoothedProbability(numerator, denominator, vocabulary); else if (denominator == 0 || denominator < Double.MIN_VALUE) return 0; else return 1.0 * numerator / denominator; }
double numerator = oid == i ? activeParameters[ai++] * value : 0; double denominator = outcomeTotals[i]; probabilities.addIn(i, getProbability(numerator, denominator, vocabulary, true), 1);
public double[] eval(String[] context, float[] values, double[] outsums) { Context[] scontexts = new Context[context.length]; java.util.Arrays.fill(outsums, 0); for (int i = 0; i < context.length; i++) { scontexts[i] = pmap.get(context[i]); } return eval(scontexts, values, outsums, evalParams, true); }
public NaiveBayesModel(Context[] params, String[] predLabels, String[] outcomeNames) { super(params, predLabels, outcomeNames); outcomeTotals = initOutcomeTotals(outcomeNames, params); this.evalParams = new NaiveBayesEvalParameters(params, outcomeNames.length, outcomeTotals, predLabels.length); modelType = ModelType.NaiveBayes; }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (model type identifier) * <br>1. # of parameters (int) * <br>2. # of outcomes (int) * <br> * list of outcome names (String) * <br>3. # of different types of outcome patterns (int) * <br> * list of (int int[]) * <br> [# of predicates for which outcome pattern is true] [outcome pattern] * <br>4. # of predicates (int) * <br> * list of predicate names (String) * * <p>If you are creating a reader for a format which won't work with this * (perhaps a database or xml file), override this method and ignore the * other methods provided in this abstract class. * * @return The NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (usually via its the constructor). */ public AbstractModel constructModel() throws IOException { String[] outcomeLabels = getOutcomes(); int[][] outcomePatterns = getOutcomePatterns(); String[] predLabels = getPredicates(); Context[] params = getParameters(outcomePatterns); return new NaiveBayesModel(params, predLabels, outcomeLabels); }
private static double getProbability(double numerator, double denominator, double vocabulary, boolean isSmoothed) { if (isSmoothed) return getSmoothedProbability(numerator, denominator, vocabulary); else if (denominator == 0 || denominator < Double.MIN_VALUE) return 0; else return 1.0 * numerator / denominator; }
static double[] eval(int[] context, float[] values, double[] prior, EvalParameters model, boolean normalize) { Context[] scontexts = new Context[context.length]; for (int i = 0; i < context.length; i++) { scontexts[i] = model.getParams()[context[i]]; } return eval(scontexts, values, prior, model, normalize); }
NaiveBayesModel(Context[] params, String[] predLabels, Map<String, Context> pmap, String[] outcomeNames) { super(params, predLabels, pmap, outcomeNames); outcomeTotals = initOutcomeTotals(outcomeNames, params); this.evalParams = new NaiveBayesEvalParameters(params, outcomeNames.length, outcomeTotals, predLabels.length); modelType = ModelType.NaiveBayes; }
public AbstractModel trainModel(DataIndexer di) { display("Incorporating indexed data for training... \n"); contexts = di.getContexts(); values = di.getValues(); numTimesEventsSeen = di.getNumTimesEventsSeen(); numEvents = di.getNumEvents(); numUniqueEvents = contexts.length; outcomeLabels = di.getOutcomeLabels(); outcomeList = di.getOutcomeList(); predLabels = di.getPredLabels(); numPreds = predLabels.length; numOutcomes = outcomeLabels.length; display("done.\n"); display("\tNumber of Event Tokens: " + numUniqueEvents + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); display("Computing model parameters...\n"); MutableContext[] finalParameters = findParameters(); display("...done.\n"); /* Create and return the model ****/ return new NaiveBayesModel(finalParameters, predLabels, outcomeLabels); }