1
1
"""
2
- - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
2
+ - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
3
3
Name - - ART1 - Adaptive Resonance Theory 1
4
4
Goal - - Cluster Binary Data
5
5
Detail: Unsupervised clustering model using a vigilance parameter
10
10
Author: Your Name
11
11
12
12
Date: 2024.10.31
13
- - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
13
+ - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
14
14
"""
15
+
15
16
import numpy as np
16
17
18
+
17
19
class ART1 :
18
20
def __init__ (self , num_features , vigilance = 0.8 ):
19
21
"""
20
22
Initialize the ART1 model with the number of features and the vigilance parameter.
21
-
23
+
22
24
Parameters:
23
25
num_features (int): Number of features in input binary data.
24
26
vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1).
25
27
"""
26
28
self .num_features = num_features
27
29
self .vigilance = vigilance
28
30
self .weights = [] # Stores the weights for clusters
29
-
31
+
30
32
def _similarity (self , x , w ):
31
33
"""
32
34
Calculate similarity between input vector x and weight vector w.
33
-
35
+
34
36
Parameters:
35
37
x (np.array): Input binary vector.
36
38
w (np.array): Cluster weight vector.
37
-
39
+
38
40
Returns:
39
41
float: Similarity value based on the intersection over the input length.
40
42
"""
41
43
return np .sum (np .minimum (x , w )) / np .sum (x )
42
-
44
+
43
45
def _weight_update (self , x , w ):
44
46
"""
45
47
Update weights for a cluster based on input vector.
46
-
48
+
47
49
Parameters:
48
50
x (np.array): Input binary vector.
49
51
w (np.array): Cluster weight vector.
50
-
52
+
51
53
Returns:
52
54
np.array: Updated weight vector.
53
55
"""
54
56
return np .minimum (x , w )
55
-
57
+
56
58
def train (self , data ):
57
59
"""
58
60
Train the ART1 model to form clusters based on the vigilance parameter.
59
-
61
+
60
62
Parameters:
61
63
data (np.array): Binary dataset with each row as a sample.
62
64
"""
@@ -69,26 +71,26 @@ def train(self, data):
69
71
break
70
72
if not assigned :
71
73
self .weights .append (x .copy ())
72
-
74
+
73
75
def predict (self , x ):
74
76
"""
75
77
Predict the cluster for a new input vector or classify it as a new cluster.
76
-
78
+
77
79
Parameters:
78
80
x (np.array): Input binary vector.
79
-
81
+
80
82
Returns:
81
83
int: Cluster index for the input or -1 if classified as a new cluster.
82
84
"""
83
85
for i , w in enumerate (self .weights ):
84
86
if self ._similarity (x , w ) >= self .vigilance :
85
87
return i
86
88
return - 1
87
-
89
+
88
90
def get_weights (self ):
89
91
"""
90
92
Retrieve the weight vectors of the clusters.
91
-
93
+
92
94
Returns:
93
95
list: List of weight vectors for each cluster.
94
96
"""
0 commit comments