開始使用免費開始

看看鄰居是怎麼投票的

當多個最近鄰居進行投票時,觀察投票是否一致或分歧,有時很有幫助。

例如,若能掌握投票者對分類的信心,無人車在前方「有任何可能」出現停車標誌時,就能更謹慎地應對。

在這個練習中,你會學到如何從 knn() 函式取得投票結果。

你的工作空間已經載入 class 套件,以及 signssign_typessigns_test 資料集。

本練習屬於課程

R 監督式學習:分類

檢視課程

練習說明

  • 建立一個 kNN 模型,加入 prob = TRUE 參數以計算投票比例,並設定 k = 7
  • 使用 attr() 函式取得對預測類別的投票比例。這些比例儲存在屬性 "prob" 中。
  • 使用 head() 函式檢視前幾個投票結果與百分比,看看各個標誌之間的信心度如何變化。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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(___)
編輯並執行程式碼