Trains the K Nearest Neighbors Regressor from the training dataset and computes the required parameters to be used by knnregressorpredict method for making predictions.
Attention: Available only with Twin Activate commercial edition.
Syntax
parameters = knnregressorfit(X,y)
parameters = knnregressorfit(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' (default): All neighbors are weighted equally; '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. 'ball_tree': uses Ball Tree to search for neighbors; 'kd_tree': uses KD Tree to search for neighbors; 'brute': uses Brute Force search; 'auto' (default): decides the most appropriate algorithm based on the input.
- Type: char
- Dimension: string
- leaf_size
- Leaf sized passed to Ball Tree or KD Tree if they are chosen as the algorithm (default: 30).
- Type: integer
- Dimension: scalar
- p
- Represents Lp norm in minkowski distance (default: 2, which is the L2 norm which is Euclidean distance).
- Type: double | 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 knnregressorfit method as options. Additionally it has below key-value pairs.
- Type: struct
-
- scorer
- Function handle pointing to r2 function (R2 Coefficient of Determination).
- 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 knnregressorfit with options
X = [0; 1; 2; 3];
y = [0; 0; 1; 1];
options = struct;
options.n_neighbors = 2;
parameters = knnregressorfit(X, y, options)
parameters = struct [
algorithm: auto
leaf_size: 30
metric: minkowski
model_name: model_16353122674771364
n_features: 1
n_neighbors: 2
n_samples: 4
p: 2
weights: uniform
]
Comments
It performs regression based on K Nearest Neighbors algorithm. Once the neighbors are found, target is predicted using aggregation of nearest neighbors. Output 'parameters' can be passed to knnregressorpredict function.