littlely
littlely

Reputation: 1428

using ksvm of kernlab package for predicting has an error

I use ksvm function to train the data, but in predicting I have an error,here is the code:

 svmmodel4 <- ksvm(svm_train[,1]~., data=svm_train,kernel = "rbfdot",C=2.4,
               kpar=list(sigma=.12),cross=5)

Warning message: In .local(x, ...) : Variable(s) `' constant. Cannot scale data.

pred <- predict(svmmodel4, svm_test[,-1])

Error in eval(expr, envir, enclos) : object 'res_var' not found.

If I add the response variable, it works:

pred <- predict(svmmodel4, svm_test)

But if you add the response variable,how can it be "predict"? what is wrong with my code? Thanks for your help!

The complete code:

library(kernlab)
svmData <- read.csv("svmData.csv",header=T,stringsAsFactors = F)
svmData$res_var <- as.factor(svmData$res_var)
svm_train <- svmData1[1:2110,]
svm_test <- svmData1[2111:2814,]
svmmodel4 <- ksvm(svm_train[,1]~.,data = svm_train,kernel = "rbfdot",C=2.4, 
              kpar=list(sigma=.12),cross=5)
pred1 <- predict(svmmodel4,svm_test[,-1])

Upvotes: 1

Views: 1123

Answers (1)

Mohammed
Mohammed

Reputation: 311

You can not remove your response column from your test dataset. You simply divide your data horizontally, meaning the response column must be in your training and testing datasets, or even validation dataset if you have one.

your function

pred <- predict(svmmodel4, svm_test)

is working just fine, the predict function will take your data, knowing your factored column, and test the rest against the model. Your training and testing datasets must have the same number of columns, but the number of rows could be different.

Upvotes: 0

Related Questions