IniziaInizia gratis

Seeing how the neighbors voted

When multiple nearest neighbors hold a vote, it can sometimes be useful to examine whether the voters were unanimous or widely separated.

For example, knowing more about the voters' confidence in the classification could allow an autonomous vehicle to use caution in the case there is any chance at all that a stop sign is ahead.

In this exercise, you will learn how to obtain the voting results from the knn() function.

The class package has already been loaded in your workspace along with the datasets signs, sign_types, and signs_test.

Questo esercizio fa parte del corso

Supervised Learning in R: Classification

Visualizza il corso

Istruzioni dell'esercizio

  • Build a kNN model with the prob = TRUE parameter to compute the vote proportions. Set k = 7.
  • Use the attr() function to obtain the vote proportions for the predicted class. These are stored in the attribute "prob".
  • Examine the first several vote outcomes and percentages using the head() function to see how the confidence varies from sign to sign.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Use the prob parameter to get the proportion of votes for the winning class
sign_pred <- ___

# Get the "prob" attribute from the predicted classes
sign_prob <- attr(___, "___")

# Examine the first several predictions
head(___)

# Examine the proportion of votes for the winning class
head(___)
Modifica ed esegui il codice