SMOTE - Supersampling Rare Events in R:用R對(duì)稀有事件進(jìn)行超級(jí)采樣
在這個(gè)例子中將用到以下三個(gè)包
{DMwR} - Functions and data for the book “Data Mining with R” and SMOTE algorithm:SMOTE算法
{caret} - modeling wrapper, functions, commands:模型封裝、函數(shù)、命令
為福安等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及福安網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、福安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
{pROC} - Area Under the Curve (AUC) functions:曲線下面積(ACU)函數(shù)
SMOTE算法是為了解決不平衡的分類問題。也就是說,它可以產(chǎn)生一個(gè)新的“SMOTEd”數(shù)據(jù),解決類不平衡問題集。或者,它也可以運(yùn)行在這個(gè)新的數(shù)據(jù)集的分類算法,并返回所得到的模型。
我們利用 Thyroid Disease 數(shù)據(jù)來進(jìn)行研究。
讓我們清洗一些數(shù)據(jù)
# 加載數(shù)據(jù),刪除冒號(hào)和句號(hào),并追加列名
hyper <-read.csv('http://archive.ics.uci.edu/ml/machine-learning-databases/thyroid-disease/hypothyroid.data', header=F)
names <- read.csv('http://archive.ics.uci.edu/ml/machine-learning-databases/thyroid-disease/hypothyroid.names', header=F, sep='\t')`1`
names <- gsub(pattern =":|[.]", replacement="", x = names)
colnames(hyper)<-names
# 我們將第一列的列名從 hypothyroid, negative改成target,并將negative變成0,其他值變成1.
colnames(hyper)[1]<-"target"
colnames(hyper)
## [1] "target" "age"
## [3] "sex" "on_thyroxine"
## [5] "query_on_thyroxine" "on_antithyroid_medication"
## [7] "thyroid_surgery" "query_hypothyroid"
## [9] "query_hyperthyroid" "pregnant"
## [11] "sick" "tumor"
## [13] "lithium" "goitre"
## [15] "TSH_measured" "TSH"
## [17] "T3_measured" "T3"
## [19] "TT4_measured" "TT4"
## [21] "T4U_measured" "T4U"
## [23] "FTI_measured" "FTI"
## [25] "TBG_measured" "TBG"
hyper$target<-ifelse(hyper$target=="negative",0,1)
# 檢查下陽性和陰性的結(jié)果
table(hyper$target)
##
## 0 1
## 3012 151
prop.table(table(hyper$target))
##
## 0 1
## 0.95226 0.04774
# 可見,1僅有5%。這顯然是一個(gè)扭曲的數(shù)據(jù)集,也是罕見事件。
head(hyper,2)
## target age sex on_thyroxine query_on_thyroxine on_antithyroid_medication
## 1 1 72 M f f f
## 2 1 15 F t f f
## thyroid_surgery query_hypothyroid query_hyperthyroid pregnant sick tumor
## 1 f f f f f f
## 2 f f f f f f
## lithium goitre TSH_measured TSH T3_measured T3 TT4_measured TT4
## 1 f f y 30 y 0.60 y 15
## 2 f f y 145 y 1.70 y 19
## T4U_measured T4U FTI_measured FTI TBG_measured TBG
## 1 y 1.48 y 10 n ?
## 2 y 1.13 y 17 n ?
# 這數(shù)據(jù)都是因子型變量(字符型的值),這些都需要轉(zhuǎn)換成二值化的數(shù)字,以方便建模:
ind<-sapply(hyper,is.factor)
hyper[ind]<-lapply(hyper[ind],as.character)
hyper[hyper=="?"]=NA
hyper[hyper=="f"]=0
hyper[hyper=="t"]=1
hyper[hyper=="n"]=0
hyper[hyper=="y"]=1
hyper[hyper=="M"]=0
hyper[hyper=="F"]=1
hyper[ind]<-lapply(hyper[ind],as.numeric)
replaceNAWithMean<-function(x) {replace(x,is.na(x),mean(x[!is.na(x)]))}
hyper<-replaceNAWithMean(hyper)
模型研究
我們利用caret包中的createDataPartition(數(shù)據(jù)分割功能)函數(shù)將數(shù)據(jù)隨機(jī)分成相同的兩份。
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
set.seed(1234)
splitIndex<-createDataPartition(hyper$target,time=1,p=0.5,list=FALSE)
trainSplit<-hyper[splitIndex,]
testSplit<-hyper[-splitIndex,]
prop.table(table(trainSplit$target))
##
## 0 1
## 0.95006 0.04994
prop.table(table(testSplit$target))
##
## 0 1
## 0.95446 0.04554
兩者的分類結(jié)果是平衡的,因此仍然有5%左右的代表,我們?nèi)匀惶幱诹己玫乃健?br />
我們利用caret包中的treebag模型算法,對(duì)訓(xùn)練集數(shù)據(jù)建立模型,并對(duì)測試集數(shù)據(jù)進(jìn)行預(yù)測。
ctrl<-trainControl(method="cv",number=5)
tbmodel<-train(target~.,data=trainSplit,method="treebag",
trControl=ctrl)
## Loading required package: ipred
## Loading required package: plyr
predictors<-names(trainSplit)[names(trainSplit)!='target']
pred<-predict(tbmodel$finalModel,testSplit[,predictors])
為了評(píng)估模型,我們用pROC包的roc函數(shù)算auc得分和畫圖
library(pROC)
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
##
## 下列對(duì)象被屏蔽了from 'package:stats':
##
## cov, smooth, var
auc<-roc(testSplit$target,pred)
print(auc)
##
## Call:
## roc.default(response = testSplit$target, predictor = pred)
##
## Data: pred in 1509 controls (testSplit$target 0) < 72 cases (testSplit$target 1).
## Area under the curve: 0.985
plot(auc,ylim=c(0,1),print.thres=TRUE,main=paste('AUC',round(auc$auc`1`,2)))
##
## Call:
## roc.default(response = testSplit$target, predictor = pred)
##
## Data: pred in 1509 controls (testSplit$target 0) < 72 cases (testSplit$target 1).
## Area under the curve: 0.985
abline(h=1,col="blue",lwd=2)
abline(h=0,col="red",lwd=2)
文章題目:R語言:SMOTE-SupersamplingRareEventsinR:用R對(duì)非平衡數(shù)據(jù)的處理方法
網(wǎng)站鏈接:http://aaarwkj.com/article10/peggdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、搜索引擎優(yōu)化、網(wǎng)站收錄、小程序開發(fā)、營銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)