在 R 中使用 randomForest 包进行预测 [英] Predict using randomForest package in R
本文介绍了在 R 中使用 randomForest 包进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 R 中 randomForrest 调用的结果来预测某些未标记数据(例如要分类的真实世界输入)的标签?
代码:
How can I use result of randomForrest call in R to predict labels on some unlabled data (e.g. real world input to be classified)?
Code:
train_data = read.csv("train.csv")
input_data = read.csv("input.csv")
result_forest = randomForest(Label ~ ., data=train_data)
labeled_input = result_forest.predict(input_data) # I need something like this
train.csv:
a;b;c;label;
1;1;1;a;
2;2;2;b;
1;2;1;c;
输入.csv:
a;b;c;
1;1;1;
2;1;2;
我需要得到这样的东西
a;b;c;label;
1;1;1;a;
2;1;2;b;
推荐答案
让我知道您是否遇到了这种情况.
Let me know if this is what you are getting at.
您使用训练数据训练随机森林:
You train your randomforest with your training data:
# Training dataset
train_data <- read.csv("train.csv")
#Train randomForest
forest_model <- randomForest(label ~ ., data=train_data)
现在随机森林已经过训练,您想为其提供新数据,以便它可以预测标签是什么.
Now that the randomforest is trained, you want to give it new data so it can predict what the labels are.
input_data$predictedlabel <- predict(forest_model, newdata=input_data)
上面的代码在 input_data 中添加了一个新列,显示了预测的标签.
The above code adds a new column to your input_data showing the predicted label.
这篇关于在 R 中使用 randomForest 包进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文