Get startedGet started for free

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.

This exercise is part of the course

Supervised Learning in R: Classification

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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(___)
Edit and Run Code