1横辆、數(shù)據(jù)集拆分
- 首先最基本的是將數(shù)據(jù)集分為訓(xùn)練集(Training)與測試集(Test)兩部分。在測試集用于訓(xùn)練茄猫、確定一個最終的模型狈蚤;然后在測試集測試模型對于未知數(shù)據(jù)的評價效果。
1.1 訓(xùn)練集
- 如上所述划纽,在訓(xùn)練集就要確定了最終的模型脆侮,包括參數(shù)優(yōu)化;
- 一般來說原始Train訓(xùn)練集會進(jìn)一步再分為Train訓(xùn)練集與Validation驗證集兩部分勇劣,以評價不同參數(shù)組合的效果靖避,以確定最終的模型。最后放到Test測試集中評價其效果比默。
1.2 測試集
- 一定要注意Test測試集自始至終沒有參與到模型的訓(xùn)練過程幻捏;它的目的只有一個:在確定一個最終模型后,評價其泛化能力退敦。
1.3 拆分方法
- 一般先拆分?jǐn)?shù)據(jù)為(Train + Validation)與Test兩部分粘咖,其比例一般為 8:2蚣抗、7:3侈百、6:4
- 其次進(jìn)一步拆分出Train + Validation的方法:可以按照設(shè)定比例進(jìn)行拆分,也有K折交叉驗證☆翰铡、Bootstrap自助法抽樣钝域。
(1)K折交叉驗證
K折交叉驗證是比較常用的拆分訓(xùn)練集、測試集锭魔,并用于模型訓(xùn)練例证、驗證的方式。具體步驟如下--
- 首先將原始訓(xùn)練集(區(qū)別test測試集數(shù)據(jù))分為K份迷捧;
- 然后選擇其中K-1 份數(shù)據(jù)用于訓(xùn)練织咧,剩余的1份用于評價效果胀葱;
- 重復(fù)K次,保證每份數(shù)據(jù)都作為過訓(xùn)練數(shù)據(jù)與驗證數(shù)據(jù)笙蒙;
- 最后得到K次的模型評價結(jié)果抵屿;取均值作為該模型參數(shù)下的最終評價結(jié)果;
- 從而可以比較不同模型參數(shù)下的評價結(jié)果捅位,進(jìn)行模型優(yōu)化轧葛,確定最終的模型。
(2)Bootstrapping自助法抽樣
自助法抽樣的核心理解就是:有放回的抽樣尿扯。
-
因為有放回,所以可能會抽到重復(fù)值焰雕;據(jù)統(tǒng)計計算會抽到63.21%的數(shù)據(jù)衷笋,而未被抽到過的數(shù)據(jù)(out-of-bag, OOB)則被視為validation驗證集淀散。
再強(qiáng)調(diào)一點就是:是將原始訓(xùn)練集進(jìn)一步拆分為 訓(xùn)練集與測試集。在上述過程中郭膛,都是把測試集放到一邊的,不去管它则剃,直到確定好模型之后才會用到test測試集调煎。
2士袄、數(shù)據(jù)實操
# Ames housing data
ames <- AmesHousing::make_ames()
2.1 原始訓(xùn)練集與測試集的拆分
有多種方式可供選擇
- (1) base R
sample()
set.seed(123) # for reproducibility
index_1 <- sample(1:nrow(ames), round(nrow(ames) * 0.7))
train_1 <- ames[index_1, ]
test_1 <- ames[-index_1, ]
- (2) caret package
library(caret)
set.seed(123) # for reproducibility
index_2 <- createDataPartition(ames$Sale_Price, p = 0.7,
list = FALSE)
train_2 <- ames[index_2, ]
test_2 <- ames[-index_2, ]
- (3) rsample
library(rsample)
set.seed(123) # for reproducibility
split_1 <- initial_split(ames, prop = 0.7)
train_3 <- training(split_1)
test_3 <- testing(split_1)
補(bǔ)充:對于不均衡樣本的拆分方式
對于分類為目的的有監(jiān)督學(xué)習(xí)(例如癌癥惡性、良性預(yù)測)娄柳;當(dāng)收集的樣本分布很不均衡時,在抽樣還有訓(xùn)練過程中都需要多加考慮。
# Job attrition data
library(tidyverse)
library(modeldata)
churn <- attrition %>%
mutate_if(is.ordered, .funs = factor, ordered = FALSE)
#如下未離職員工與離職員工比例約為 84:16
table(churn$Attrition) %>% prop.table()
##
## No Yes
## 0.8387755 0.1612245
- 分層抽樣Stratified sampling
# stratified sampling with the rsample package
set.seed(123)
split_strat <- initial_split(churn, prop = 0.7,
strata = "Attrition")
train_strat <- training(split_strat)
test_strat <- testing(split_strat)
table(train_strat$Attrition) %>% prop.table()
##
## No Yes
## 0.838835 0.161165
table(test_strat$Attrition) %>% prop.table()
##
## No Yes
## 0.8386364 0.1613636
通過抽樣來均衡比例
Down-sampling balances the dataset by reducing the size of the abundant class(es) to match the frequencies in the least prevalent class. This method is used when the quantity of data is sufficient.
On the contrary, up-sampling is used when the quantity of data is insufficient. It tries to balance the dataset by increasing the size of rarer samples. Rather than getting rid of abundant samples, new rare samples are generated by using repetition or bootstrapping
- 另外關(guān)于K-折交叉驗證在后面算法學(xué)習(xí)過程中經(jīng)常用到蕉朵,而Bootstrap抽樣在隨機(jī)森林等算法中也會用到堪伍。
這里簡單演示通過rsample
包的實現(xiàn)方式:
# K-折交叉驗證
rsample::vfold_cv(ames, v = 10)
# Bootstrap
rsample::bootstraps(ames, times = 10)