Skip to content

Commit e203df8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 115ac6b commit e203df8

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

neural_network/adaptive_resonance_theory.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
"""
22
adaptive_resonance_theory.py
33
4-
This module implements the Adaptive Resonance Theory 1 (ART1) model, a type
5-
of neural network designed for unsupervised learning and clustering of binary
6-
input data. The ART1 algorithm continuously learns to categorize inputs based
7-
on their similarity while preserving previously learned categories. This is
8-
achieved through a vigilance parameter that controls the strictness of
4+
This module implements the Adaptive Resonance Theory 1 (ART1) model, a type
5+
of neural network designed for unsupervised learning and clustering of binary
6+
input data. The ART1 algorithm continuously learns to categorize inputs based
7+
on their similarity while preserving previously learned categories. This is
8+
achieved through a vigilance parameter that controls the strictness of
99
category matching, allowing for flexible and adaptive clustering.
1010
11-
ART1 is particularly useful in applications where it is critical to learn new
12-
patterns without forgetting previously learned ones, making it suitable for
11+
ART1 is particularly useful in applications where it is critical to learn new
12+
patterns without forgetting previously learned ones, making it suitable for
1313
real-time data clustering and pattern recognition tasks.
1414
1515
References:
16-
1. Carpenter, G. A., & Grossberg, S. (1987). "Adaptive Resonance Theory."
16+
1. Carpenter, G. A., & Grossberg, S. (1987). "Adaptive Resonance Theory."
1717
In: Neural Networks for Pattern Recognition, Oxford University Press, pp..
18-
2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern
19-
Recognition by a Self-Organizing Neural Network." IEEE Transactions on
18+
2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern
19+
Recognition by a Self-Organizing Neural Network." IEEE Transactions on
2020
Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656
2121
"""
2222

2323
import numpy as np
2424

25+
2526
class ART1:
2627
"""
2728
Adaptive Resonance Theory 1 (ART1) model for binary data clustering.
@@ -64,12 +65,22 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl
6465
Returns:
6566
float: The similarity score between the weight and the input.
6667
"""
67-
if len(weight_vector) != self.num_features or len(input_vector) != self.num_features:
68-
raise ValueError("Both weight_vector and input_vector must have the same number of features.")
68+
if (
69+
len(weight_vector) != self.num_features
70+
or len(input_vector) != self.num_features
71+
):
72+
raise ValueError(
73+
"Both weight_vector and input_vector must have the same number of features."
74+
)
6975

7076
return np.dot(weight_vector, input_vector) / self.num_features
7177

72-
def _learn(self, current_weights: np.ndarray, input_vector: np.ndarray, learning_rate: float = 0.5) -> np.ndarray:
78+
def _learn(
79+
self,
80+
current_weights: np.ndarray,
81+
input_vector: np.ndarray,
82+
learning_rate: float = 0.5,
83+
) -> np.ndarray:
7384
"""
7485
Update cluster weights using the learning rate.
7586
@@ -100,7 +111,9 @@ def train(self, input_data: np.ndarray) -> None:
100111
self.weights.append(input_vector)
101112
else:
102113
# Update the weights of the assigned cluster
103-
self.weights[assigned_cluster_index] = self._learn(self.weights[assigned_cluster_index], input_vector)
114+
self.weights[assigned_cluster_index] = self._learn(
115+
self.weights[assigned_cluster_index], input_vector
116+
)
104117

105118
def predict(self, input_vector: np.ndarray) -> int:
106119
"""
@@ -112,8 +125,12 @@ def predict(self, input_vector: np.ndarray) -> int:
112125
Returns:
113126
int: Index of the assigned cluster, or -1 if no match.
114127
"""
115-
similarities = [self._similarity(weight, input_vector) for weight in self.weights]
116-
return np.argmax(similarities) if max(similarities) >= self.vigilance else -1 # -1 if no match
128+
similarities = [
129+
self._similarity(weight, input_vector) for weight in self.weights
130+
]
131+
return (
132+
np.argmax(similarities) if max(similarities) >= self.vigilance else -1
133+
) # -1 if no match
117134

118135

119136
# Example usage for ART1

0 commit comments

Comments
 (0)