predict method

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

Implementation of sklearn.neghbors.KNeighborsClassifier.predict.

Implementation

@override
int predict(List<double> X) {
  if (nNeighbors == 1) {
    int idx = 0;
    double minDist = INFINITY;
    double curDist;
    for (int i = 0; i < fitY.length; i++) {
      curDist = cmp(fitX[i], X, p);
      if (curDist <= minDist) {
        minDist = curDist;
        idx = fitY[i];
      }
    }
    return classes[idx];
  }
  var compute = List<int>.filled(classes.length, 0);
  var dists = [];
  fitY.asMap().forEach((i, v) => dists.add(Neighbor(v, cmp(fitX[i], X, p))));
  dists.sort((a, b) => a.dist.compareTo(b.dist));
  classes.asMap().forEach((i, v) => compute[dists[i].cls]++);

  return classes[argmax(compute)];
}