Trains the K Nearest Neighbors Classifier from the training dataset and computes the required parameters to be used by knnClassifierPredict method for making predictions.
Attention: Available only with Twin Activate commercial edition.
Syntax
parameters = knnclassifierfit(X,y)
parameters = knnclassifierfit(X,y,options)
Inputs
- X
- Training data.
- Type: double
- Dimension: vector | matrix
- y
- Target values.
- Type: double
- Dimension: vector | matrix
- options
- Type: struct
-
- n_neighbors
- Number of neighbors to use while making prediction (default: 5).
- Type: integer
- Dimension: scalar
- weights
- Weight function used in prediction. 'uniform': all neighbors are weighted equally (default); 'distance': all neighbors are weighted by the inverse of their distance between query point and them. Greater the distance, lesser the weight for the neighbour.
- Type: char
- Dimension: string
- algorithm
- Algorithm to compute the nearest neighbors. 'auto': decides the most appropriate algorithm based on the input (default), 'ball_tree': uses Ball Tree to search for neighbors; 'kd_tree': uses KD Tree to search for neighbors; 'brute': uses Brute Force search.
- Type: char
- Dimension: string
- leaf_size
- Leaf sized passed to Ball Tree or KD Tree if they’re chosen as the algorithm (default: 30).
- Type: integer
- Dimension: scalar
- p
- p represents Lp norm in minkowski distance. Default: 2 (represents L2 norm which is Euclidean distance)
- Type: integer
- Dimension: scalar
- metric
- Distance metric to compute distance between data points. 'chebyshev', 'cityblock', 'euclidean', 'infinity', 'l1', 'l2', 'manhattan', 'minkowski' (default with p = 2)
- Type: char
- Dimension: string
Outputs
- parameters
- Contains all the values passed to knnclassifierfit method as options. Additionally it has below key-value pairs.
- Type: struct
-
- scorer
- Function handle pointing to 'accuracy' function.
- Type: function handle
- n_samples
- Number of rows in the training data.
- Type: integer
- Dimension: scalar
- n_features
- Number of columns in the training data.
- Type: integer
- Dimension: scalar
Example
Usage of knnclassifierfit
data = dlmread('digits.csv', ',');
X = data(:, 1:end-1);
y = data(:, end);
parameters = knnclassifierfit(X, y);
> parameters
parameters = struct [
algorithm: auto
leaf_size: 30
metric: minkowski
n_features: 64
n_neighbors: 5
n_samples: 1797
p: 2
weights: uniform
]
Comments
Output 'parameters' should be passed as input to knnclassifierpredict function.