我第一次使用R和e1071包和SVM多类!那时我很困惑。目标是:如果我有一个阳光充足的句子;它将被归类为“是”句子;如果我有一个带云的句子,如果我有一个下雨的句子,它将被归类为“可能”; il将被归类为广告“否”。真正的目标是为我的研究做一些文本分类。
我有两个文件:
示例:
V1 V2
1 sunny yes
2 sunny sunny yes
3 sunny rainy sunny yes
4 sunny cloud sunny yes
5 rainy no
6 rainy rainy no
7 rainy sunny rainy no
8 rainy cloud rainy no
9 cloud maybe
10 cloud cloud maybe
11 cloud rainy cloud maybe
12 cloud sunny cloud maybe
示例:
V1
1 sunny
2 rainy
3 hello
4 cloud
5 a
6 b
7 cloud
8 d
9 e
10 f
11 g
12 hello
按照虹膜数据集的示例 (https://cran.r-project.org/web/packages/e1071/e1071.pdf和http://rischanlab.github.io/SVM.html) 我创建了我的模型,然后以这种方式测试训练数据:
> library(e1071)
> train <- read.csv(file="C:/Users/Stef/Desktop/train.csv", sep = ";", header = FALSE)
> test <- read.csv(file="C:/Users/Stef/Desktop/test.csv", sep = ";", header = FALSE)
> attach(train)
> x <- subset(train, select=-V2)
> y <- V2
> model <- svm(V2 ~ ., data = train, probability=TRUE)
> summary(model)
Call:
svm(formula = V2 ~ ., data = train, probability = TRUE)
Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1
gamma: 0.08333333
Number of Support Vectors: 12
( 4 4 4 )
Number of Classes: 3
Levels:
maybe no yes
> pred <- predict(model,x)
> system.time(pred <- predict(model,x))
user system elapsed
0 0 0
> table(pred,y)
y
pred maybe no yes
maybe 4 0 0
no 0 4 0
yes 0 0 4
> pred
1 2 3 4 5 6 7 8 9 10 11 12
yes yes yes yes no no no no maybe maybe maybe maybe
Levels: maybe no yes
我认为直到现在还可以。 现在的问题是:测试数据怎么样?我没有找到任何测试数据。然后,我想也许我应该用测试数据测试模型。我这样做了:
> test
V1
1 sunny
2 rainy
3 hello
4 cloud
5 a
6 b
7 cloud
8 d
9 e
10 f
11 g
12 hello
> z <- subset(test, select=V1)
> pred <-predict(model,z)
Error in predict.svm(model, z) : test data does not match model !
这里有什么问题?能否请您解释一下我如何使用旧火车模型测试新数据? 谢谢
修改
这些是每个文件的前5行.csv
> head(train,5)
V1 V2
1 sunny yes
2 sunny sunny yes
3 sunny rainy sunny yes
4 sunny cloud sunny yes
5 rainy no
> head(test,5)
V1
1 sunny
2 rainy
3 hello
4 cloud
5 a
答案 0 :(得分:1)
列车和测试数据集中的因素在这里有所不同,因此您需要先修复它。
library(e1071)
#sample data
train_data <- data.frame(V1 = c("sunny","sunny sunny","rainy","rainy rainy","cloud","cloud cloud"),
V2= c("yes","yes","no","no","maybe","maybe"))
test_data <- data.frame(V1 = c("sunny","rainy","hello","cloud"))
#fix levels in train_data & test_data dataset before running model
train_data$ind <- "train"
test_data$ind <- "test"
merged_data <- rbind(train_data[,-grep("V2", colnames(train_data))],test_data)
#train data
train <- merged_data[merged_data$ind=="train",]
train$V2 <- train_data$V2
train <- train[,-grep("ind", colnames(train))]
#test data
test <- merged_data[merged_data$ind=="test",]
test <- data.frame(V1 = test[,-grep("ind", colnames(test))])
#svm model
svm_model <- svm(V2 ~ ., data = train, probability=TRUE)
summary(svm_model)
train_pred <- predict(svm_model,train["V1"])
table(train_pred,train$V2)
#prediction on test data
test$test_pred <- predict(svm_model,test)
test
希望这有帮助!
答案 1 :(得分:0)
我认为问题可能出在您对子集函数的select参数上 - 如果您只执行pred<-predict(model,test)
会发生什么?有点难以判断原始数据是有两列(V1,V2)还是最多四列。由于您使用data=train
训练/初始化模型,我认为预测测试而不是子集(测试)应该可以解决问题。
预测将适用于SVM,即使测试集中的行数与SVM训练的行数不同......它应该是微不足道的。类似的东西:
test.preds<-predict(some.svm, test)
misclassification.rate<-mean(test.preds != test$V2)