This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
code:perceptron [2015/08/28 10:35] asa |
code:perceptron [2015/09/03 15:03] asa |
||
---|---|---|---|
Line 5: | Line 5: | ||
<file python perceptron.py> | <file python perceptron.py> | ||
- | |||
import numpy as np | import numpy as np | ||
Line 14: | Line 13: | ||
"""An implementation of the perceptron algorithm. | """An implementation of the perceptron algorithm. | ||
Note that this implementation does not include a bias term""" | Note that this implementation does not include a bias term""" | ||
- | |||
- | attributes = {'max_iterations': 50, | ||
- | 'learning_rate' : 0.2} | ||
def __init__(self, max_iterations=100, learning_rate=0.2) : | def __init__(self, max_iterations=100, learning_rate=0.2) : | ||
Line 24: | Line 20: | ||
def fit(self, X, y) : | def fit(self, X, y) : | ||
+ | """ | ||
+ | Train a classifier using the perceptron training algorithm. | ||
+ | After training the attribute 'w' will contain the perceptron weight vector. | ||
+ | Parameters | ||
+ | ---------- | ||
+ | |||
+ | X : ndarray, shape (num_examples, n_features) | ||
+ | Training data. | ||
+ | |||
+ | y : ndarray, shape (n_examples,) | ||
+ | Array of labels. | ||
+ | | ||
+ | """ | ||
self.w = np.zeros(len(X[0])) | self.w = np.zeros(len(X[0])) | ||
converged = False | converged = False | ||
Line 34: | Line 43: | ||
self.w = self.w + y[i] * self.learning_rate * X[i] | self.w = self.w + y[i] * self.learning_rate * X[i] | ||
converged = False | converged = False | ||
+ | plot_data(X, y, self.w) | ||
iterations += 1 | iterations += 1 | ||
- | plot_data(X, y, self.w) | ||
self.converged = converged | self.converged = converged | ||
if converged : | if converged : | ||
Line 44: | Line 53: | ||
| | ||
def predict(self, X) : | def predict(self, X) : | ||
+ | """ | ||
+ | make predictions using a trained linear classifier | ||
+ | Parameters | ||
+ | ---------- | ||
+ | |||
+ | X : ndarray, shape (num_examples, n_features) | ||
+ | Training data. | ||
+ | """ | ||
+ | | ||
scores = np.dot(self.w, X) | scores = np.dot(self.w, X) | ||
return np.sign(scores) | return np.sign(scores) | ||
- | |||
def generate_separable_data(N) : | def generate_separable_data(N) : | ||
Line 71: | Line 88: | ||
if __name__=='__main__' : | if __name__=='__main__' : | ||
- | X,y,w = generate_separable_data(20) | + | X,y,w = generate_separable_data(40) |
p = Perceptron() | p = Perceptron() | ||
p.fit(X,y) | p.fit(X,y) | ||
</file> | </file> |