A user is trying to create a knn classification model using the knn method from R class package:

770    Asked by IraJoshi in Data Science , Asked on Dec 20, 2019
Answered by Ira Joshi

impens_test_pred <- knn(train = train_set, test = test_set, cl = train_set_labels$cleavage, k = 5)

impens_test_pred2 <- knn(train = train_set, test = test_set, cl = train_set_labels$cleavage, k = 5)

But when he calls the ggmodels.CrossTable for the models the confusion table is different:

How to fix that?

It can be fixed by setting the seed of R‘s random number generator, which is useful for creating simulations or random objects that can be reproduced.

By using set.seed, we can see the following behavior.

set.seed(123)

sample(1:9, 1) # will always return 3

sample(1:9, 1) # will always return 8

For getting the same output we should set the seed twice.

set.seed(123)

sample(1:9, 1) # returns 3

set.seed(123)

sample(1:9, 1) # returns 3 again

For doing the same in KNN, we need to seed the sample twice to get the same result.



Your Answer

Interviews

Parent Categories