Rpart模型每次运行都会获得不同的结果

时间:2016-04-09 23:23:38

标签: r decision-tree rpart

我使用相同的训练和测试集,但由于某种原因,每次运行时混淆矩阵和输出图都不同。每次迭代都实现两种精度:

fit_rpart <- train(goodbad~.,method='rpart',data=training, control = rpart.control(maxdepth = 30, minsplit=30, minbucket=1, cp=0.001))


fancyRpartPlot(fit_rpart$finalModel)

pred_rpart <- predict(fit_rpart, testing)
confusionMatrix(pred_rpart, testing$goodbad, positive = 'bad')

1 个答案:

答案 0 :(得分:1)

rpart使用随机抽样。在每次运行之前使用set.seed,每次都应该获得相同的模型。

set.seed(100)
fit_rpart <- train(goodbad~.,method='rpart',data=training, control = rpart.control(maxdepth = 30, minsplit=30, minbucket=1, cp=0.001))


fancyRpartPlot(fit_rpart$finalModel)

pred_rpart <- predict(fit_rpart, testing)
confusionMatrix(pred_rpart, testing$goodbad, positive = 'bad')