From b8a4a9fb4f7ebd9b614b23368adca155ea3a6251 Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:47:51 +0530 Subject: [PATCH 01/13] Create radial_basis_function_neural_network --- .../radial_basis_function_neural_network | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 neural_network/radial_basis_function_neural_network diff --git a/neural_network/radial_basis_function_neural_network b/neural_network/radial_basis_function_neural_network new file mode 100644 index 000000000000..fa58be6c490d --- /dev/null +++ b/neural_network/radial_basis_function_neural_network @@ -0,0 +1,88 @@ +""" + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - +Name - - RBFNN - Radial Basis Function Neural Network +Goal - - Recognize Patterns in Data +Detail: Total 3 layers neural network + * Input layer + * Hidden layer with RBF activation + * Output layer +Author: Your Name +Github: your_email@example.com +Date: 2024.10.31 +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +""" + +import numpy as np # For numerical operations + +class RBFNN: + def __init__(self, input_size, hidden_size, output_size): + """ + Initialize the RBFNN parameters. + + :param input_size: Number of input features + :param hidden_size: Number of hidden units in the RBF layer + :param output_size: Number of output classes + """ + self.input_size = input_size # Size of input layer + self.hidden_size = hidden_size # Size of hidden layer + self.output_size = output_size # Size of output layer + + # Initialize centers and spread of the RBF neurons + self.centers = np.random.rand(hidden_size, input_size) # Centers for RBF + self.spread = np.random.rand(hidden_size) # Spread for each RBF + + # Initialize weights for the output layer + self.weights = np.random.rand(hidden_size, output_size) # Weights for output layer + + def rbf(self, x, center, spread): + """ Radial Basis Function (Gaussian). """ + return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2)) + + def forward(self, x): + """ Forward pass through the network. """ + hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer + for i in range(self.hidden_size): + hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs + + output = np.dot(hidden_outputs, self.weights) # Compute final output + return output + + def train(self, X, y, epochs, learning_rate): + """ + Train the RBFNN model. + + :param X: Input data + :param y: Target output + :param epochs: Number of training iterations + :param learning_rate: Learning rate for weight updates + """ + for epoch in range(epochs): + for i in range(len(X)): + x_i = X[i] + y_i = y[i] + + # Forward pass + hidden_outputs = np.zeros(self.hidden_size) + for j in range(self.hidden_size): + hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j]) + + output = np.dot(hidden_outputs, self.weights) # Output layer + + # Calculate the error + error = y_i - output + + # Update weights + self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error + + def predict(self, X): + """ + Predict outputs for given input data. + + :param X: Input data + :return: Predicted outputs + """ + predictions = [] + for x in X: + output = self.forward(x) # Forward pass to get prediction + predictions.append(output) + return np.array(predictions) From 923dcaec1fcc5f88c2205005763fe78246ca0b76 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:20:38 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network b/neural_network/radial_basis_function_neural_network index fa58be6c490d..73ad18479d10 100644 --- a/neural_network/radial_basis_function_neural_network +++ b/neural_network/radial_basis_function_neural_network @@ -1,5 +1,5 @@ """ - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - RBFNN - Radial Basis Function Neural Network Goal - - Recognize Patterns in Data Detail: Total 3 layers neural network @@ -9,7 +9,7 @@ Detail: Total 3 layers neural network Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ import numpy as np # For numerical operations @@ -18,7 +18,7 @@ class RBFNN: def __init__(self, input_size, hidden_size, output_size): """ Initialize the RBFNN parameters. - + :param input_size: Number of input features :param hidden_size: Number of hidden units in the RBF layer :param output_size: Number of output classes @@ -26,7 +26,7 @@ class RBFNN: self.input_size = input_size # Size of input layer self.hidden_size = hidden_size # Size of hidden layer self.output_size = output_size # Size of output layer - + # Initialize centers and spread of the RBF neurons self.centers = np.random.rand(hidden_size, input_size) # Centers for RBF self.spread = np.random.rand(hidden_size) # Spread for each RBF @@ -50,7 +50,7 @@ class RBFNN: def train(self, X, y, epochs, learning_rate): """ Train the RBFNN model. - + :param X: Input data :param y: Target output :param epochs: Number of training iterations @@ -67,17 +67,17 @@ class RBFNN: hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j]) output = np.dot(hidden_outputs, self.weights) # Output layer - + # Calculate the error error = y_i - output - + # Update weights self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error def predict(self, X): """ Predict outputs for given input data. - + :param X: Input data :return: Predicted outputs """ From fa6233a1ab0b896eb42e6e304a3282f12e803631 Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:04:00 +0530 Subject: [PATCH 03/13] Rename radial_basis_function_neural_network to radial_basis_function_neural_network.py --- ...ion_neural_network => radial_basis_function_neural_network.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename neural_network/{radial_basis_function_neural_network => radial_basis_function_neural_network.py} (100%) diff --git a/neural_network/radial_basis_function_neural_network b/neural_network/radial_basis_function_neural_network.py similarity index 100% rename from neural_network/radial_basis_function_neural_network rename to neural_network/radial_basis_function_neural_network.py From 2a8b2014eeec96c283cdd5aed234c25b0aa00f4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:34:27 +0000 Subject: [PATCH 04/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 73ad18479d10..5abab11a557b 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -14,6 +14,7 @@ import numpy as np # For numerical operations + class RBFNN: def __init__(self, input_size, hidden_size, output_size): """ @@ -32,17 +33,21 @@ def __init__(self, input_size, hidden_size, output_size): self.spread = np.random.rand(hidden_size) # Spread for each RBF # Initialize weights for the output layer - self.weights = np.random.rand(hidden_size, output_size) # Weights for output layer + self.weights = np.random.rand( + hidden_size, output_size + ) # Weights for output layer def rbf(self, x, center, spread): - """ Radial Basis Function (Gaussian). """ - return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2)) + """Radial Basis Function (Gaussian).""" + return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * spread**2)) def forward(self, x): - """ Forward pass through the network. """ + """Forward pass through the network.""" hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer for i in range(self.hidden_size): - hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs + hidden_outputs[i] = self.rbf( + x, self.centers[i], self.spread[i] + ) # Compute RBF outputs output = np.dot(hidden_outputs, self.weights) # Compute final output return output From a3bf6259ff2de48957442d04ae17d50ac760ba54 Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:07:54 +0530 Subject: [PATCH 05/13] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 5abab11a557b..4c889596e434 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,5 @@ """ - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - RBFNN - Radial Basis Function Neural Network Goal - - Recognize Patterns in Data Detail: Total 3 layers neural network @@ -9,17 +9,16 @@ Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ import numpy as np # For numerical operations - class RBFNN: def __init__(self, input_size, hidden_size, output_size): """ Initialize the RBFNN parameters. - + :param input_size: Number of input features :param hidden_size: Number of hidden units in the RBF layer :param output_size: Number of output classes @@ -27,44 +26,41 @@ def __init__(self, input_size, hidden_size, output_size): self.input_size = input_size # Size of input layer self.hidden_size = hidden_size # Size of hidden layer self.output_size = output_size # Size of output layer - + + rng = np.random.default_rng() # Create a random number generator # Initialize centers and spread of the RBF neurons - self.centers = np.random.rand(hidden_size, input_size) # Centers for RBF - self.spread = np.random.rand(hidden_size) # Spread for each RBF + self.centers = rng.random((hidden_size, input_size)) # Centers for RBF + self.spread = rng.random(hidden_size) # Spread for each RBF # Initialize weights for the output layer - self.weights = np.random.rand( - hidden_size, output_size - ) # Weights for output layer + self.weights = rng.random((hidden_size, output_size)) # Weights for output layer def rbf(self, x, center, spread): - """Radial Basis Function (Gaussian).""" - return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * spread**2)) + """ Radial Basis Function (Gaussian). """ + return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2)) def forward(self, x): - """Forward pass through the network.""" + """ Forward pass through the network. """ hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer for i in range(self.hidden_size): - hidden_outputs[i] = self.rbf( - x, self.centers[i], self.spread[i] - ) # Compute RBF outputs + hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs output = np.dot(hidden_outputs, self.weights) # Compute final output return output - def train(self, X, y, epochs, learning_rate): + def train(self, x_train, y_train, epochs, learning_rate): """ Train the RBFNN model. - - :param X: Input data - :param y: Target output + + :param x_train: Input data + :param y_train: Target output :param epochs: Number of training iterations :param learning_rate: Learning rate for weight updates """ - for epoch in range(epochs): - for i in range(len(X)): - x_i = X[i] - y_i = y[i] + for _ in range(epochs): # Use underscore for unused loop variable + for i in range(len(x_train)): + x_i = x_train[i] + y_i = y_train[i] # Forward pass hidden_outputs = np.zeros(self.hidden_size) @@ -72,22 +68,22 @@ def train(self, X, y, epochs, learning_rate): hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j]) output = np.dot(hidden_outputs, self.weights) # Output layer - + # Calculate the error error = y_i - output - + # Update weights self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error - def predict(self, X): + def predict(self, x_test): """ Predict outputs for given input data. - - :param X: Input data + + :param x_test: Input data :return: Predicted outputs """ predictions = [] - for x in X: + for x in x_test: output = self.forward(x) # Forward pass to get prediction predictions.append(output) return np.array(predictions) From c2a5cd0cdc8116b34404478282ccd4553f8417d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:38:21 +0000 Subject: [PATCH 06/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 4c889596e434..68e3ecbd2aa3 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,5 @@ """ - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - RBFNN - Radial Basis Function Neural Network Goal - - Recognize Patterns in Data Detail: Total 3 layers neural network @@ -9,16 +9,17 @@ Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ import numpy as np # For numerical operations + class RBFNN: def __init__(self, input_size, hidden_size, output_size): """ Initialize the RBFNN parameters. - + :param input_size: Number of input features :param hidden_size: Number of hidden units in the RBF layer :param output_size: Number of output classes @@ -26,24 +27,28 @@ def __init__(self, input_size, hidden_size, output_size): self.input_size = input_size # Size of input layer self.hidden_size = hidden_size # Size of hidden layer self.output_size = output_size # Size of output layer - + rng = np.random.default_rng() # Create a random number generator # Initialize centers and spread of the RBF neurons self.centers = rng.random((hidden_size, input_size)) # Centers for RBF self.spread = rng.random(hidden_size) # Spread for each RBF # Initialize weights for the output layer - self.weights = rng.random((hidden_size, output_size)) # Weights for output layer + self.weights = rng.random( + (hidden_size, output_size) + ) # Weights for output layer def rbf(self, x, center, spread): - """ Radial Basis Function (Gaussian). """ - return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2)) + """Radial Basis Function (Gaussian).""" + return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * spread**2)) def forward(self, x): - """ Forward pass through the network. """ + """Forward pass through the network.""" hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer for i in range(self.hidden_size): - hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs + hidden_outputs[i] = self.rbf( + x, self.centers[i], self.spread[i] + ) # Compute RBF outputs output = np.dot(hidden_outputs, self.weights) # Compute final output return output @@ -51,7 +56,7 @@ def forward(self, x): def train(self, x_train, y_train, epochs, learning_rate): """ Train the RBFNN model. - + :param x_train: Input data :param y_train: Target output :param epochs: Number of training iterations @@ -68,17 +73,17 @@ def train(self, x_train, y_train, epochs, learning_rate): hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j]) output = np.dot(hidden_outputs, self.weights) # Output layer - + # Calculate the error error = y_i - output - + # Update weights self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error def predict(self, x_test): """ Predict outputs for given input data. - + :param x_test: Input data :return: Predicted outputs """ From f6028cc6bf9f929d04f3dc3c87509e6f169404a7 Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:13:46 +0530 Subject: [PATCH 07/13] Created adaptive_resonance_thoery_1.py --- neural_network/adaptive_resonance_thoery_1.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 neural_network/adaptive_resonance_thoery_1.py diff --git a/neural_network/adaptive_resonance_thoery_1.py b/neural_network/adaptive_resonance_thoery_1.py new file mode 100644 index 000000000000..5215f62cfe1e --- /dev/null +++ b/neural_network/adaptive_resonance_thoery_1.py @@ -0,0 +1,95 @@ +""" +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +Name - - ART1 - Adaptive Resonance Theory 1 +Goal - - Cluster Binary Data +Detail: Unsupervised clustering model using a vigilance parameter + to control cluster formation in binary datasets. + * Initialize with features and vigilance threshold + * Train to form clusters based on input patterns + * Predict for assigning new inputs to clusters +Author: Your Name +Github: your_email@example.com +Date: 2024.10.31 +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +""" +import numpy as np + +class ART1: + def __init__(self, num_features, vigilance=0.8): + """ + Initialize the ART1 model with the number of features and the vigilance parameter. + + Parameters: + num_features (int): Number of features in input binary data. + vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). + """ + self.num_features = num_features + self.vigilance = vigilance + self.weights = [] # Stores the weights for clusters + + def _similarity(self, x, w): + """ + Calculate similarity between input vector x and weight vector w. + + Parameters: + x (np.array): Input binary vector. + w (np.array): Cluster weight vector. + + Returns: + float: Similarity value based on the intersection over the input length. + """ + return np.sum(np.minimum(x, w)) / np.sum(x) + + def _weight_update(self, x, w): + """ + Update weights for a cluster based on input vector. + + Parameters: + x (np.array): Input binary vector. + w (np.array): Cluster weight vector. + + Returns: + np.array: Updated weight vector. + """ + return np.minimum(x, w) + + def train(self, data): + """ + Train the ART1 model to form clusters based on the vigilance parameter. + + Parameters: + data (np.array): Binary dataset with each row as a sample. + """ + for x in data: + assigned = False + for i, w in enumerate(self.weights): + if self._similarity(x, w) >= self.vigilance: + self.weights[i] = self._weight_update(x, w) + assigned = True + break + if not assigned: + self.weights.append(x.copy()) + + def predict(self, x): + """ + Predict the cluster for a new input vector or classify it as a new cluster. + + Parameters: + x (np.array): Input binary vector. + + Returns: + int: Cluster index for the input or -1 if classified as a new cluster. + """ + for i, w in enumerate(self.weights): + if self._similarity(x, w) >= self.vigilance: + return i + return -1 + + def get_weights(self): + """ + Retrieve the weight vectors of the clusters. + + Returns: + list: List of weight vectors for each cluster. + """ + return self.weights From 6d8e719bb126a2a7c8132c9f6764d534ba2197de Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:14:12 +0530 Subject: [PATCH 08/13] Rename adaptive_resonance_thoery_1.py to adaptive_resonance_theory_1.py --- ...ptive_resonance_thoery_1.py => adaptive_resonance_theory_1.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename neural_network/{adaptive_resonance_thoery_1.py => adaptive_resonance_theory_1.py} (100%) diff --git a/neural_network/adaptive_resonance_thoery_1.py b/neural_network/adaptive_resonance_theory_1.py similarity index 100% rename from neural_network/adaptive_resonance_thoery_1.py rename to neural_network/adaptive_resonance_theory_1.py From 4b5fa398cc0a217bc9685034b77be3e374b1c488 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:44:24 +0000 Subject: [PATCH 09/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory_1.py | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/neural_network/adaptive_resonance_theory_1.py b/neural_network/adaptive_resonance_theory_1.py index 5215f62cfe1e..4d5c303931ee 100644 --- a/neural_network/adaptive_resonance_theory_1.py +++ b/neural_network/adaptive_resonance_theory_1.py @@ -1,5 +1,5 @@ """ -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - ART1 - Adaptive Resonance Theory 1 Goal - - Cluster Binary Data Detail: Unsupervised clustering model using a vigilance parameter @@ -10,15 +10,17 @@ Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ + import numpy as np + class ART1: def __init__(self, num_features, vigilance=0.8): """ Initialize the ART1 model with the number of features and the vigilance parameter. - + Parameters: num_features (int): Number of features in input binary data. vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). @@ -26,37 +28,37 @@ def __init__(self, num_features, vigilance=0.8): self.num_features = num_features self.vigilance = vigilance self.weights = [] # Stores the weights for clusters - + def _similarity(self, x, w): """ Calculate similarity between input vector x and weight vector w. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: float: Similarity value based on the intersection over the input length. """ return np.sum(np.minimum(x, w)) / np.sum(x) - + def _weight_update(self, x, w): """ Update weights for a cluster based on input vector. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: np.array: Updated weight vector. """ return np.minimum(x, w) - + def train(self, data): """ Train the ART1 model to form clusters based on the vigilance parameter. - + Parameters: data (np.array): Binary dataset with each row as a sample. """ @@ -69,14 +71,14 @@ def train(self, data): break if not assigned: self.weights.append(x.copy()) - + def predict(self, x): """ Predict the cluster for a new input vector or classify it as a new cluster. - + Parameters: x (np.array): Input binary vector. - + Returns: int: Cluster index for the input or -1 if classified as a new cluster. """ @@ -84,11 +86,11 @@ def predict(self, x): if self._similarity(x, w) >= self.vigilance: return i return -1 - + def get_weights(self): """ Retrieve the weight vectors of the clusters. - + Returns: list: List of weight vectors for each cluster. """ From 3d4150abf4faeeeddb3f3d29cd07b4341345373a Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:17:26 +0530 Subject: [PATCH 10/13] Update adaptive_resonance_theory_1.py --- neural_network/adaptive_resonance_theory_1.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/neural_network/adaptive_resonance_theory_1.py b/neural_network/adaptive_resonance_theory_1.py index 4d5c303931ee..6febab500835 100644 --- a/neural_network/adaptive_resonance_theory_1.py +++ b/neural_network/adaptive_resonance_theory_1.py @@ -15,12 +15,11 @@ import numpy as np - class ART1: def __init__(self, num_features, vigilance=0.8): """ Initialize the ART1 model with the number of features and the vigilance parameter. - + Parameters: num_features (int): Number of features in input binary data. vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). @@ -28,69 +27,71 @@ def __init__(self, num_features, vigilance=0.8): self.num_features = num_features self.vigilance = vigilance self.weights = [] # Stores the weights for clusters - + def _similarity(self, x, w): """ Calculate similarity between input vector x and weight vector w. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: float: Similarity value based on the intersection over the input length. """ return np.sum(np.minimum(x, w)) / np.sum(x) - + def _weight_update(self, x, w): """ Update weights for a cluster based on input vector. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: np.array: Updated weight vector. """ return np.minimum(x, w) - + def train(self, data): """ Train the ART1 model to form clusters based on the vigilance parameter. - + Parameters: data (np.array): Binary dataset with each row as a sample. """ for x in data: assigned = False for i, w in enumerate(self.weights): + # Split the line here to satisfy the line-length requirement if self._similarity(x, w) >= self.vigilance: self.weights[i] = self._weight_update(x, w) assigned = True break if not assigned: self.weights.append(x.copy()) - + def predict(self, x): """ Predict the cluster for a new input vector or classify it as a new cluster. - + Parameters: x (np.array): Input binary vector. - + Returns: int: Cluster index for the input or -1 if classified as a new cluster. """ for i, w in enumerate(self.weights): + # Split the line here to satisfy the line-length requirement if self._similarity(x, w) >= self.vigilance: return i return -1 - + def get_weights(self): """ Retrieve the weight vectors of the clusters. - + Returns: list: List of weight vectors for each cluster. """ From a65dcc25414c49af6cbc1a80e30a6df73713f5b7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:47:50 +0000 Subject: [PATCH 11/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory_1.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/neural_network/adaptive_resonance_theory_1.py b/neural_network/adaptive_resonance_theory_1.py index 6febab500835..11499c0d051d 100644 --- a/neural_network/adaptive_resonance_theory_1.py +++ b/neural_network/adaptive_resonance_theory_1.py @@ -15,11 +15,12 @@ import numpy as np + class ART1: def __init__(self, num_features, vigilance=0.8): """ Initialize the ART1 model with the number of features and the vigilance parameter. - + Parameters: num_features (int): Number of features in input binary data. vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). @@ -27,37 +28,37 @@ def __init__(self, num_features, vigilance=0.8): self.num_features = num_features self.vigilance = vigilance self.weights = [] # Stores the weights for clusters - + def _similarity(self, x, w): """ Calculate similarity between input vector x and weight vector w. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: float: Similarity value based on the intersection over the input length. """ return np.sum(np.minimum(x, w)) / np.sum(x) - + def _weight_update(self, x, w): """ Update weights for a cluster based on input vector. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: np.array: Updated weight vector. """ return np.minimum(x, w) - + def train(self, data): """ Train the ART1 model to form clusters based on the vigilance parameter. - + Parameters: data (np.array): Binary dataset with each row as a sample. """ @@ -71,14 +72,14 @@ def train(self, data): break if not assigned: self.weights.append(x.copy()) - + def predict(self, x): """ Predict the cluster for a new input vector or classify it as a new cluster. - + Parameters: x (np.array): Input binary vector. - + Returns: int: Cluster index for the input or -1 if classified as a new cluster. """ @@ -87,11 +88,11 @@ def predict(self, x): if self._similarity(x, w) >= self.vigilance: return i return -1 - + def get_weights(self): """ Retrieve the weight vectors of the clusters. - + Returns: list: List of weight vectors for each cluster. """ From b52bc75e0e3504ec12637b50b457cc787ed0bb60 Mon Sep 17 00:00:00 2001 From: Swastik Bhattacharya <152956696+Swastik0710@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:23:53 +0530 Subject: [PATCH 12/13] Update adaptive_resonance_theory_1.py --- neural_network/adaptive_resonance_theory_1.py | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/neural_network/adaptive_resonance_theory_1.py b/neural_network/adaptive_resonance_theory_1.py index 11499c0d051d..9152b6283b93 100644 --- a/neural_network/adaptive_resonance_theory_1.py +++ b/neural_network/adaptive_resonance_theory_1.py @@ -1,5 +1,5 @@ """ -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - ART1 - Adaptive Resonance Theory 1 Goal - - Cluster Binary Data Detail: Unsupervised clustering model using a vigilance parameter @@ -10,17 +10,16 @@ Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ import numpy as np - class ART1: def __init__(self, num_features, vigilance=0.8): """ Initialize the ART1 model with the number of features and the vigilance parameter. - + Parameters: num_features (int): Number of features in input binary data. vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). @@ -28,71 +27,73 @@ def __init__(self, num_features, vigilance=0.8): self.num_features = num_features self.vigilance = vigilance self.weights = [] # Stores the weights for clusters - + def _similarity(self, x, w): """ Calculate similarity between input vector x and weight vector w. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: float: Similarity value based on the intersection over the input length. """ return np.sum(np.minimum(x, w)) / np.sum(x) - + def _weight_update(self, x, w): """ Update weights for a cluster based on input vector. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: np.array: Updated weight vector. """ return np.minimum(x, w) - + def train(self, data): """ Train the ART1 model to form clusters based on the vigilance parameter. - + Parameters: data (np.array): Binary dataset with each row as a sample. """ for x in data: assigned = False for i, w in enumerate(self.weights): - # Split the line here to satisfy the line-length requirement - if self._similarity(x, w) >= self.vigilance: + # Check similarity and update weights if similarity exceeds vigilance + similarity = self._similarity(x, w) + if similarity >= self.vigilance: self.weights[i] = self._weight_update(x, w) assigned = True break if not assigned: self.weights.append(x.copy()) - + def predict(self, x): """ Predict the cluster for a new input vector or classify it as a new cluster. - + Parameters: x (np.array): Input binary vector. - + Returns: int: Cluster index for the input or -1 if classified as a new cluster. """ for i, w in enumerate(self.weights): - # Split the line here to satisfy the line-length requirement - if self._similarity(x, w) >= self.vigilance: + # Check similarity for prediction + similarity = self._similarity(x, w) + if similarity >= self.vigilance: return i return -1 - + def get_weights(self): """ Retrieve the weight vectors of the clusters. - + Returns: list: List of weight vectors for each cluster. """ From 8196fd24d38e0e9060b9f672557cac45d3db36ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:54:16 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/adaptive_resonance_theory_1.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/neural_network/adaptive_resonance_theory_1.py b/neural_network/adaptive_resonance_theory_1.py index 9152b6283b93..abff2cf7e294 100644 --- a/neural_network/adaptive_resonance_theory_1.py +++ b/neural_network/adaptive_resonance_theory_1.py @@ -1,5 +1,5 @@ """ -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - ART1 - Adaptive Resonance Theory 1 Goal - - Cluster Binary Data Detail: Unsupervised clustering model using a vigilance parameter @@ -10,16 +10,17 @@ Author: Your Name Github: your_email@example.com Date: 2024.10.31 -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - """ import numpy as np + class ART1: def __init__(self, num_features, vigilance=0.8): """ Initialize the ART1 model with the number of features and the vigilance parameter. - + Parameters: num_features (int): Number of features in input binary data. vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1). @@ -27,37 +28,37 @@ def __init__(self, num_features, vigilance=0.8): self.num_features = num_features self.vigilance = vigilance self.weights = [] # Stores the weights for clusters - + def _similarity(self, x, w): """ Calculate similarity between input vector x and weight vector w. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: float: Similarity value based on the intersection over the input length. """ return np.sum(np.minimum(x, w)) / np.sum(x) - + def _weight_update(self, x, w): """ Update weights for a cluster based on input vector. - + Parameters: x (np.array): Input binary vector. w (np.array): Cluster weight vector. - + Returns: np.array: Updated weight vector. """ return np.minimum(x, w) - + def train(self, data): """ Train the ART1 model to form clusters based on the vigilance parameter. - + Parameters: data (np.array): Binary dataset with each row as a sample. """ @@ -72,14 +73,14 @@ def train(self, data): break if not assigned: self.weights.append(x.copy()) - + def predict(self, x): """ Predict the cluster for a new input vector or classify it as a new cluster. - + Parameters: x (np.array): Input binary vector. - + Returns: int: Cluster index for the input or -1 if classified as a new cluster. """ @@ -89,11 +90,11 @@ def predict(self, x): if similarity >= self.vigilance: return i return -1 - + def get_weights(self): """ Retrieve the weight vectors of the clusters. - + Returns: list: List of weight vectors for each cluster. """