Learn R Programming

e1071 (version 1.3-11)

predict.svm: Predict method for Support Vector Machines

Description

This function predicts values based upon a model trained by svm.

Usage

## S3 method for class 'svm':
predict(object, newdata, ...)

Arguments

object
object of class "svm", created by svm.
newdata
a matrix containing the new input data.
...
currently not used.

Value

  • The predicted value (for classification: the label, for density estimation: TRUE or FALSE).

References

  • Chang, Chih-Chung and Lin, Chih-Jen: LIBSVM 2.0: Solving Different Support Vector Formulations. http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm2.ps.gz
  • Chang, Chih-Chung and Lin, Chih-Jen: Libsvm: Introduction and Benchmarks http://www.csie.ntu.edu.tw/~cjlin/papers/q2.ps.gz

See Also

svm

Examples

Run this code
data(iris)
attach(iris)

## classification mode
# default with factor response:
model <- svm (Species~., data=iris)

# alternatively the traditional interface:
x <- subset (iris, select = -Species)
y <- Species
model <- svm (x, y) 

print (model)
summary (model)

# test with train data
pred <- predict (model, x)
# (same as:)
pred <- predict (model)

# Check accuracy:
table (pred,y)

## try regression mode on two dimensions

# create data
x <- seq (0.1,5,by=0.05)
y <- log(x) + rnorm (x, sd=0.2)

# estimate model and predict input values
m   <- svm (x,y)
new <- predict (m,x)

# visualize
plot   (x,y)
points (x, log(x), col=2)
points (x, new, col=4)

## density-estimation

# create 2-dim. normal with rho=0:
X <- data.frame (a=rnorm (1000), b=rnorm (1000))
attach (X)

# traditional way:
m <- svm (X, gamma=0.1)

# formula interface:
m <- svm (~., data=X, gamma=0.1)
# or:
m <- svm (~a+b, gamma=0.1)

# test:
newdata <- data.frame(a=c(0,4), b=c(0,4))
predict (m, newdata)

Run the code above in your browser using DataLab