Machine Learning - Perceptron
Formula
For , find hyperplane which can divide the data into two parts.
The percetron can defined as:
where:
The distance between and hyperplane is
As we know:
so the distance is:
For error set , we define the loss function:
Gradient(partial derivative of ):
Using random gradient descent:
Dual Form
Assume is used times when update , at last is:
so if we know ,
Then the dual form is updating :
Code
class Perceptron:
def __init__(self):
pass
def fit(self, X, y, alpha=0.001, loop=1000):
n_sample, n_feature = X.shape
times = np.zeros(n_sample)
gram = np.dot(X, X.T)
for _ in range(loop):
has_error = False
for i in range(n_sample):
if (y[i] * np.dot(alpha * times * y, gram[:, i])) <= 0:
has_error = True
times[i] += 1
if not has_error:
break
self.theta = np.dot(alpha * times * y, X)
return self
def predict(self, X):
return np.dot(X, self.theta)