随机森林变量选择

我有一个基于100个不同变量构建的随机森林。我想选择“最重要”的变量来构建我的随机森林,以尝试提高性能,但我不知道从哪里开始,除了从rf$importance获取重要性之外。

我的数据仅包含已经标准化的数值变量。

以下是我的随机森林代码:

rf.2 = randomForest(x~., data=train,importance=TRUE, ntree=1501)#trainrf_prob_train = data.frame(predict(rf.2, newdata=train, type="prob"))rf_prob_train <-data.frame(rf_prob_train$X0)val_rf_train<-cbind(rf_prob_train,train$x)names(val_rf_train)<-c("Probs","x")##Run accuracy ratiox<-data.frame(rcorr.cens(-val_rf_train$Probs, val_rf_train$x))rf_train_AR<-x[2,1]rf_train_AR#testrf_prob_test = data.frame(predict(rf.2, test, type="prob"))rf_prob_test <-data.frame(rf_prob_test$X0)val_rf_test<-cbind(rf_prob_test,test$x)names(val_rf_test)<-c("Probs","x")##Run accuracy ratiox<-data.frame(rcorr.cens(-val_rf_test$Probs, val_rf_test$x))rf_test_AR<-x[2,1]rf_test_AR

回答:

今天很忙,所以没能早点给你这个。这给出了使用通用数据集的一般思路。

library(randomForest)library(datasets)head(iris)#To make our formula for RF easier to manipulatevar.predict<-paste(names(iris)[-5],collapse="+")rf.form <- as.formula(paste(names(iris)[5], var.predict, sep = " ~ "))print(rf.form)#This is our current itteration of the formula we're using in RFiris.rf<-randomForest(rf.form,data=iris,importance=TRUE,ntree=100)varImpPlot(iris.rf)#Examine our Variable importance plotto.remove<-c(which(data.frame(iris.rf$importance)$MeanDecreaseAccuracy==min(data.frame(iris.rf$importance)$MeanDecreaseAccuracy)))#Remove the variable with the lowest decrease in Accuracy (Least relevant variable)#Rinse, wash hands, repeatvar.predict<-paste(names(iris)[-c(5,to.remove)],collapse="+")rf.form <- as.formula(paste(names(iris)[5], var.predict, sep = " ~ "))iris.rf<-randomForest(rf.form,data=iris,importance=TRUE,ntree=100)varImpPlot(iris.rf)#Examine our Variable importance plotto.remove<-c(to.remove, which(data.frame(iris.rf$importance)$MeanDecreaseAccuracy==min(data.frame(iris.rf$importance)$MeanDecreaseAccuracy)))#And so on...

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注