Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:41:44+00:00 2026-06-17T10:41:44+00:00

I want to train an SVM classifier in R and be able to use

  • 0

I want to train an SVM classifier in R and be able to use it in other software by exporting the relevant parameters. To do so, I first want to be able to reproduce the behavior of predict.svm() in R (using the e1071 package).

I trained the model based on the iris data.

data(iris)

# simplify the data by removing the third label
ir <- iris[1:100,]
ir$Species <- as.factor(as.integer(ir$Species))

# train the model
m <- svm(Species ~ ., data=ir, cost=8)

# the model internally uses a scaled version of the data, example:
m$x.scale
# # # # # 
# $`scaled:center`
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#        5.471        3.099        2.861        0.786 
#
# $`scaled:scale`
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#    0.6416983    0.4787389    1.4495485    0.5651531 
# # # # #

# because the model uses scaled data, make a scaled data frame
irs<-ir;
sc<-data.frame(m$x.scale);
for(col in row.names(sc)){
      irs[[col]]<-(ir[[col]]-sc[[col,1]])/sc[[col,2]]
}

# a radial kernel function
k<-function(x,x1,gamma){
    return(exp(-gamma*sum((x-x1)^2)))
}

According to Hastie, Tibshirani, Friedman (2001), equation 12.24, the prediction function of x can be written as the sum over the support vectors of the coefficient times the kernel function of the SV and x, which corresponds to a matrix product, plus the intercept.

$\hat{f}(x)= \sum^N_{i=1} \hat{\alpha}_i y_i K(x,x_i)+\hat{\beta}_0 $, where $y_i$ is already contained in m$coefs.

# m$coefs contains the coefficients of the support vectors, m$SV 
# the support vectors, and m$rho the *negative* intercept
f<-function(x,m){
    return(t(m$coefs) %*% as.matrix(apply(m$SV,1,k,x,m$gamma)) - m$rho)
}

# a prediction function based on the sign of the prediction function
my.predict<-function(m,x){
    apply(x,1,function(y) sign(f(y,m)))
}

# applying my prediction function to the scaled data frame should
# yield the same result as applying predict.svm() to the original data
# example, thus the table should show one-to-one correspondence:
table(my.predict(m,irs[,1:4]),predict(m,ir[,1:4]))

# the unexpected result:    
# # # # #
#      1  2
#  -1  4 24
#  1  46 26
# # # # #

Who can explain where this is going wrong?

Edit: there was a minor error in my code, it now gives the following, expected result:

      1  2
  -1  0 50
  1  50  0

I hope to be of help to anyone facing the same problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:41:46+00:00Added an answer on June 17, 2026 at 10:41 am

    There was a minor error in one of my functions. The edited version works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the out-of-fold predictions from a caret model to train a
I've just used Weka to train my SVM classifier under Classify tag. Now I
Want to be able to provide a search interface for a collection of objects
I have a binary application on windows (train timetable software) which contains a lot
I am trying to use tesseract-2.04 in my iPhone application and just want to
I have some data in csv format I want to use for predictive modeling.
I have an m x n matrix and I want to use it in
I have a database for a Train company. I want to combine two queries
I want to train a MultiLayerPerceptron using Weka with ~200 samples and 6 attributes.
I want to make a POST request to get schedule of a train number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.