K Nearest Neighbor (KNN)

class mlpy.Knn(k, dist='se')

k-Nearest Neighbor (KNN).

Initialize the Knn class.

Input

  • k - [integer] number of NN
  • dist - [string] adopted distance (‘se’ = SQUARED EUCLIDEAN, ‘e’ = EUCLIDEAN)
compute(x, y)

Store x and y data.

Input

  • x - [2D numpy array float] (#sample x #feature) training data
  • y - [1D numpy array integer] classes
    • -1 or 1 for binary classification
    • 1, ..., nclasses for multiclass classification

Output

  • 1
predict(p)

Predict knn model on a test point(s).

Input

  • p - [1D or 2D numpy array float] test point(s)

Output: the predicted value(s) on success:

  • -1 or 1 for binary classification
  • 1, ..., nclasses for multiclass classification
  • 0 on succes with non unique classification
  • -2 otherwise

Previous topic

Support Vector Machines (SVMs)

Next topic

Fisher Discriminant Analysis (FDA)

This Page