Tune a kNN Model
The Amazon SageMaker knearest neighbors algorithm is a supervised algorithm. The algorithm consumes a test data set and emits a metric about the accuracy for a classification task or about the mean squared error for a regression task. These accuracy metrics compare the model predictions for their respective task to the ground truth provided by the empirical test data. To find the best model that reports the highest accuracy or lowest error on the test dataset, run a hyperparameter tuning job for kNN.
Automatic model tuning, also known as hyperparameter tuning, finds the best version of a model by running many jobs that test a range of hyperparameters on your dataset. You choose the tunable hyperparameters, a range of values for each, and an objective metric. You choose the objective metric appropriate for the prediction task of the algorithm. Automatic model tuning searches the hyperparameters chosen to find the combination of values that result in the model that optimizes the objective metric. The hyperparameters are used only to help estimate model parameters and are not used by the trained model to make predictions.
For more information about model tuning, see Perform Automatic Model Tuning.
Metrics Computed by the kNN Algorithm
The knearest neighbors algorithm computes one of two metrics in the following
table during training depending on the type of task specified by the
predictor_type
hyperparameter.

classifier specifies a classification task and computes
test:accuracy

regressor specifies a regression task and computes
test:mse
.
Choose the predictor_type
value appropriate for the type of task
undertaken to calculate the relevant objective metric when tuning a model.
Metric Name  Description  Optimization Direction 

test:accuracy 
When 
Maximize 
test:mse 
When 
Minimize 
Tunable kNN Hyperparameters
Tune the Amazon SageMaker knearest neighbor model with the following hyperparameters.
Parameter Name  Parameter Type  Recommended Ranges 

k 
IntegerParameterRanges 
MinValue: 1, MaxValue: 1024 
sample_size 
IntegerParameterRanges 
MinValue: 256, MaxValue: 20000000 