@Test public void testPredictWithTwoClasses() { double first = 1; double second = 2; double[][][] probabilities = new double[][][] { {{.5, .5}, {.2, .3, .5}, {2. / 3., 1. / 3.}, {.4, .1, .5}, {.5, .5}}, {{0, 1}, {1. / 7, 2. / 7, 4. / 7}, {4. / 7, 3. / 7}, {2. / 7, 3. / 7, 2. / 7}, {4. / 7, 3. / 7,}} }; double[] classProbabilities = new double[] {6. / 13, 7. / 13}; double[][] thresholds = new double[][] {{.5}, {.2, .7}, {.5}, {.5, 1.5}, {.5}}; DiscreteNaiveBayesModel mdl = new DiscreteNaiveBayesModel(probabilities, classProbabilities, new double[] {first, second}, thresholds, new DiscreteNaiveBayesSumsHolder()); Vector observation = VectorUtils.of(2, 0, 1, 2, 0); Assert.assertEquals(second, mdl.predict(observation), 0.0001); }
@Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; for (int i = 0; i < clsProbabilities.length; i++) { double probabilityPower = Math.log(clsProbabilities[i]); for (int j = 0; j < probabilities[0].length; j++) { int x = toBucketNumber(vector.get(j), bucketThresholds[j]); double p = probabilities[i][j][x]; probabilityPower += (p > 0 ? Math.log(p) : .0); } if (probabilityPower > maxProbapilityPower) { maxLabelIndex = i; maxProbapilityPower = probabilityPower; } } return labels[maxLabelIndex]; }
DiscreteNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { @Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; for (int i = 0; i < clsProbabilities.length; i++) { double probabilityPower = Math.log(clsProbabilities[i]); for (int j = 0; j < probabilities[0].length; j++) { int x = toBucketNumber(vector.get(j), bucketThresholds[j]); double p = probabilities[i][j][x]; probabilityPower += (p > 0 ? Math.log(p) : .0); } if (probabilityPower > maxProbapilityPower) { maxLabelIndex = i; maxProbapilityPower = probabilityPower; } } return labels[maxLabelIndex]; } }
DiscreteNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { @Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; for (int i = 0; i < clsProbabilities.length; i++) { double probabilityPower = Math.log(clsProbabilities[i]); for (int j = 0; j < probabilities[0].length; j++) { int x = toBucketNumber(vector.get(j), bucketThresholds[j]); double p = probabilities[i][j][x]; probabilityPower += (p > 0 ? Math.log(p) : .0); } if (probabilityPower > maxProbapilityPower) { maxLabelIndex = i; maxProbapilityPower = probabilityPower; } } return labels[maxLabelIndex]; } DiscreteNaiveBayesModel(double[][][] probabilities, double[] clsProbabilities, double[] labels,
        double[][] bucketThresholds, DiscreteNaiveBayesSumsHolder sumsHolder); }
DiscreteNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { @Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; for (int i = 0; i < clsProbabilities.length; i++) { double probabilityPower = Math.log(clsProbabilities[i]); for (int j = 0; j < probabilities[0].length; j++) { int x = toBucketNumber(vector.get(j), bucketThresholds[j]); double p = probabilities[i][j][x]; probabilityPower += (p > 0 ? Math.log(p) : .0); } if (probabilityPower > maxProbapilityPower) { maxLabelIndex = i; maxProbapilityPower = probabilityPower; } } return labels[maxLabelIndex]; } DiscreteNaiveBayesModel(double[][][] probabilities, double[] clsProbabilities, double[] labels,
        double[][] bucketThresholds, DiscreteNaiveBayesSumsHolder sumsHolder); @Override void saveModel(Exporter<DiscreteNaiveBayesModel, P> exporter, P path); @Override Double predict(Vector vector); double[][][] getProbabilities(); double[] getClsProbabilities(); double[][] getBucketThresholds(); DiscreteNaiveBayesSumsHolder getSumsHolder(); }
DiscreteNaiveBayesModel implements IgniteModel<Vector, Double>, Exportable<DiscreteNaiveBayesModel>, Serializable { @Override public Double predict(Vector vector) { double maxProbapilityPower = -Double.MAX_VALUE; int maxLabelIndex = -1; for (int i = 0; i < clsProbabilities.length; i++) { double probabilityPower = Math.log(clsProbabilities[i]); for (int j = 0; j < probabilities[0].length; j++) { int x = toBucketNumber(vector.get(j), bucketThresholds[j]); double p = probabilities[i][j][x]; probabilityPower += (p > 0 ? Math.log(p) : .0); } if (probabilityPower > maxProbapilityPower) { maxLabelIndex = i; maxProbapilityPower = probabilityPower; } } return labels[maxLabelIndex]; } DiscreteNaiveBayesModel(double[][][] probabilities, double[] clsProbabilities, double[] labels,
        double[][] bucketThresholds, DiscreteNaiveBayesSumsHolder sumsHolder); @Override void saveModel(Exporter<DiscreteNaiveBayesModel, P> exporter, P path); @Override Double predict(Vector vector); double[][][] getProbabilities(); double[] getClsProbabilities(); double[][] getBucketThresholds(); DiscreteNaiveBayesSumsHolder getSumsHolder();  }