与えられた R コードを使用して、RF モデルを正常に実行できました。それは以下で、私のデータのスニペットも含まれています。
唯一の問題は、コードの記述方法が、確率のベクトルのみを出力し、「testset」と呼ばれる元のテスト データ セットからのデータを出力しないことです。そのため、オンラインで解決策が見つからなかったため、元のデータ フレームと共に確率を出力する方法を見つけようとしています。つまり、FLSAStat 列の直後のように、データ セット内の別の列にしたいということです。これで、すべてをまとめて csv ファイルに出力できるようになります。
ここに私が持っているものがあります:
#####################################################
# 1. SETUP DATA
#####################################################
mydata <- read.csv("train_test.csv", header=TRUE)
colnames(testset)
[1] "train" "Target" "ApptCode" "Directorate" "New_Discipline" "Series" "Adjusted.Age"
[8] "Adj.Service" "Adj.Age.Service" "HiEducLv" "Gender" "RetCd" "FLSAStat"
> head(testset)
train Target ApptCode Directorate New_Discipline Series Adjusted.Age Adj.Service Adj.Age.Service HiEducLv Gender
5909 0 NA IN Business Math Computer Science IT PSTS 54.44 10 64.44 Bachelor Male
5910 0 NA IN Computation Math Computer Science IT PSTS 51.51 15 66.51 Bachelor Male
5911 0 NA IN Physical and Life Sciences Physics PSTS 40.45 5 45.45 PHD Male
5912 0 NA IN Weapons and Complex Integ Physics PSTS 62.21 35 97.21 PHD Male
5913 0 NA IN Weapons and Complex Integ Physics PSTS 45.65 15 60.65 PHD Male
5914 0 NA FX Physical and Life Sciences Physics PSTS 36.13 5 41.12 PHD Male
RetCd FLSAStat
5909 TCP2 E
5910 TCP2 E
5911 TCP2 E
5912 TCP2 E
5913 TCP1 E
5914 TCP2 E
#create train and test sets
trainset = mydata[mydata$train == 1,]
testset = mydata[mydata$train == 0,]
#eliminate unwanted columns from train set
trainset$train = NULL
#####################################################
# 2. set the formula
#####################################################
theTarget <- "Target"
theFormula <- as.formula(paste("as.factor(",theTarget, ") ~ . "))
theFormula1 <- as.formula(paste(theTarget," ~ . "))
trainTarget = trainset[,which(names(trainset)==theTarget)]
testTarget = testset[,which(names(testset)==theTarget)]
#####################################################
# Random Forest
#####################################################
library(randomForest)
what <- "Random Forest"
FOREST_model <- randomForest(theFormula, data=trainset, ntree=500)
train_pred <- predict(FOREST_model, trainset, type="prob")[,2]
test_pred <- predict(FOREST_model, testset, type="prob")[,2]
display_results()
testID <- testset$case_id
predictions <- test_pred
submit_file = cbind(testID,predictions)
write.csv(submit_file, file="RANDOM4.csv", row.names = FALSE)
問題は、予測ベクトルをマージして testSet に戻す追加のコード行が不足していることだと思います。これは、コードの 3 行目から最後の行の前のどこかに行くと思います。