-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicSVM.py
More file actions
67 lines (49 loc) · 1.77 KB
/
BasicSVM.py
File metadata and controls
67 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
style.use('ggplot')
class Support_Vector_Machine:
def __init__(self, visualization=True):
self.visualization = visualization
self.colors = (1:'r',-1:'b')
if self.visualization:
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
#train
def fit(self, data):
self.data = data
# { ||w||: [w,b] }
opt_dict = {}
transforms = [[1,1],[-1,1],[-1,-1],[1,-1]]
all_data = []
for yi in self.data:
for featureset in self.data[yi]:
for feature in featureset:
all_data.append(feature)
self.max_feature_value = max(all_data)
self.min_feature_value = min(all_data)
all_data = None
step_sizes = [self.max_feature_value * 0.1,
self.max_feature_value * 0.01,
#point of expense.
self.max_feature_value * 0.001]
#extremely expensive
b_range_multiple = 5
b_multiple = 5
latest_optimum = self.max_feature_value*10
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_i.w+b)
classification = np.sign(np.dot(np.array(features),self.w)+self.b)
return classification
data_dict = (-1:np.array([[1,7],[2,8],[3,8]]),1:np.array([[5,1],[6,-1],[7,3]]))