predict method

  1. @override
int predict(
  1. List<double> X
)
override

Implementation of sklearn.nayve_bayes.BernoulliNB.predict.

Implementation

@override
int predict(List<double> X) {
  int nClasses = classes.length;
  int nFeatures = delProbes.length;

  var jll = List<double>.generate(nClasses, (index) => index.toDouble());
  for (int i = 0; i < nClasses; i++) {
    double sum = 0.0;
    for (int j = 0; j < nFeatures; j++) {
      sum += X[i] * delProbes[j][i];
    }
    jll[i] = sum;
  }
  for (int i = 0; i < nClasses; i++) {
    double sum = 0.0;
    for (int j = 0; j < nFeatures; j++) {
      sum += negProbs[i][j];
    }
    jll[i] += priors[i] + sum;
  }
  var idx = 0;

  classes.asMap().forEach((i, v) => idx = jll[i] > jll[idx] ? i : idx);
  return classes[idx];
}