@Test public void testBatchWCNB() { System.out.println("---Batch WCNB---"); MathEx.setSeed(19650218); int[] prediction = CrossValidation.classification(10, Movie.x, Movie.y, (x, y) -> { DiscreteNaiveBayes bayes = new DiscreteNaiveBayes(DiscreteNaiveBayes.Model.WCNB, 2, Movie.feature.length); bayes.update(x, y); return bayes; }); int nulls = (int) Arrays.stream(prediction).filter(y -> y == Integer.MIN_VALUE).count(); int error = Error.of(Movie.y, prediction) - nulls; System.out.format("Error = %d out of %d%n", error, Movie.x.length - nulls); assertEquals(315, error); }
@Override public void update(int[] x, int y) { if (!isGoodInstance(x)) { logger.info("Skip updating the model with a sample without any feature word"); return; } if (model == Model.TWCNB) { throw new UnsupportedOperationException("TWCNB supports only batch learning"); } y = labels.indexOf(y); switch (model) { case MULTINOMIAL: case CNB: case WCNB: case TWCNB: for (int i = 0; i < p; i++) { ntc[y][i] += x[i]; nt[y] += x[i]; } break; case POLYAURN: for (int i = 0; i < p; i++) { ntc[y][i] += x[i] * 2; nt[y] += x[i] * 2; } break; case BERNOULLI: for (int i = 0; i < p; i++) { if (x[i] > 0) { ntc[y][i]++; } } break; default: throw new IllegalStateException("Unknown model: " + model); } n++; nc[y]++; update(); }
DiscreteNaiveBayes implements OnlineClassifier<int[]>, SoftClassifier<int[]> { @Override public void update(int[] x, int y) { if (!isGoodInstance(x)) { logger.info("Skip updating the model with a sample without any feature word"); return; } if (model == Model.TWCNB) { throw new UnsupportedOperationException("TWCNB supports only batch learning"); } y = labels.indexOf(y); switch (model) { case MULTINOMIAL: case CNB: case WCNB: case TWCNB: for (int i = 0; i < p; i++) { ntc[y][i] += x[i]; nt[y] += x[i]; } break; case POLYAURN: for (int i = 0; i < p; i++) { ntc[y][i] += x[i] * 2; nt[y] += x[i] * 2; } break; case BERNOULLI: for (int i = 0; i < p; i++) { if (x[i] > 0) { ntc[y][i]++; } } break; default: throw new IllegalStateException("Unknown model: " + model); } n++; nc[y]++; update(); } }
DiscreteNaiveBayes implements OnlineClassifier<int[]>, SoftClassifier<int[]> { @Override public void update(int[] x, int y) { if (!isGoodInstance(x)) { logger.info("Skip updating the model with a sample without any feature word"); return; } if (model == Model.TWCNB) { throw new UnsupportedOperationException("TWCNB supports only batch learning"); } y = labels.indexOf(y); switch (model) { case MULTINOMIAL: case CNB: case WCNB: case TWCNB: for (int i = 0; i < p; i++) { ntc[y][i] += x[i]; nt[y] += x[i]; } break; case POLYAURN: for (int i = 0; i < p; i++) { ntc[y][i] += x[i] * 2; nt[y] += x[i] * 2; } break; case BERNOULLI: for (int i = 0; i < p; i++) { if (x[i] > 0) { ntc[y][i]++; } } break; default: throw new IllegalStateException("Unknown model: " + model); } n++; nc[y]++; update(); } DiscreteNaiveBayes(Model model, int k, int p);  DiscreteNaiveBayes(Model model, int k, int p, double sigma, IntSet labels);  DiscreteNaiveBayes(Model model, double[] priori, int p);  DiscreteNaiveBayes(Model model, double[] priori, int p, double sigma, IntSet labels); }
DiscreteNaiveBayes implements OnlineClassifier<int[]>, SoftClassifier<int[]> { @Override public void update(int[] x, int y) { if (!isGoodInstance(x)) { logger.info("Skip updating the model with a sample without any feature word"); return; } if (model == Model.TWCNB) { throw new UnsupportedOperationException("TWCNB supports only batch learning"); } y = labels.indexOf(y); switch (model) { case MULTINOMIAL: case CNB: case WCNB: case TWCNB: for (int i = 0; i < p; i++) { ntc[y][i] += x[i]; nt[y] += x[i]; } break; case POLYAURN: for (int i = 0; i < p; i++) { ntc[y][i] += x[i] * 2; nt[y] += x[i] * 2; } break; case BERNOULLI: for (int i = 0; i < p; i++) { if (x[i] > 0) { ntc[y][i]++; } } break; default: throw new IllegalStateException("Unknown model: " + model); } n++; nc[y]++; update(); } DiscreteNaiveBayes(Model model, int k, int p);  DiscreteNaiveBayes(Model model, int k, int p, double sigma, IntSet labels);  DiscreteNaiveBayes(Model model, double[] priori, int p);  DiscreteNaiveBayes(Model model, double[] priori, int p, double sigma, IntSet labels); double[] priori(); @Override void update(int[] x, int y); void update(SparseArray x, int y); @Override void update(int[][] x, int[] y); void update(SparseArray[] x, int[] y); @Override int predict(int[] x); @Override int predict(int[] x, double[] posteriori); int predict(SparseArray x); int predict(SparseArray x, double[] posteriori); }
DiscreteNaiveBayes implements OnlineClassifier<int[]>, SoftClassifier<int[]> { @Override public void update(int[] x, int y) { if (!isGoodInstance(x)) { logger.info("Skip updating the model with a sample without any feature word"); return; } if (model == Model.TWCNB) { throw new UnsupportedOperationException("TWCNB supports only batch learning"); } y = labels.indexOf(y); switch (model) { case MULTINOMIAL: case CNB: case WCNB: case TWCNB: for (int i = 0; i < p; i++) { ntc[y][i] += x[i]; nt[y] += x[i]; } break; case POLYAURN: for (int i = 0; i < p; i++) { ntc[y][i] += x[i] * 2; nt[y] += x[i] * 2; } break; case BERNOULLI: for (int i = 0; i < p; i++) { if (x[i] > 0) { ntc[y][i]++; } } break; default: throw new IllegalStateException("Unknown model: " + model); } n++; nc[y]++; update(); } DiscreteNaiveBayes(Model model, int k, int p);  DiscreteNaiveBayes(Model model, int k, int p, double sigma, IntSet labels);  DiscreteNaiveBayes(Model model, double[] priori, int p);  DiscreteNaiveBayes(Model model, double[] priori, int p, double sigma, IntSet labels); double[] priori(); @Override void update(int[] x, int y); void update(SparseArray x, int y); @Override void update(int[][] x, int[] y); void update(SparseArray[] x, int[] y); @Override int predict(int[] x); @Override int predict(int[] x, double[] posteriori); int predict(SparseArray x); int predict(SparseArray x, double[] posteriori);  }