1
1
import numpy as np
2
2
3
+
3
4
class ART1 :
4
5
"""
5
6
Adaptive Resonance Theory 1 (ART1) model for binary data clustering.
6
7
7
- The ART1 algorithm is a type of neural network used for unsupervised
8
- learning and clustering of binary input data. It continuously learns
9
- to categorize inputs based on similarity while preserving previously
10
- learned categories. The vigilance parameter controls the degree of
8
+ The ART1 algorithm is a type of neural network used for unsupervised
9
+ learning and clustering of binary input data. It continuously learns
10
+ to categorize inputs based on similarity while preserving previously
11
+ learned categories. The vigilance parameter controls the degree of
11
12
similarity required to assign an input to an existing category,
12
13
allowing for flexible and adaptive clustering.
13
14
14
15
Attributes:
15
16
num_features (int): Number of features in the input data.
16
- vigilance (float): Threshold for similarity that determines whether
17
+ vigilance (float): Threshold for similarity that determines whether
17
18
an input matches an existing cluster.
18
19
weights (list): List of cluster weights representing the learned categories.
19
20
"""
20
-
21
+
21
22
def __init__ (self , num_features : int , vigilance : float = 0.7 ) -> None :
22
23
"""
23
24
Initialize the ART1 model with the given number of features and vigilance parameter.
24
25
25
26
Args:
26
27
num_features (int): Number of features in the input data.
27
28
vigilance (float): Threshold for similarity (default is 0.7).
28
-
29
+
29
30
Examples:
30
31
>>> model = ART1(num_features=4, vigilance=0.5)
31
32
>>> model.num_features
@@ -35,8 +36,8 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None:
35
36
"""
36
37
self .vigilance = vigilance # Controls cluster strictness
37
38
self .num_features = num_features
38
- self .weights = [] # List of cluster weights
39
-
39
+ self .weights = [] # List of cluster weights
40
+
40
41
def train (self , data : np .ndarray ) -> None :
41
42
"""
42
43
Train the ART1 model on the provided data.
@@ -80,8 +81,10 @@ def _similarity(self, w: np.ndarray, x: np.ndarray) -> float:
80
81
0.25
81
82
"""
82
83
return np .dot (w , x ) / (self .num_features )
83
-
84
- def _learn (self , w : np .ndarray , x : np .ndarray , learning_rate : float = 0.5 ) -> np .ndarray :
84
+
85
+ def _learn (
86
+ self , w : np .ndarray , x : np .ndarray , learning_rate : float = 0.5
87
+ ) -> np .ndarray :
85
88
"""
86
89
Update cluster weights using the learning rate.
87
90
@@ -121,7 +124,9 @@ def predict(self, x: np.ndarray) -> int:
121
124
-1
122
125
"""
123
126
similarities = [self ._similarity (w , x ) for w in self .weights ]
124
- return np .argmax (similarities ) if max (similarities ) >= self .vigilance else - 1 # -1 if no match
127
+ return (
128
+ np .argmax (similarities ) if max (similarities ) >= self .vigilance else - 1
129
+ ) # -1 if no match
125
130
126
131
127
132
# Example usage for ART1
@@ -146,5 +151,6 @@ def art1_example() -> None:
146
151
cluster = model .predict (x )
147
152
print (f"Data point { i } assigned to cluster: { cluster } " )
148
153
154
+
149
155
if __name__ == "__main__" :
150
156
art1_example ()
0 commit comments