Why should we use set.seed() before apply knn() in R?

Viewed 1026

When I read An Introduction To Statistical Learning, I am puzzled by the following passage:

We set a random seed before we apply knn() because if several observations are tied as nearest neighbors, then R will randomly break the tie. Therefore, a seed must be set in order to ensure reproducibility of results.

Could anyone please tell me why is the result of KNN random?

1 Answers

The reason behind that if we use set.seed() before knn() in R then it helps to select only one random number because if we run knn() then random numbers are generated but if we want that the numbers do not change then we can use it.

Related