我需要使用RWeka的实现(J48()
)在我的流失数据集上优化C4.5算法的准确性。因此,我使用插入符号包的train()
函数来帮助我确定最佳参数设置(对于 M 和 C )。
我尝试使用J48()
确定的参数手动运行train()
来验证结果。结果令人惊讶,因为手动运行得到了更好的结果。
J48()
?train()
函数提供与手动参数设置相似或更好的结果?library("RWeka", lib.loc="~/R/win-library/3.3")
library("caret", lib.loc="~/R/win-library/3.3")
library("gmodels", lib.loc="~/R/win-library/3.3")
set.seed(7331)
使用来自包装插入符的train()确定使用J48的最佳C4.5模型:
ctrl <- trainControl(method="LGOCV", p=0.8, seeds=NA)
grid <- expand.grid(.M=25*(1:15), .C=c(0.1,0.05,0.025,0.01,0.0075,0.005))
使用完整数据集“response_nochar”训练模型:
rtrain <- train(churn~.,data=response_nochar,method="J48",na.action=na.pass,trControl=ctrl,tuneGrid=grid)
返回rtrain $ finalmodel,预测精度为0.6055(树长3,有2片叶子):
# Accuracy was used to select the optimal model using the largest value.
# The final values used for the model were C = 0.005 and M = 25.
大约有精确到0.6055的50种组合,范围从最终模型的给定值到(M = 325,C = 0.1)(中间有一个例外)。
使用J48手动试用参数值:
# splitting into training and test datasets, deriving from full dataset "response_nochar"
# similar/equal to the above splitting with LGOCV and p=0.8?
response_sample <- sample(10000, 8000)
response_train <- response_nochar[response_sample,]
response_test <- response_nochar[-response_sample,]
# setting parameters
jctrl <- Weka_control(M=25,C=0.005)
计算模型:
c45 <- J48(churn~.,data=response_train,na.action=na.pass,control=jctrl)
使用测试数据集进行预测:
pred_c45 <- predict(c45, newdata=response_test, na.action=na.pass)
模型预测准确度为0.655(树木大小为25,有13片叶子)。
CrossTable(response_test$churn, pred_c45, prop.chisq= FALSE, prop.c= FALSE, prop.r= FALSE, dnn= c('actual churn','predicted churn'))
PS:我使用的数据集包含10000条记录,目标变量的分布为50:50。