Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Commit 1453318

Browse files
authored
Update svm_smo.py
1 parent 734224d commit 1453318

File tree

1 file changed

+11
-61
lines changed

1 file changed

+11
-61
lines changed

svm_smo.py

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,8 @@ def linear(x, y, b=1):
3939
Linear kernel between x and y
4040
"""
4141

42-
result = None
43-
44-
#######################################################################
45-
# TODO: #
46-
# Compute the linear kernel between x and y #
47-
#######################################################################
48-
result = np.dot(x, y.T)+b
49-
pass
50-
51-
#######################################################################
52-
# END OF YOUR CODE #
53-
#######################################################################
54-
42+
result = None
43+
result = np.dot(x, y.T)+b
5544
return result
5645

5746

@@ -69,24 +58,15 @@ def gaussian(x, y, sigma=1):
6958
"""
7059

7160
result = None
72-
73-
#######################################################################
74-
# TODO: #
75-
# Compute the Gaussian kernel between x and y #
76-
#######################################################################
77-
#result = np.exp(-(np.absolute(x-y)**2)/2*(sigma**2))
61+
7862
if np.ndim(x) == 1 and np.ndim(y) == 1:
7963
result = np.exp(- np.linalg.norm(x - y) / (2 * sigma ** 2))
8064
elif (np.ndim(x) > 1 and np.ndim(y) == 1) or (np.ndim(x) == 1 and np.ndim(y) > 1):
8165
result = np.exp(- np.linalg.norm(x - y, axis=1) / (2 * sigma ** 2))
8266
elif np.ndim(x) > 1 and np.ndim(y) > 1:
8367
result = np.exp(- np.linalg.norm(x[:, np.newaxis] - y[np.newaxis, :], axis=2) / (2 * sigma ** 2))
8468
pass
85-
86-
#######################################################################
87-
# END OF YOUR CODE #
88-
#######################################################################
89-
69+
9070
return result
9171

9272

@@ -103,19 +83,8 @@ def objective_function(alphas, y,kernel, X):
10383
Value of the objective function
10484
"""
10585

106-
result = None
107-
108-
#######################################################################
109-
# TODO: #
110-
# Compute the objective function #
111-
#######################################################################
112-
result = np.sum(alphas) - 0.5 * np.sum(y * y * kernel(X, X) * alphas * alphas) #correct
113-
pass
114-
115-
#######################################################################
116-
# END OF YOUR CODE #
117-
#######################################################################
118-
86+
result = None
87+
result = np.sum(alphas) - 0.5 * np.sum(y * y * kernel(X, X) * alphas * alphas) #correct
11988
return result
12089

12190

@@ -134,20 +103,9 @@ def decision_function(alphas, target, kernel, X_train, x_test, b):
134103
Output of decision function
135104
"""
136105

137-
result = None
138-
139-
#######################################################################
140-
# TODO: #
141-
# Compute the decision function #
142-
#######################################################################
143-
#result = (alphas * target) @ kernel(X_train, x_test) - b #correct
144-
result = np.dot((alphas * target), kernel(X_train, x_test)) - b
145-
pass
146-
147-
#######################################################################
148-
# END OF YOUR CODE #
149-
#######################################################################
150-
106+
result = None
107+
#result = (alphas * target) @ kernel(X_train, x_test) - b #only for python3.x+
108+
result = np.dot((alphas * target), kernel(X_train, x_test)) - b
151109
return result
152110

153111

@@ -215,23 +173,15 @@ def take_step(i1, i2, model):
215173
if (eta < 0):
216174
a2 = alph2 - y2 * (E1 - E2) / eta
217175
# Clip a2 based on bounds L & H
218-
219-
#######################################################################
220-
# TODO: #
221-
# Clip a2 based on the last equation in the notes #
222-
#######################################################################
176+
223177
if L < a2 < H:
224178
a2 = a2
225179
elif (a2 <= L):
226180
a2 = L
227181
elif (a2 >= H):
228182
a2 = H
229183
pass
230-
231-
#######################################################################
232-
# END OF YOUR CODE #
233-
#######################################################################
234-
184+
235185
# If eta is non-negative, move new a2 to bound with greater objective function value
236186
else:
237187
alphas_adj = model.alphas.copy()

0 commit comments

Comments
 (0)