MATLAB File Help: cv.KNearest Index
cv.KNearest

The class implements K-Nearest Neighbors model

K-Nearest Neighbors

The algorithm caches all training samples and predicts the response for a new sample by analyzing a certain number (K) of the nearest neighbors of the sample using voting, calculating weighted sum, and so on. The method is sometimes referred to as "learning by example" because for prediction it looks for the feature vector with a known response that is closest to the given vector.

Example

Xtrain = [randn(20,4)+1; randn(20,4)-1];    % training samples
Ytrain = int32([ones(20,1); zeros(20,1)]);  % training labels
knn = cv.KNearest(Xtrain, Ytrain);
Xtest = randn(50,4);                        % testing samples
Ytest = knn.predict(Xtest);                 % predictions

References

[BEIS97]:

J.S. Beis and D.G. Lowe. "Shape indexing using approximate nearest-neighbor search in highdimensional spaces". In Proc. IEEE Conf. Comp. Vision Patt. Recog., pp 1000-1006, 1997. http://citeseer.ist.psu.edu/beis97shape.html

See also
Class Details
Superclasses handle
Sealed false
Construct on load false
Constructor Summary
KNearest Creates/trains a K-Nearest Neighbors model 
Property Summary
AlgorithmType Algorithm type. 
DefaultK Default number of neighbors to use in predict method. 
Emax Parameter for 'KDTree' implementation. 
IsClassifier Whether classification or regression model should be trained. 
id Object ID 
Method Summary
  addlistener Add listener for event. 
  calcError Computes error on the training or test dataset 
  clear Clears the algorithm state 
  delete Destructor 
  empty Returns true if the algorithm is empty 
  eq == (EQ) Test handle equality. 
  findNearest Finds the neighbors and predicts responses for input vectors 
  findobj Find objects matching specified conditions. 
  findprop Find property of MATLAB handle object. 
  ge >= (GE) Greater than or equal relation for handles. 
  getDefaultName Returns the algorithm string identifier 
  getVarCount Returns the number of variables in training samples 
  gt > (GT) Greater than relation for handles. 
  isClassifier Returns true if the model is a classifier 
  isTrained Returns true if the model is trained 
Sealed   isvalid Test handle validity. 
  le <= (LE) Less than or equal relation for handles. 
  load Loads algorithm from a file or a string 
  lt < (LT) Less than relation for handles. 
  ne ~= (NE) Not equal relation for handles. 
  notify Notify listeners of event. 
  predict Predicts response(s) for the provided sample(s) 
  save Saves the algorithm parameters to a file or a string 
  train Trains the model