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