如何使用 R e1071 SVM 多类测试数据
How to test data with R e1071 SVM multiclass
我第一次使用 R 和 e1071 包和 SVM 多类!那我很困惑。目标是:如果我有一个带有sunny的句子;它将被分类为"yes"句;如果我有一个带云的句子,它会被分类为"maybe",如果我有一个带雨的句子; il 将被分类为广告 "no"。真正的目标是为我的研究做一些文本分类。
我有两个文件:
- train.csv:一个文件里有两个columns/Variables一个是
数据,另一个是标签。
示例:
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
- test.csv: 在这个文件中有新的数据要分类。
示例:
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 and 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 !
这里有什么问题?你能解释一下如何使用旧的火车模型测试新数据吗?
谢谢
编辑
这是每个文件 .csv 的前 5 行
> 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
我认为问题可能出在子集函数的 select 参数上 - 如果您只执行 pred<-predict(model,test)
会发生什么?很难判断您的原始数据是两列(V1、V2)还是最多四列。由于您 trained/initialized 具有 data=train
的模型,我认为预测测试而不是子集(测试,)应该可以解决问题。
即使测试集中的行数与训练 SVM 的行数不同,Predict 也可以在 SVM 上工作……这应该是微不足道的。类似于:
test.preds<-predict(some.svm, test)
misclassification.rate<-mean(test.preds != test$V2)
此处训练数据集和测试数据集的因素不同,因此您需要先修复它。
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
希望对您有所帮助!
我第一次使用 R 和 e1071 包和 SVM 多类!那我很困惑。目标是:如果我有一个带有sunny的句子;它将被分类为"yes"句;如果我有一个带云的句子,它会被分类为"maybe",如果我有一个带雨的句子; il 将被分类为广告 "no"。真正的目标是为我的研究做一些文本分类。
我有两个文件:
- train.csv:一个文件里有两个columns/Variables一个是 数据,另一个是标签。
示例:
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
- test.csv: 在这个文件中有新的数据要分类。
示例:
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 and 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 !
这里有什么问题?你能解释一下如何使用旧的火车模型测试新数据吗? 谢谢
编辑
这是每个文件 .csv 的前 5 行
> 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
我认为问题可能出在子集函数的 select 参数上 - 如果您只执行 pred<-predict(model,test)
会发生什么?很难判断您的原始数据是两列(V1、V2)还是最多四列。由于您 trained/initialized 具有 data=train
的模型,我认为预测测试而不是子集(测试,)应该可以解决问题。
即使测试集中的行数与训练 SVM 的行数不同,Predict 也可以在 SVM 上工作……这应该是微不足道的。类似于:
test.preds<-predict(some.svm, test)
misclassification.rate<-mean(test.preds != test$V2)
此处训练数据集和测试数据集的因素不同,因此您需要先修复它。
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
希望对您有所帮助!