我编写了这段R代码来重现问题。这里,我创建了一个唯一的”ID”列,但我不知道如何将预测列添加回测试数据集,并映射到它们各自的ID。请指导我正确的方法。
#Codelibrary(C50)data(churn)data=rbind(churnTest,churnTrain)data$ID<-seq.int(nrow(data)) #adding unique id columnrm(churnTrain)rm(churnTest)set.seed(1223)ind <- sample(2,nrow(data),replace = TRUE, prob = c(0.7,0.3))train <- data[ind==1,1:21]test <- data[ind==2, 1:21]xtrain <- train[,-20]ytrain <- train$churnxtest <- test[,-20]ytest<- test$churnx <- cbind(xtrain,ytrain)## C50 Modelc50Model <- C5.0(churn ~ state + account_length + area_code + international_plan + voice_mail_plan + number_vmail_messages + total_day_minutes + total_day_calls + total_day_charge + total_eve_minutes + total_eve_calls + total_eve_charge + total_night_minutes + total_night_calls + total_night_charge + total_intl_minutes + total_intl_calls + total_intl_charge + number_customer_service_calls,data=train, trials=10)# Evaluate Modelc50Result <- predict(c50Model, xtest)table(c50Result, ytest) #adding prediction to test datatestnew = cbind(xtest,c50Result)#OR predict directlyxtest$churn = predict(c50Model, xtest)
回答:
我会使用match(dataID, predictedID)来匹配数据集中的ID列。
回复你的评论:如果你想将预测值添加到原始数据框中,这两种合并数据和预测的方法都是正确的,并且产生相同的结果。唯一需要注意的是,我会使用
xtest$churn_hut <- predict(c50Model, xtest)
而不是
xtest$churn <- predict(c50Model, xtest)
因为在这里你用模型预测的值替换了原始的churn(如data$churn),这样你就无法比较这两个值了。