Testing other 'k' values
By default, the knn() function in the class package uses only the single nearest neighbor.
Setting a k parameter allows the algorithm to consider additional nearby neighbors. This enlarges the collection of neighbors which will vote on the predicted class.
Compare k values of 1, 7, and 15 to examine the impact on traffic sign classification accuracy.
The class package is already loaded in your workspace along with the datasets signs, signs_test, and sign_types. The object signs_actual holds the true values of the signs.
Questo esercizio fa parte del corso
Supervised Learning in R: Classification
Istruzioni dell'esercizio
- Compute the accuracy of the default
k = 1model using the given code, then find the accuracy of the model usingmean()to comparesigns_actualand the model's predictions. - Modify the
knn()function call by settingk = 7and again find accuracy value. - Revise the code once more by setting
k = 15, plus find the accuracy value one more time.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Compute the accuracy of the baseline model (default k = 1)
k_1 <- knn(train = ___, test = ___, cl = ___)
mean(___)
# Modify the above to set k = 7
k_7 <- ___
mean(___)
# Set k = 15 and compare to the above
k_15 <- ___
mean(___)