Title: Find Support Vectors
Slug: find_support_vectors
Summary: How to find support vectors in Scikit-Learn
Date: 2017-09-22 12:00
Category: Machine Learning
Tags: Support Vector Machines
Authors: Chris Albon

Preliminaries


In [1]:
# Load libraries
from sklearn.svm import SVC
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
import numpy as np

Load Iris Flower Dataset


In [2]:
#Load data with only two classes
iris = datasets.load_iris()
X = iris.data[:100,:]
y = iris.target[:100]

Standardize Features


In [3]:
# Standarize features
scaler = StandardScaler()
X_std = scaler.fit_transform(X)

Train Support Vector Classifier


In [4]:
# Create support vector classifier object
svc = SVC(kernel='linear', random_state=0)

# Train classifier
model = svc.fit(X_std, y)

View Support Vectors


In [5]:
# View support vectors
model.support_vectors_


Out[5]:
array([[-0.5810659 ,  0.43490123, -0.80621461, -0.50581312],
       [-1.52079513, -1.67626978, -1.08374115, -0.8607697 ],
       [-0.89430898, -1.46515268,  0.30389157,  0.38157832],
       [-0.5810659 , -1.25403558,  0.09574666,  0.55905661]])

View Indices Of Support Vectors


In [6]:
# View indices of support vectors
model.support_


Out[6]:
array([23, 41, 57, 98], dtype=int32)

View Number Of Support Vectors With Each Class


In [7]:
# View number of support vectors for each class
model.n_support_


Out[7]:
array([2, 2], dtype=int32)