副问题[/!--empirenews.page--]
SMOTE - Supersampling Rare Events in R:用R对罕有变乱举办超等采样
在这个例子中将用到以下三个包 {DMwR} - Functions and data for the book “Data Mining with R” and SMOTE algorithm:SMOTE算法 {caret} - modeling wrapper,functions,commands:模子封装、函数、呼吁
{pROC} - Area Under the Curve (AUC) functions:曲线下面积(ACU)函数
SMOTE算法是为了办理不服衡的分类题目。也就是说,它可以发生一个新的“SMOTEd”数据,办理类不服衡题目集。可能,它也可以运行在这个新的数据集的分类算法,并返回所获得的模子。
我们操作 Thyroid Disease 数据来举办研究。 让我们洗濯一些数据 # 加载数据,删除冒号和句号,并追加列名 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",1) # 搜查下阳性和阴性的功效 table(hyper$target) ##? ## ? ?0 ? ?1? ## 3012 ?151 prop.table(table(hyper$target)) ##? ## ? ? ? 0 ? ? ? 1? ## 0.95226 0.04774 # 可见,1仅有5%。这显然是一个扭曲的数据集,也是有数变乱。 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 ? ? # 这数据都是因子型变量(字符型的值),这些都必要转换成二值化的数字,以利便建模: 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(数据支解成果)函数将数据随机分成沟通的两份。
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 两者的分类功效是均衡的,因此如故有5%阁下的代表,我们如故处于精采的程度。
我们操作caret包中的treebag模子算法,对实习集数据成立模子,并对测试集数据举办猜测。
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]) 为了评估模子,我们用pROC包的roc函数算auc得分和绘图 library(pROC) ## Type 'citation("pROC")' for a citation. ##? ## Attaching package: 'pROC' ##? ## 下列工具被屏障了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)

auc得分是0.98,已经长短常不错的功效了(由于它的范畴是在0.5到1之间)。
很难想象SMOTE对此能再有进步了,但接下来我们操作SMOTE对数据处理赏罚后再建模,看看auc功效
在R中,SMOTE算法是DMwR软件包的一部门,首要参数有如下三个:perc.over:过采样时,天生少数类的样本个数;k:过采样中行使K近邻算法天生少数类样本时的K值,默认是5;perc.under:欠采样时,对应每个天生的少数类样本,选择原始数据大都类样本的个数。譬喻,perc.over=500暗示对原始数据齐集的每个少数样本,都将天生5个新的少数样本;perc.under=80暗示从原始数据齐集选择的大都类的样本是新生的数据齐集少数样本的80%。
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|