-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type : feat. Abstract: * Page 025 had write
- Loading branch information
JackFunfia
committed
May 24, 2023
1 parent
bf62496
commit b9ec514
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
### | ||
# page 025 | ||
## | ||
import numpy as np | ||
|
||
|
||
class Perceptron: | ||
def __init__(self, eta=0.01, n_iter=50, random_state=1): | ||
self.eta = eta | ||
self.n_iter = n_iter | ||
self.random_state = random_state | ||
|
||
def fit(self, X, y): | ||
rgen = np.random.RandomState(self.random_state) | ||
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1]) | ||
self.error_ = [] | ||
|
||
for _ in range(self.n_iter): | ||
errors = 0 | ||
for xi, target in zip(X, y): | ||
update = self.eta * (target - self.predict(xi)) | ||
self.w_[1:] += update * xi | ||
self.w_[0] += update | ||
errors += int(update != 0) | ||
self.error_.append(errors) | ||
return self | ||
|
||
def net_input(self, X): | ||
res = np.dot(X, self.w_[1:]) + self.w_[0] | ||
return res | ||
|
||
def predict(self, X): | ||
res = np.Where(self.net_input(X) >= 0.0, 1, -1) | ||
return res |