# How can I know number of support vectors in SVM [closed]

I want to compare number of support vectors in different SVM model. I have data for training/testing. I wan't to see how different are the numbers of support vectors in case of One vs One and One vs All methods. How can I know it?

• How should I calculate it? In what sense? Like, you calculate the number of Support Vectors in both regimes and compare it. Without further clarifications this sounds unclear. – Firebug May 11 '17 at 20:42
• The time spent on one vs one is much less than one vs all both for training and testing of one feature. I want to see if the SV number is influencing it. Need exact numbers – maximus May 12 '17 at 4:12

One important concept in SVM is $\alpha$, (see this answer for details), the lagrange multipliers. For each data point $i$, there is associated $\alpha_i$. Most $\alpha_i$ will close to $0$, for non-zero ones, it is a support vector. Counting non-zero $\alpha$ is the way to go. Different software will have different implementations.

Here is a reproducible example in R.

library(mlbench)
library(kernlab)
set.seed(1)
d=mlbench.2dnormals(100,sd=0.5)

svp <- ksvm(d$x,d$classes,type="C-svc",kernel="polydot",
prob.model=TRUE,kpar=list(degree=1, scale=1, offset=0))

plot(svp,data=x)


Here is the output for svp, and length(svp@alpha[[1]]). Note there are $6$ support vectors in this case (as plotted in the figure, $6$ solid black points), and the length of $\alpha$ is 6, since it contains only none-zero values.

> svp
Support Vector Machine object of class "ksvm"

SV type: C-svc  (classification)
parameter : cost C = 1

Polynomial kernel function.
Hyperparameters : degree =  1  scale =  1  offset =  0

Number of Support Vectors : 6

Objective Function Value : -2.6809
Training error : 0
Probability model included.
> svp@alpha
[[1]]
[1] 1.0000000 0.3360636 1.0000000 0.8088748 1.0000000 0.1449384


If you are using scikit-learn library in Python, you can use

clf.support_vectors_


where clf is your support vector classifier. More information on the attributes can be found here.