From 40398b995e387eadcb8620bbd1309155c02286b7 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:50:41 +0530 Subject: [PATCH 01/24] Add files via upload --- neural_network/adaptive_resonance_theory.py | 147 ++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 neural_network/adaptive_resonance_theory.py diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py new file mode 100644 index 000000000000..34234417728c --- /dev/null +++ b/neural_network/adaptive_resonance_theory.py @@ -0,0 +1,147 @@ +import numpy as np + +class ART1: + """ + Adaptive Resonance Theory 1 (ART1) model for binary data clustering. + + The ART1 algorithm is a type of neural network used for unsupervised learning and clustering of binary input data. + It continuously learns to categorize inputs based on similarity while preserving previously learned categories. + The vigilance parameter controls the degree of similarity required to assign an input to an existing category, + allowing for flexible and adaptive clustering. + + Attributes: + num_features (int): Number of features in the input data. + vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. + weights (list): List of cluster weights representing the learned categories. + """ + + def __init__(self, num_features: int, vigilance: float = 0.7) -> None: + """ + Initialize the ART1 model with the given number of features and vigilance parameter. + + Args: + num_features (int): Number of features in the input data. + vigilance (float): Threshold for similarity (default is 0.7). + + Examples: + >>> model = ART1(num_features=4, vigilance=0.5) + >>> model.num_features + 4 + >>> model.vigilance + 0.5 + """ + self.vigilance = vigilance # Controls cluster strictness + self.num_features = num_features + self.weights = [] # List of cluster weights + + def train(self, data: np.ndarray) -> None: + """ + Train the ART1 model on the provided data. + + Args: + data (np.ndarray): A 2D array of binary input data (num_samples x num_features). + + Examples: + >>> model = ART1(num_features=4, vigilance=0.5) + >>> data = np.array([[1, 1, 0, 0], [1, 1, 1, 0]]) + >>> model.train(data) + >>> len(model.weights) + 2 + """ + for x in data: + match = False + for i, w in enumerate(self.weights): + if self._similarity(w, x) >= self.vigilance: + self.weights[i] = self._learn(w, x) + match = True + break + if not match: + self.weights.append(x.copy()) # Add a new cluster + + def _similarity(self, w: np.ndarray, x: np.ndarray) -> float: + """ + Calculate similarity between weight and input. + + Args: + w (np.ndarray): Weight vector representing a cluster. + x (np.ndarray): Input vector. + + Returns: + float: The similarity score between the weight and the input. + + Examples: + >>> model = ART1(num_features=4) + >>> w = np.array([1, 1, 0, 0]) + >>> x = np.array([1, 0, 0, 0]) + >>> model._similarity(w, x) + 0.25 + """ + return np.dot(w, x) / (self.num_features) + + def _learn(self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: + """ + Update cluster weights using the learning rate. + + Args: + w (np.ndarray): Current weight vector for the cluster. + x (np.ndarray): Input vector. + learning_rate (float): Learning rate for weight update (default is 0.5). + + Returns: + np.ndarray: Updated weight vector. + + Examples: + >>> model = ART1(num_features=4) + >>> w = np.array([1, 1, 0, 0]) + >>> x = np.array([0, 1, 1, 0]) + >>> model._learn(w, x) + array([0.5, 1. , 0.5, 0. ]) + """ + return learning_rate * x + (1 - learning_rate) * w + + def predict(self, x: np.ndarray) -> int: + """ + Assign data to the closest cluster. + + Args: + x (np.ndarray): Input vector. + + Returns: + int: Index of the assigned cluster, or -1 if no match. + + Examples: + >>> model = ART1(num_features=4) + >>> model.weights = [np.array([1, 1, 0, 0])] + >>> model.predict(np.array([1, 1, 0, 0])) + 0 + >>> model.predict(np.array([0, 0, 0, 0])) + -1 + """ + similarities = [self._similarity(w, x) for w in self.weights] + return np.argmax(similarities) if max(similarities) >= self.vigilance else -1 # -1 if no match + + +# Example usage for ART1 +def art1_example() -> None: + """ + Example function demonstrating the usage of the ART1 model. + + This function creates a dataset, trains the ART1 model, and prints the assigned clusters for each data point. + + Examples: + >>> art1_example() + Data point 0 assigned to cluster: 0 + Data point 1 assigned to cluster: 0 + Data point 2 assigned to cluster: 1 + Data point 3 assigned to cluster: 1 + """ + data = np.array([[1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 1, 0, 1]]) + model = ART1(num_features=4, vigilance=0.5) + model.train(data) + + for i, x in enumerate(data): + cluster = model.predict(x) + print(f"Data point {i} assigned to cluster: {cluster}") + +if __name__ == "__main__": + art1_example() From 13af4c4873101c7598bdc7a4281fcaab47ce083e Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:51:54 +0530 Subject: [PATCH 02/24] formatting code in adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 34234417728c..8280214446e3 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -4,14 +4,17 @@ class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - The ART1 algorithm is a type of neural network used for unsupervised learning and clustering of binary input data. - It continuously learns to categorize inputs based on similarity while preserving previously learned categories. - The vigilance parameter controls the degree of similarity required to assign an input to an existing category, + The ART1 algorithm is a type of neural network used for unsupervised + learning and clustering of binary input data. It continuously learns + to categorize inputs based on similarity while preserving previously + learned categories. The vigilance parameter controls the degree of + similarity required to assign an input to an existing category, allowing for flexible and adaptive clustering. Attributes: num_features (int): Number of features in the input data. - vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. + vigilance (float): Threshold for similarity that determines whether + an input matches an existing cluster. weights (list): List of cluster weights representing the learned categories. """ From d9a013473177746215f1a65787092d59da6f6d7b Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:54:05 +0530 Subject: [PATCH 03/24] add art in directory solving issue 12321 --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..1eb538372edc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -832,6 +832,7 @@ * [Input Data](neural_network/input_data.py) * [Simple Neural Network](neural_network/simple_neural_network.py) * [Two Hidden Layers Neural Network](neural_network/two_hidden_layers_neural_network.py) + * [Adaptive Resonance Theory](neural_network/adaptive_resonance_theory.py) ## Other * [Activity Selection](other/activity_selection.py) From 241981a3ca952bc6e4f839a5a240521594a44c69 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:25:22 +0000 Subject: [PATCH 04/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 30 ++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 8280214446e3..e6508e7c68d8 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,24 @@ import numpy as np + class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - The ART1 algorithm is a type of neural network used for unsupervised - learning and clustering of binary input data. It continuously learns - to categorize inputs based on similarity while preserving previously - learned categories. The vigilance parameter controls the degree of + The ART1 algorithm is a type of neural network used for unsupervised + learning and clustering of binary input data. It continuously learns + to categorize inputs based on similarity while preserving previously + learned categories. The vigilance parameter controls the degree of similarity required to assign an input to an existing category, allowing for flexible and adaptive clustering. Attributes: num_features (int): Number of features in the input data. - vigilance (float): Threshold for similarity that determines whether + vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. weights (list): List of cluster weights representing the learned categories. """ - + def __init__(self, num_features: int, vigilance: float = 0.7) -> None: """ Initialize the ART1 model with the given number of features and vigilance parameter. @@ -25,7 +26,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: Args: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity (default is 0.7). - + Examples: >>> model = ART1(num_features=4, vigilance=0.5) >>> model.num_features @@ -35,8 +36,8 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: """ self.vigilance = vigilance # Controls cluster strictness self.num_features = num_features - self.weights = [] # List of cluster weights - + self.weights = [] # List of cluster weights + def train(self, data: np.ndarray) -> None: """ Train the ART1 model on the provided data. @@ -80,8 +81,10 @@ def _similarity(self, w: np.ndarray, x: np.ndarray) -> float: 0.25 """ return np.dot(w, x) / (self.num_features) - - def _learn(self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: + + def _learn( + self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 + ) -> np.ndarray: """ Update cluster weights using the learning rate. @@ -121,7 +124,9 @@ def predict(self, x: np.ndarray) -> int: -1 """ similarities = [self._similarity(w, x) for w in self.weights] - return np.argmax(similarities) if max(similarities) >= self.vigilance else -1 # -1 if no match + return ( + np.argmax(similarities) if max(similarities) >= self.vigilance else -1 + ) # -1 if no match # Example usage for ART1 @@ -146,5 +151,6 @@ def art1_example() -> None: cluster = model.predict(x) print(f"Data point {i} assigned to cluster: {cluster}") + if __name__ == "__main__": art1_example() From fc3d7dd73635abe723040d8934756ce5036e7649 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:58:34 +0530 Subject: [PATCH 05/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 69 ++++++--------------- 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index e6508e7c68d8..0c8a0ed0ae0b 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -5,12 +5,7 @@ class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - The ART1 algorithm is a type of neural network used for unsupervised - learning and clustering of binary input data. It continuously learns - to categorize inputs based on similarity while preserving previously - learned categories. The vigilance parameter controls the degree of - similarity required to assign an input to an existing category, - allowing for flexible and adaptive clustering. + ... Attributes: num_features (int): Number of features in the input data. @@ -26,62 +21,34 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: Args: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity (default is 0.7). - - Examples: - >>> model = ART1(num_features=4, vigilance=0.5) - >>> model.num_features - 4 - >>> model.vigilance - 0.5 + + Raises: + ValueError: If num_features is not positive or if vigilance is not between 0 and 1. """ - self.vigilance = vigilance # Controls cluster strictness + if num_features <= 0: + raise ValueError("Number of features must be a positive integer.") + if not (0 <= vigilance <= 1): + raise ValueError("Vigilance parameter must be between 0 and 1.") + + self.vigilance = vigilance self.num_features = num_features - self.weights = [] # List of cluster weights - - def train(self, data: np.ndarray) -> None: - """ - Train the ART1 model on the provided data. + self.weights = [] - Args: - data (np.ndarray): A 2D array of binary input data (num_samples x num_features). - - Examples: - >>> model = ART1(num_features=4, vigilance=0.5) - >>> data = np.array([[1, 1, 0, 0], [1, 1, 1, 0]]) - >>> model.train(data) - >>> len(model.weights) - 2 - """ - for x in data: - match = False - for i, w in enumerate(self.weights): - if self._similarity(w, x) >= self.vigilance: - self.weights[i] = self._learn(w, x) - match = True - break - if not match: - self.weights.append(x.copy()) # Add a new cluster - - def _similarity(self, w: np.ndarray, x: np.ndarray) -> float: + def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ Calculate similarity between weight and input. Args: - w (np.ndarray): Weight vector representing a cluster. - x (np.ndarray): Input vector. + weight_vector (np.ndarray): Weight vector representing a cluster. + input_vector (np.ndarray): Input vector. Returns: float: The similarity score between the weight and the input. - - Examples: - >>> model = ART1(num_features=4) - >>> w = np.array([1, 1, 0, 0]) - >>> x = np.array([1, 0, 0, 0]) - >>> model._similarity(w, x) - 0.25 """ - return np.dot(w, x) / (self.num_features) - + if len(weight_vector) != self.num_features or len(input_vector) != self.num_features: + raise ValueError(f"Both weight_vector and input_vector must have {self.num_features} features.") + + return np.dot(weight_vector, input_vector) / self.num_features def _learn( self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 ) -> np.ndarray: From cfd23a222de1ade7866d6d76e3f706c4a49ca69c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:28:56 +0000 Subject: [PATCH 06/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 0c8a0ed0ae0b..5b87cea8e0b8 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -21,7 +21,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: Args: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity (default is 0.7). - + Raises: ValueError: If num_features is not positive or if vigilance is not between 0 and 1. """ @@ -29,7 +29,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: raise ValueError("Number of features must be a positive integer.") if not (0 <= vigilance <= 1): raise ValueError("Vigilance parameter must be between 0 and 1.") - + self.vigilance = vigilance self.num_features = num_features self.weights = [] @@ -45,10 +45,16 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl Returns: float: The similarity score between the weight and the input. """ - if len(weight_vector) != self.num_features or len(input_vector) != self.num_features: - raise ValueError(f"Both weight_vector and input_vector must have {self.num_features} features.") - + if ( + len(weight_vector) != self.num_features + or len(input_vector) != self.num_features + ): + raise ValueError( + f"Both weight_vector and input_vector must have {self.num_features} features." + ) + return np.dot(weight_vector, input_vector) / self.num_features + def _learn( self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 ) -> np.ndarray: From acfe9f08d07a02dc3dad477487b4df41f9795fc1 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:03:42 +0530 Subject: [PATCH 07/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 5b87cea8e0b8..415f9ebeadb4 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -16,14 +16,14 @@ class ART1: def __init__(self, num_features: int, vigilance: float = 0.7) -> None: """ - Initialize the ART1 model with the given number of features and vigilance parameter. + Initialize the ART1 model with number of features and vigilance parameter. Args: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity (default is 0.7). Raises: - ValueError: If num_features is not positive or if vigilance is not between 0 and 1. + ValueError: If num_features not positive or vigilance not between 0 and 1. """ if num_features <= 0: raise ValueError("Number of features must be a positive integer.") @@ -50,7 +50,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - f"Both weight_vector and input_vector must have {self.num_features} features." + "Both weight_vector and input_vector must have certain number." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -107,7 +107,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates a dataset, trains the ART1 model, and prints the assigned clusters for each data point. + This function creates dataset, trains ART1 model, and prints assigned clusters. Examples: >>> art1_example() From 00a17fd43876acb438383467ab831e872ac88890 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:06:39 +0530 Subject: [PATCH 08/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 53 +++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 415f9ebeadb4..3c0534fb23c8 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,3 +1,27 @@ +""" +adaptive_resonance_theory.py + +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of +category matching, allowing for flexible and adaptive clustering. + +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for +real-time data clustering and pattern recognition tasks. + +References: +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, + pp. 194–203. +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 + +""" + import numpy as np @@ -5,8 +29,6 @@ class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - ... - Attributes: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity that determines whether @@ -50,7 +72,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have certain number." + "Both weight_vector and input_vector must have the same number of features." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -78,6 +100,29 @@ def _learn( """ return learning_rate * x + (1 - learning_rate) * w + def train(self, data: np.ndarray) -> None: + """ + Train the ART1 model on the provided data. + + Args: + data (np.ndarray): Input data for training. + + Returns: + None + """ + for x in data: + # Predict the cluster for the input data point + cluster_index = self.predict(x) + + if cluster_index == -1: # No existing cluster matches + # Create a new cluster with the current input + self.weights.append(x) + else: + # Update the existing cluster's weights + self.weights[cluster_index] = self._learn( + self.weights[cluster_index], x + ) + def predict(self, x: np.ndarray) -> int: """ Assign data to the closest cluster. @@ -107,7 +152,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates dataset, trains ART1 model, and prints assigned clusters. + This function creates a dataset, trains the ART1 model, and prints assigned clusters. Examples: >>> art1_example() From 4bfd5eed67f066c512af758cc872ad1a325fbbc1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:37:01 +0000 Subject: [PATCH 09/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 3c0534fb23c8..f079a8816210 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp. 194–203. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 """ From ac9dba6057d356c9eca82f78fdf99ac95165ae6e Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:11:51 +0530 Subject: [PATCH 10/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 35 +++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index f079a8816210..4569018136fb 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,28 +1,29 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, - pp. 194–203. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, + pp. +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2)DOI: 10.1109/TNN.1988.82656 """ import numpy as np +from typing import List class ART1: @@ -33,7 +34,7 @@ class ART1: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. - weights (list): List of cluster weights representing the learned categories. + weights (List[np.ndarray]): List cluster weights representing learned categories. """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -54,7 +55,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: self.vigilance = vigilance self.num_features = num_features - self.weights = [] + self.weights: List[np.ndarray] = [] # Type annotation added here def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ @@ -72,7 +73,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have the same number of features." + "Both weight_vector and input_vector must have the same features." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -152,7 +153,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates a dataset, trains the ART1 model, and prints assigned clusters. + This function creates a dataset, trains the ART1 model, and prints clusters. Examples: >>> art1_example() From 09754566dc577843eae0b94b9b98d615d00b57e2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:42:13 +0000 Subject: [PATCH 11/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 4569018136fb..da1cb60eae30 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2)DOI: 10.1109/TNN.1988.82656 """ From 6716b43be882fb63e4f7cc8e0c579c92b73d975f Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:15:15 +0530 Subject: [PATCH 12/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index da1cb60eae30..c0fb1ddd1763 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,24 +1,24 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, - pp. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2)DOI: 10.1109/TNN.1988.82656 +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, + pp. 194–203. +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 """ @@ -34,7 +34,7 @@ class ART1: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. - weights (List[np.ndarray]): List cluster weights representing learned categories. + weights (List[np.ndarray]): List of cluster weights representing the learned categories. """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -55,7 +55,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: self.vigilance = vigilance self.num_features = num_features - self.weights: List[np.ndarray] = [] # Type annotation added here + self.weights: List[np.ndarray] = [] # Correctly typed list of numpy arrays def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ @@ -73,7 +73,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have the same features." + "Both weight_vector and input_vector must have the same number of features." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -153,7 +153,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates a dataset, trains the ART1 model, and prints clusters. + This function creates a dataset, trains the ART1 model, and prints assigned clusters. Examples: >>> art1_example() From f350ec0905760f8b9f0d45d90cb1eb791dcf801b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:45:36 +0000 Subject: [PATCH 13/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index c0fb1ddd1763..639c2a2b1213 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp. 194–203. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 """ From 80c65282abee21cabe87bd7aa2410b2a1ae1b986 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:16:45 +0530 Subject: [PATCH 14/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 639c2a2b1213..d25d5071bc56 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp. 194–203. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 """ @@ -55,7 +55,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: self.vigilance = vigilance self.num_features = num_features - self.weights: List[np.ndarray] = [] # Correctly typed list of numpy arrays + self.weights: List[np.ndarray] = [] # Type annotation added here def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ From fa54a2d091a1e0ebe0d751485186931d85b2bd5a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:47:06 +0000 Subject: [PATCH 15/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index d25d5071bc56..a84fd1de4ebe 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp. 194–203. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 """ From fbff1607cededc9e77f84d74b2b3eef6a11c90c4 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:20:16 +0530 Subject: [PATCH 16/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index a84fd1de4ebe..d1cc01d2c185 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -15,10 +15,10 @@ References: 1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." In: Neural Networks for Pattern Recognition, Oxford University Press, - pp. 194–203. + pp. 2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2), 115-130. DOI: 10.1109/TNN.1988.82656 + Neural Networks, 1(2) DOI: 10.1109/TNN.1988.82656 """ @@ -34,7 +34,7 @@ class ART1: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. - weights (List[np.ndarray]): List of cluster weights representing the learned categories. + weights (List[np.ndarray]): List of cluster weights representing learned categories. """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -73,14 +73,13 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have the same number of features." + "Both weight_vector and input_vector must have the same features." ) return np.dot(weight_vector, input_vector) / self.num_features def _learn( - self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 - ) -> np.ndarray: + self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: """ Update cluster weights using the learning rate. @@ -153,7 +152,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates a dataset, trains the ART1 model, and prints assigned clusters. + This function creates a dataset, trains the ART1 model, and prints clusters. Examples: >>> art1_example() From 97c6dc60071be030ad46b5068c7981f544db4b9a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:50:41 +0000 Subject: [PATCH 17/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index d1cc01d2c185..c88f7e024760 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -79,7 +79,8 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl return np.dot(weight_vector, input_vector) / self.num_features def _learn( - self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: + self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 + ) -> np.ndarray: """ Update cluster weights using the learning rate. From e813eb8b22965c188c3212752992c385e1ccc27c Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:22:12 +0530 Subject: [PATCH 18/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 61 ++------------------- 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index c88f7e024760..50399fc79f37 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,40 +1,14 @@ -""" -adaptive_resonance_theory.py - -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of -category matching, allowing for flexible and adaptive clustering. - -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for -real-time data clustering and pattern recognition tasks. - -References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, - pp. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2) DOI: 10.1109/TNN.1988.82656 - -""" - -import numpy as np -from typing import List - - class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. + ... + Attributes: num_features (int): Number of features in the input data. vigilance (float): Threshold for similarity that determines whether an input matches an existing cluster. - weights (List[np.ndarray]): List of cluster weights representing learned categories. + weights (list): List of cluster weights representing the learned categories. """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -55,7 +29,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: self.vigilance = vigilance self.num_features = num_features - self.weights: List[np.ndarray] = [] # Type annotation added here + self.weights = [] def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ @@ -73,7 +47,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have the same features." + "Both weight_vector and input_vector must have certain number." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -101,29 +75,6 @@ def _learn( """ return learning_rate * x + (1 - learning_rate) * w - def train(self, data: np.ndarray) -> None: - """ - Train the ART1 model on the provided data. - - Args: - data (np.ndarray): Input data for training. - - Returns: - None - """ - for x in data: - # Predict the cluster for the input data point - cluster_index = self.predict(x) - - if cluster_index == -1: # No existing cluster matches - # Create a new cluster with the current input - self.weights.append(x) - else: - # Update the existing cluster's weights - self.weights[cluster_index] = self._learn( - self.weights[cluster_index], x - ) - def predict(self, x: np.ndarray) -> int: """ Assign data to the closest cluster. @@ -153,7 +104,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates a dataset, trains the ART1 model, and prints clusters. + This function creates dataset, trains ART1 model, and prints assigned clusters. Examples: >>> art1_example() From 7b55c67ae77719a8792a4ddf1c7dbdc0e4a9c637 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:24:55 +0530 Subject: [PATCH 19/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 50399fc79f37..168924484fdd 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,3 +1,30 @@ +""" +adaptive_resonance_theory.py + +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of +category matching, allowing for flexible and adaptive clustering. + +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for +real-time data clustering and pattern recognition tasks. + +References: +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, + pp.. +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656 + +""" + +import numpy as np + + class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. From 0f017362cb6a74f6d4dfa7db695793d9308a114d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:55:17 +0000 Subject: [PATCH 20/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 168924484fdd..817c1e7b7590 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,23 +1,23 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, +1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp.. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656 """ From 5eeea82f5bd114b1f3b249f04498584dd1fb9980 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:27:16 +0530 Subject: [PATCH 21/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 53 ++++++++------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 817c1e7b7590..a1072ecdb98e 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,27 +1,3 @@ -""" -adaptive_resonance_theory.py - -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of -category matching, allowing for flexible and adaptive clustering. - -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for -real-time data clustering and pattern recognition tasks. - -References: -1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press, - pp.. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656 - -""" - import numpy as np @@ -29,13 +5,22 @@ class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - ... - - Attributes: - num_features (int): Number of features in the input data. - vigilance (float): Threshold for similarity that determines whether - an input matches an existing cluster. - weights (list): List of cluster weights representing the learned categories. + This model is designed for unsupervised learning and clustering of binary + input data. The ART1 algorithm continuously learns to categorize inputs based + on their similarity while preserving previously learned categories. This is + achieved through a vigilance parameter that controls the strictness of + category matching, allowing for flexible and adaptive clustering. + + ART1 is particularly useful in applications where it is critical to learn new + patterns without forgetting previously learned ones, making it suitable for + real-time data clustering and pattern recognition tasks. + + References: + 1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press. + 2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2). DOI: 10.1109/TNN.1988.82656 """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -56,7 +41,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: self.vigilance = vigilance self.num_features = num_features - self.weights = [] + self.weights: list[np.ndarray] = [] def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float: """ @@ -74,7 +59,7 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl or len(input_vector) != self.num_features ): raise ValueError( - "Both weight_vector and input_vector must have certain number." + "Both weight_vector and input_vector must have the same number." ) return np.dot(weight_vector, input_vector) / self.num_features @@ -142,7 +127,7 @@ def art1_example() -> None: """ data = np.array([[1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 1, 0, 1]]) model = ART1(num_features=4, vigilance=0.5) - model.train(data) + # model.train(data) # Ensure this method is defined in ART1 for i, x in enumerate(data): cluster = model.predict(x) From 1deb38b0e6afae5b3552ccd2665a6920950c024a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:57:38 +0000 Subject: [PATCH 22/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index a1072ecdb98e..56463b14e00b 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -5,21 +5,21 @@ class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - This model is designed for unsupervised learning and clustering of binary - input data. The ART1 algorithm continuously learns to categorize inputs based - on their similarity while preserving previously learned categories. This is - achieved through a vigilance parameter that controls the strictness of + This model is designed for unsupervised learning and clustering of binary + input data. The ART1 algorithm continuously learns to categorize inputs based + on their similarity while preserving previously learned categories. This is + achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. - ART1 is particularly useful in applications where it is critical to learn new - patterns without forgetting previously learned ones, making it suitable for + ART1 is particularly useful in applications where it is critical to learn new + patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: - 1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." + 1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." In: Neural Networks for Pattern Recognition, Oxford University Press. - 2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on + 2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2). DOI: 10.1109/TNN.1988.82656 """ From 115ac6b77394ad3706f75258291dc06f5b5f5268 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 10:29:58 +0530 Subject: [PATCH 23/24] Update adaptive_resonance_theory.py --- neural_network/adaptive_resonance_theory.py | 115 ++++++++++---------- 1 file changed, 60 insertions(+), 55 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index 56463b14e00b..e63311cc165a 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,26 +1,36 @@ -import numpy as np +""" +adaptive_resonance_theory.py + +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of +category matching, allowing for flexible and adaptive clustering. + +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for +real-time data clustering and pattern recognition tasks. + +References: +1. Carpenter, G. A., & Grossberg, S. (1987). "Adaptive Resonance Theory." + In: Neural Networks for Pattern Recognition, Oxford University Press, pp.. +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on + Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656 +""" +import numpy as np class ART1: """ Adaptive Resonance Theory 1 (ART1) model for binary data clustering. - This model is designed for unsupervised learning and clustering of binary - input data. The ART1 algorithm continuously learns to categorize inputs based - on their similarity while preserving previously learned categories. This is - achieved through a vigilance parameter that controls the strictness of - category matching, allowing for flexible and adaptive clustering. - - ART1 is particularly useful in applications where it is critical to learn new - patterns without forgetting previously learned ones, making it suitable for - real-time data clustering and pattern recognition tasks. - - References: - 1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory." - In: Neural Networks for Pattern Recognition, Oxford University Press. - 2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on - Neural Networks, 1(2). DOI: 10.1109/TNN.1988.82656 + Attributes: + num_features (int): Number of features in the input data. + vigilance (float): Threshold for similarity that determines whether + an input matches an existing cluster. + weights (list): List of cluster weights representing the learned categories. """ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: @@ -32,7 +42,7 @@ def __init__(self, num_features: int, vigilance: float = 0.7) -> None: vigilance (float): Threshold for similarity (default is 0.7). Raises: - ValueError: If num_features not positive or vigilance not between 0 and 1. + ValueError: If num_features is not positive or vigilance is not between 0 and 1. """ if num_features <= 0: raise ValueError("Number of features must be a positive integer.") @@ -54,61 +64,56 @@ def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> fl Returns: float: The similarity score between the weight and the input. """ - if ( - len(weight_vector) != self.num_features - or len(input_vector) != self.num_features - ): - raise ValueError( - "Both weight_vector and input_vector must have the same number." - ) + if len(weight_vector) != self.num_features or len(input_vector) != self.num_features: + raise ValueError("Both weight_vector and input_vector must have the same number of features.") return np.dot(weight_vector, input_vector) / self.num_features - def _learn( - self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5 - ) -> np.ndarray: + def _learn(self, current_weights: np.ndarray, input_vector: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: """ Update cluster weights using the learning rate. Args: - w (np.ndarray): Current weight vector for the cluster. - x (np.ndarray): Input vector. + current_weights (np.ndarray): Current weight vector for the cluster. + input_vector (np.ndarray): Input vector. learning_rate (float): Learning rate for weight update (default is 0.5). Returns: np.ndarray: Updated weight vector. + """ + return learning_rate * input_vector + (1 - learning_rate) * current_weights - Examples: - >>> model = ART1(num_features=4) - >>> w = np.array([1, 1, 0, 0]) - >>> x = np.array([0, 1, 1, 0]) - >>> model._learn(w, x) - array([0.5, 1. , 0.5, 0. ]) + def train(self, input_data: np.ndarray) -> None: """ - return learning_rate * x + (1 - learning_rate) * w + Train the ART1 model on the provided input data. + + Args: + input_data (np.ndarray): Array of input vectors to train on. - def predict(self, x: np.ndarray) -> int: + Returns: + None + """ + for input_vector in input_data: + assigned_cluster_index = self.predict(input_vector) + if assigned_cluster_index == -1: + # No matching cluster, create a new one + self.weights.append(input_vector) + else: + # Update the weights of the assigned cluster + self.weights[assigned_cluster_index] = self._learn(self.weights[assigned_cluster_index], input_vector) + + def predict(self, input_vector: np.ndarray) -> int: """ Assign data to the closest cluster. Args: - x (np.ndarray): Input vector. + input_vector (np.ndarray): Input vector. Returns: int: Index of the assigned cluster, or -1 if no match. - - Examples: - >>> model = ART1(num_features=4) - >>> model.weights = [np.array([1, 1, 0, 0])] - >>> model.predict(np.array([1, 1, 0, 0])) - 0 - >>> model.predict(np.array([0, 0, 0, 0])) - -1 """ - similarities = [self._similarity(w, x) for w in self.weights] - return ( - np.argmax(similarities) if max(similarities) >= self.vigilance else -1 - ) # -1 if no match + similarities = [self._similarity(weight, input_vector) for weight in self.weights] + return np.argmax(similarities) if max(similarities) >= self.vigilance else -1 # -1 if no match # Example usage for ART1 @@ -116,7 +121,7 @@ def art1_example() -> None: """ Example function demonstrating the usage of the ART1 model. - This function creates dataset, trains ART1 model, and prints assigned clusters. + This function creates a dataset, trains the ART1 model, and prints assigned clusters. Examples: >>> art1_example() @@ -127,10 +132,10 @@ def art1_example() -> None: """ data = np.array([[1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [0, 1, 0, 1]]) model = ART1(num_features=4, vigilance=0.5) - # model.train(data) # Ensure this method is defined in ART1 + model.train(data) - for i, x in enumerate(data): - cluster = model.predict(x) + for i, input_vector in enumerate(data): + cluster = model.predict(input_vector) print(f"Data point {i} assigned to cluster: {cluster}") From e203df8c410c8580688831091c2e0cdd8d4a37a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 05:00:21 +0000 Subject: [PATCH 24/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory.py | 49 ++++++++++++++------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/neural_network/adaptive_resonance_theory.py b/neural_network/adaptive_resonance_theory.py index e63311cc165a..aff03b5c113b 100644 --- a/neural_network/adaptive_resonance_theory.py +++ b/neural_network/adaptive_resonance_theory.py @@ -1,27 +1,28 @@ """ adaptive_resonance_theory.py -This module implements the Adaptive Resonance Theory 1 (ART1) model, a type -of neural network designed for unsupervised learning and clustering of binary -input data. The ART1 algorithm continuously learns to categorize inputs based -on their similarity while preserving previously learned categories. This is -achieved through a vigilance parameter that controls the strictness of +This module implements the Adaptive Resonance Theory 1 (ART1) model, a type +of neural network designed for unsupervised learning and clustering of binary +input data. The ART1 algorithm continuously learns to categorize inputs based +on their similarity while preserving previously learned categories. This is +achieved through a vigilance parameter that controls the strictness of category matching, allowing for flexible and adaptive clustering. -ART1 is particularly useful in applications where it is critical to learn new -patterns without forgetting previously learned ones, making it suitable for +ART1 is particularly useful in applications where it is critical to learn new +patterns without forgetting previously learned ones, making it suitable for real-time data clustering and pattern recognition tasks. References: -1. Carpenter, G. A., & Grossberg, S. (1987). "Adaptive Resonance Theory." +1. Carpenter, G. A., & Grossberg, S. (1987). "Adaptive Resonance Theory." In: Neural Networks for Pattern Recognition, Oxford University Press, pp.. -2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern - Recognition by a Self-Organizing Neural Network." IEEE Transactions on +2. Carpenter, G. A., & Grossberg, S. (1988). "The ART of Adaptive Pattern + Recognition by a Self-Organizing Neural Network." IEEE Transactions on Neural Networks, 1(2) . DOI: 10.1109/TNN.1988.82656 """ import numpy as np + class ART1: """ 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 Returns: float: The similarity score between the weight and the input. """ - if len(weight_vector) != self.num_features or len(input_vector) != self.num_features: - raise ValueError("Both weight_vector and input_vector must have the same number of features.") + if ( + len(weight_vector) != self.num_features + or len(input_vector) != self.num_features + ): + raise ValueError( + "Both weight_vector and input_vector must have the same number of features." + ) return np.dot(weight_vector, input_vector) / self.num_features - def _learn(self, current_weights: np.ndarray, input_vector: np.ndarray, learning_rate: float = 0.5) -> np.ndarray: + def _learn( + self, + current_weights: np.ndarray, + input_vector: np.ndarray, + learning_rate: float = 0.5, + ) -> np.ndarray: """ Update cluster weights using the learning rate. @@ -100,7 +111,9 @@ def train(self, input_data: np.ndarray) -> None: self.weights.append(input_vector) else: # Update the weights of the assigned cluster - self.weights[assigned_cluster_index] = self._learn(self.weights[assigned_cluster_index], input_vector) + self.weights[assigned_cluster_index] = self._learn( + self.weights[assigned_cluster_index], input_vector + ) def predict(self, input_vector: np.ndarray) -> int: """ @@ -112,8 +125,12 @@ def predict(self, input_vector: np.ndarray) -> int: Returns: int: Index of the assigned cluster, or -1 if no match. """ - similarities = [self._similarity(weight, input_vector) for weight in self.weights] - return np.argmax(similarities) if max(similarities) >= self.vigilance else -1 # -1 if no match + similarities = [ + self._similarity(weight, input_vector) for weight in self.weights + ] + return ( + np.argmax(similarities) if max(similarities) >= self.vigilance else -1 + ) # -1 if no match # Example usage for ART1