-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpocket_algorithm.py
More file actions
65 lines (50 loc) · 2.68 KB
/
pocket_algorithm.py
File metadata and controls
65 lines (50 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import itertools
import numpy as np
from perceptron_algorithm import PerceptronAlgorithm
class PocketAlgorithm(PerceptronAlgorithm):
"""
An implementation of the Pocket Algorithm, extending the PerceptronAlgorithm.
The Pocket Algorithm is designed for binary classification, maintaining the
best weights and bias observed during training to handle non-linearly separable data.
Attributes:
training_data (ndarray): A 2D array where each row represents a feature vector for a training example.
training_labels (ndarray): A 1D array where each element represents the label (e.g., -1 or 1) of the corresponding training example.
weight_vector (ndarray): A 1D array representing the current weight vector of the model.
b (float): The current bias term of the model.
_errors (list): A list storing the error rates observed at different stages of training.
Inherits:
PerceptronAlgorithm: The base Perceptron algorithm, providing basic functionality for binary classification.
Methods:
(To be implemented in the full algorithm, such as running the Pocket Algorithm or accessing error rates.)
"""
def __init__(self, training_data, training_labels):
super().__init__(training_data, training_labels)
def run(self, updates, max_epochs=1):
updates_count = 0
epochs_count = 0
# Store initial error rate and weights as the best ones
self.best_error_rate = self.error_rate
self.best_weight_vector = self.weight_vector.copy()
self.best_b = self.b
for i in itertools.cycle(range(len(self.training_data))):
if updates_count >= updates or epochs_count >= max_epochs:
break
# If we reach the end of the data, increment epoch count
if i == len(self.training_data) - 1:
epochs_count += 1
# If the current sample is classified correctly, continue
if self.classify_single(self.training_data[i]) == self.training_labels[i]:
continue
# Run one update of the Perceptron
super().run(updates=1, max_epochs=1)
# After the update, calculate the current error rate
current_error_rate = self.error_rate
# If the new error rate is better (lower), update the pocket parameters
if current_error_rate < self.best_error_rate:
self.best_weight_vector = self.weight_vector.copy()
self.best_b = self.b
self.best_error_rate = current_error_rate
updates_count += 1
# After finishing the updates, set the best weight vector and bias
self.update_parameters(self.best_weight_vector, self.best_b)
return None