開始使用免費開始

避免類別不平衡

有些資料的結果非常不平衡,例如罕見疾病的資料集。若是隨機切分,你可能會得到很不理想的結果。想像一下,所有罕見的觀測都落在測試集,而訓練集中一個都沒有。這會毀掉整個訓練流程!

所幸,initial_split() 可以幫上忙。你將在本練習中觀察並解決這類所謂的「類別不平衡」。

我們已經提供程式碼,建立了 75% 訓練、25% 測試比例的切分物件 diabetes_split

本練習屬於課程

R 的樹狀模型機器學習

檢視課程

動手互動練習

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

# Preparation
set.seed(9888)
diabetes_split <- initial_split(diabetes, prop = 0.75)

# Proportion of 'yes' outcomes in the training data
counts_train <- table(training(___)$outcome)
prop_yes_train <- counts_train["___"] / sum(counts_train)

# Proportion of 'yes' outcomes in the test data
counts_test <- table(___)
prop_yes_test <- ___ / sum(___)

paste("Proportion of positive outcomes in training set:", round(prop_yes_train, 2))
paste("Proportion of positive outcomes in test set:", round(prop_yes_test, 2))
編輯並執行程式碼