Lab 10: Support Vector Machine (SVM) Classification

CILO: 3
Weeks: 36–37
Lab #10

Aim

Train a linear SVM on toy data and predict class for a new sample.

Objectives

Algorithm / Procedure

  1. Define X, y.
  2. Fit SVC(kernel='linear').
  3. Predict a sample and print support vectors.

Python Code

from sklearn import svm

X = [[0,0],[1,1],[2,2],[3,3]]
y = [0,1,1,0]

clf = svm.SVC(kernel='linear').fit(X, y)
print("Prediction for [1,2]:", clf.predict([[1,2]])[0])
print("Number of support vectors:", clf.n_support_)

Sample Output (expected)

Prediction for [1,2]: 1
Number of support vectors: [2 2]