Porting Support Vector Machine Models from R to Another Language – Part 1

R is well known for its machine learning capabilities. With the packages caret and kernlab e.g. you can perform multivariate regression with nonlinear support vector machines (SVM) using Gaussian radial basis kernel. After your model is trained you can predict new data using the S4 method predict(). Thereby, modelling and prediction is conducted on a high programming level on just two code lines. Also the steps of reading and pre-processing calibration data are much shorter and easier than in other languages like C/C++ or Java. On the other hand, R as an interpreter language isn’t that fast and not convenient for embedded systems. But you can take advantage of easy SVM modelling in R and after that export your trained model to another language. So you can train your support vector machine on a fast desktop pc and use it later on an Arduino or Raspberry Pi for example.

Take the following steps:

Step 1: Extract all data and model parameter you need for prediction from the R environment.

Step 2: Implement the SVM prediction algorithm in a programming language of your choice and parameterise it with the data coming from R.

In part 1 of this article you will learn how to finish off step 1. You will learn which data you need for prediction in another environment and how to extract it from kernlab SVM model.

In part 2 of this article you will learn how to finish off step 2. The SVM prediction algorithm and an implementation example will be presented.

Step 1

Load R libraries for machine learning



Perform a support vector machine regression with data saved in the training data frame svmdat.

In that example the data frame contains  one response variable y (target) whereas all other columns are predictors.

Train control is conducted by cross validation with repeated train/test splits (75% of data used for modelling, 25 repeats).

ctrl   <- trainControl(method = "LGOCV", p = .75, savePred="final")
svmmod <- train(y ~ . ,data=svmdat,method="svmRadialSigma",trControl = ctrl)    


Extract your SVM parameters from kernlab model and collect further information you need  to predict new data:

Get sigma parameter of Gaussian radial basis function tuned by cross validation.

sigma <- svmmod$bestTune$sigma

Get resulting alpha vector (Lagrange multipliers) identified by support vector machine.

a <- alpha(svmmod$finalModel)

Get scaled support vectors.

supportV <- xmatrix(object)

Get model offset.

bias <- b(svmmod$finalModel)

Because SVM’s are not scale invariant all data are auto scaled by default before the SVM starts optimisation. Hence you need the centers and scales of calibration data.

ymean  <- attributes(svmmod$finalModel)$scaling$y.scale$`scaled:center`   
yscale <- attributes(svmmod$finalModel)$scaling$y.scale$`scaled:scale`   
xmean  <- attributes(svmmod$finalModel)$scaling$x.scale$`scaled:center`   
xscale <- attributes(svmmod$finalModel)$scaling$x.scale$`scaled:scale`   

Save the predictors/features from training data frame. For testing the algorithm in Part 2, you can use X as new data to predict the response variable and compare it with the training target.

X <- svmdat[,colnames(svmdat)!="y"]

Save all these variables to separate files, a data base, or wathever you can access later from the other programming language in which your prediction algorithm is implemented.

To figure out how to use all this information to predict new data, please read Porting Support Vector Machine Models from R to Another Language – Part 2 (coming soon).

Yours faithfully,

Dennis Vier



One thought on “Porting Support Vector Machine Models from R to Another Language – Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s