Skip to content

Commit

Permalink
Initialize perceptron testing
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherjenness committed Aug 13, 2017
1 parent 55f46e0 commit 0f7b793
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 5 additions & 6 deletions ML/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import cvxopt

#Useful Kernels
# Useful Kernels
def linear_kernel(**kwargs):
def f(x1, x2):
return np.inner(x1, x2)
Expand Down Expand Up @@ -94,7 +94,7 @@ def fit(self, X, y):

minimization = cvxopt.solvers.qp(P, q, G, h, A, b)
alphas = np.ravel(minimization['x'])
#Extract support vectors
# Extract support vectors
for index, alpha in enumerate(alphas):
if alpha > 10**-6:
self.SValphas.append(alpha)
Expand Down Expand Up @@ -136,7 +136,7 @@ def polynomial(x1, x2, power, coef, *args):
return (np.dot(x1, x2) + c)**power

@staticmethod
def rbf(x1, x2, gamma):
def rbf(x1, x2, gamma, *args, **kwargs):
difference = x1 - x2
return np.exp(-gamma * (np.dot(difference, difference.T)**2))

Expand Down Expand Up @@ -203,7 +203,7 @@ def fit(self, X, y):
y = np.asarray(y)
X = np.asarray(X)
X = np.column_stack((np.ones(np.shape(X)[0]), X))
#Check if y contains only 1 or -1 values
# Check if y contains only 1 or -1 values
if False in np.in1d(y, [-1, 1]):
raise NameError('y required to contain only 1 and -1')
if isinstance(self.weights, bool):
Expand All @@ -216,8 +216,7 @@ def fit(self, X, y):
if iteration > self.max_iter:
if self.pocket:
self.weights = pocket_weights
else:
self.weights = np.NaN
self.learned = True
return self
classification = np.equal(np.sign(np.dot(X, np.transpose(self.weights))), y)
#find misclassified point, and update classifier to correct misclassified point
Expand Down
9 changes: 9 additions & 0 deletions tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ def test_SupportVectorMachine_polynomial_kernel():
assert prediction == y[0]
prediction = SVM.predict(X[-1])
assert prediction == y[-1]


def test_Perceptron():
X, y = data.categorical_2Dmatrix_data_big()
y = (y * 2) - 1
perceptron = svm.Perceptron()
perceptron.fit(X, y)
predictions = perceptron.predict(X)
assert (predictions == y).sum() > 7

0 comments on commit 0f7b793

Please sign in to comment.