class numpy_ml.nonparametric.KNN(k=5, leaf_size=40, classifier=True, metric=None, weights='uniform')[source]

A k-nearest neighbors (kNN) model relying on a ball tree for efficient computation.

  • k (int) – The number of neighbors to use during prediction. Default is 5.
  • leaf_size (int) – The maximum number of datapoints at each leaf in the ball tree. Default is 40.
  • classifier (bool) – Whether to treat the values in Y as class labels (classifier = True) or real-valued targets (classifier = False). Default is True.
  • metric (Distance metric or None) – The distance metric to use for computing nearest neighbors. If None, use the euclidean() metric by default. Default is None.
  • weights ({'uniform', 'distance'}) – How to weight the predictions from each neighbors. ‘uniform’ assigns uniform weights to each neighbor, while ‘distance’ assigns weights proportional to the inverse of the distance from the query point. Default is ‘uniform’.
fit(X, y)[source]

Fit the model to the data and targets in X and y

  • X (numpy array of shape (N, M)) – An array of N examples to generate predictions on.
  • y (numpy array of shape (N, *)) – Targets for the N rows in X.

Generate predictions for the targets associated with the rows in X.

Parameters:X (numpy array of shape (N’, M’)) – An array of N’ examples to generate predictions on.
Returns:y (numpy array of shape (N’, *)) – Predicted targets for the N’ rows in X.