knn#

swordfish.function.knn()#

Implement the k-nearest neighbors (k-NN) algorithm with a brute-force search for classification and regression.

Parameters:
  • Y (Constant) – A vector with the same length as the number of rows of X. Each element is a label corresponding to each row in X.

  • X (Constant) – A table. Each row is an observation and each column is a feature.

  • type (Constant) – A string. It can be either “regressor” or “classifier”.

  • nNeighbor (Constant) – A positive integer indicating the number of nearest neighbors in training.

  • power (Constant) – A positive integer indicating the parameter of Minkowski distance used in training. The default value is 2 indicating Euclidean distance. If power=1, it means Manhattan distance is used in training.

Returns:

A dictionary with the following keys:

  • nNeighbor: the number of nearest neighbors in training.

  • modelName: string “KNN”.

  • model: the model to be saved.

  • power: the parameter of Minkowski distance used in training.

  • type: “regressor” or “classifier”.

Return type:

Constant