MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Supervised Learning in R: Classification

Lihat Kursus

Petunjuk latihan

  • Compute the accuracy of the default k = 1 model using the given code, then find the accuracy of the model using mean() to compare signs_actual and the model's predictions.
  • Modify the knn() function call by setting k = 7 and again find accuracy value.
  • Revise the code once more by setting k = 15, plus find the accuracy value one more time.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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(___)
Edit dan Jalankan Kode