Support Vector Machine Optimization in Python
https://pythonprogramming.net/svm-optimization-python-machine-learning-tutorial/
More resources:-
- Convex Optimization Book: https://web.stanford.edu/~boyd/cvxboo…
- equential Minimal Optimization book: http://research.microsoft.com/pubs/68…
- More SMO: http://research.microsoft.com/pubs/69…
- CVXOPT (Convex Optimization Module for Python): http://cvxopt.org/
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
style.use('ggplot')
# build SVM class
class Support_Vector_Machine:
# The __init__ method of a class is one that runs whenever an object is created with the class
# calling self in the class allows sharing of variables across the class, so is included in all function defs
def __init__(self, visualisation=True):
# sets visualisations to what ever the user specifies (defaults to True)
self.visualisation = visualisation
# defines colours for the two states 1 & -1
self.colors = {1:'r', -1:'b'}
# sets some standards for the graphs
if self.visualisation:
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
# train
def fit(self, data):
# set up access to the data that's passed when the function is called
self.data = data
# { ||w||: [w,b] }
opt_dict = {}
#
transforms = [[1,1],
[-1,1],
[-1,-1],
[1,-1]]
# finding values to work with for our ranges.
all_data = [] # set up a placeholder for the values
# for loop to step through data and append it to all_data (list of values)
for yi in self.data:
for featureset in self.data[yi]:
for feature in featureset:
all_data.append(feature)
# next define the max and min value in list
self.max_feature_value = max(all_data)
self.min_feature_value = min(all_data)
# free up memory once we've got the values
all_data=None
# define step size for optimisation Big through to small
step_sizes = [self.max_feature_value * 0.1,
self.max_feature_value * 0.01,
# starts getting very high cost after this.
self.max_feature_value * 0.001]
# extremely expensive
b_range_multiple = 5
b_multiple = 5
# first element in vector w
latest_optimum = self.max_feature_value*10
## Begin the stepping process
for step in step_sizes:
w = np.array([latest_optimum,latest_optimum])
# we can do this because convex
optimized = False
while not optimized:
pass
def predict(self,features):
# sign( x.w+b )
classification = np.sign(np.dot(np.array(features),self.w)+self.b)
return classification
# define data dictionary
data_dict = {-1:np.array([[1,7],
[2,8],
[3,8],]),
1:np.array([[5,1],
[6,-1],
[7,3],])}