我正在尝试使用 R 中的 SVM 对一些数据进行分类。
数据集:
D1 | D2 | D3 | word1 | word2 |...
1 | 2 | 3 | 0 | 1 |
3 | 2 | 1 | 1 | 0 |
D1、D2、D3 取值从 0 到 9,每个单词取值 0/1。
首先,我想构建一个分类器,根据 word1、word2 等预测 D1。然后,我想构建一个分类器,根据 D1 和单词中的预测内容来预测 D2。 D1、D2、D3曾经是一个3位数的数字,并且与前一个数字之间存在关系。
到目前为止我已经:
trainD1 <- train[,-1]
trainD1$D2 <- NULL
trainD1$D3 <- NULL
modelD1 <- svm( train$D1~., trainD1, type="C-classification")
但我完全迷失了,欢迎任何帮助。
谢谢
最佳答案
我确信您已经知道这一点,但我只是想确保我涵盖了我的基础 - 如果 D1 和 D2 可以预测 D3,那么使用 D1 和 D3 的实际值而不是预测值总是更好其中。
出于此问题的目的,我假设 D1 和 D2 可能不存在于您的预测数据集中,因此这就是您必须预测它们的原因。从“单词”变量直接预测 D3 可能仍然更准确,但这超出了本问题的范围。
train <- read.csv("trainingSmallExtra.csv")
require(e1071)
d1 <- svm( x = train[,5:100], # arbitrary subset of words
y = train$D1,
gamma = 0.1)
d1.predict <- predict(d1)
train <- cbind(d1.predict, train)
x_names <- c("d1.predict", train[,6:101])
d2 <- svm( x = x_names, # d1 prediction + arbitrary subset of words
y = train$D2,
gamma = 0.1)
d2.predict <- predict(d2)
train <- cbind(d2.predict, train)
x_names <- c("d1.predict", "d2.predict", colnames(train)[25:150])
final <- svm( x = train[,x_names],
y = train$D3,
gamma = 0.1)
summary(final)
Call: svm.default(x = train[, x_names], y = train$D3, gamma = 0.1)
Parameters: SVM-Type: eps-regression SVM-Kernel: radial
cost: 1 gamma: 0.1 epsilon: 0.1
Number of Support Vectors: 932
这只是为了向您展示整个过程。在您的代码中,您将需要使用更多的单词并设置您认为最合适的任何选项。
我建议使用保留样本或交叉验证来进行性能基准测试。将集成模型与尝试通过检查性能基准直接从单词预测 D3 的单个模型进行比较。
关于r - 使用 SVM 在 R 中集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173934/