Kom igångKom igång 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.

Den här övningen är en del av kursen

Supervised Learning in R: Classification

Visa kurs

Övningsinstruktioner

  • 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.

Interaktiv övning med praktiskt arbete

Testa den här övningen genom att slutföra den här exempelkoden.

# 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(___)
Redigera och kör kod