Skip to content

Commit

Permalink
Merge pull request #2 from drkrillo/sequential_minimum_optimization_a…
Browse files Browse the repository at this point in the history
…dd_doctests

Sequential minimum optimization add doctests
  • Loading branch information
drkrillo authored Sep 18, 2024
2 parents 2642f19 + 94b1403 commit fb58570
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions machine_learning/sequential_minimum_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2):
# Normalize data using min-max method
def _norm(self, data):
if self._init:
self._min = np.min(data, axis=0) # [0, -2, 2] [1, 1, 0]
self._max = np.max(data, axis=0) # [1, -1, 3] [1, 1, 1]
self._min = np.min(data, axis=0)
self._max = np.max(data, axis=0)
self._init = False
return (data - self._min) / (self._max - self._min)
else:
Expand Down Expand Up @@ -436,6 +436,12 @@ def length(self):


class Kernel:
"""
>>> from machine_learning.sequential_minimum_optimization import Kernel
>>> kernel = Kernel(kernel='linear')
>>> kernel._kernel_name
'linear'
"""
def __init__(self, kernel, degree=1.0, coef0=0.0, gamma=1.0):
self.degree = np.float64(degree)
self.coef0 = np.float64(coef0)
Expand All @@ -445,9 +451,23 @@ def __init__(self, kernel, degree=1.0, coef0=0.0, gamma=1.0):
self._check()

def _polynomial(self, v1, v2):
"""
>>> from machine_learning.sequential_minimum_optimization import Kernel
>>> kernel = Kernel(kernel='linear')
>>> result = kernel._polynomial(np.array([1, 2]), np.array([2, 3]))
>>> int(result) == 8
True
"""
return (self.gamma * np.inner(v1, v2) + self.coef0) ** self.degree

def _linear(self, v1, v2):
"""
>>> from machine_learning.sequential_minimum_optimization import Kernel
>>> kernel = Kernel(kernel='linear')
>>> result = kernel._polynomial(np.array([1, 2]), np.array([2, 3]))
>>> int(result) == 8
True
"""
return np.inner(v1, v2) + self.coef0

def _rbf(self, v1, v2):
Expand Down

0 comments on commit fb58570

Please sign in to comment.