This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
code:perceptron [2015/08/28 10:36] asa |
code:perceptron [2016/08/09 10:25] 127.0.0.1 external edit |
||
---|---|---|---|
Line 5: | Line 5: | ||
<file python perceptron.py> | <file python perceptron.py> | ||
- | |||
import numpy as np | import numpy as np | ||
Line 21: | 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 31: | 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 41: | 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 68: | 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> |