Skip to content

solving Issue 12321 #12324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
40398b9
Add files via upload
JeninaAngelin Oct 30, 2024
13af4c4
formatting code in adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
d9a0134
add art in directory solving issue 12321
JeninaAngelin Oct 30, 2024
241981a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
fc3d7dd
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
cfd23a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
acfe9f0
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
00a17fd
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
4bfd5ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
ac9dba6
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
0975456
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
6716b43
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
f350ec0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
80c6528
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
fa54a2d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
fbff160
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
97c6dc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
e813eb8
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
7b55c67
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
0f01736
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
5eeea82
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
1deb38b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
115ac6b
Update adaptive_resonance_theory.py
JeninaAngelin Oct 30, 2024
e203df8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
175 changes: 175 additions & 0 deletions neural_network/adaptive_resonance_theory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"""
adaptive_resonance_theory.py

This module implements the Adaptive Resonance Theory 1 (ART1) model, a type

Check failure on line 4 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:4:76: W291 Trailing whitespace
of neural network designed for unsupervised learning and clustering of binary

Check failure on line 5 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:5:78: W291 Trailing whitespace
input data. The ART1 algorithm continuously learns to categorize inputs based

Check failure on line 6 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:6:78: W291 Trailing whitespace
on their similarity while preserving previously learned categories. This is

Check failure on line 7 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:7:76: W291 Trailing whitespace
achieved through a vigilance parameter that controls the strictness of

Check failure on line 8 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:8:71: W291 Trailing whitespace
category matching, allowing for flexible and adaptive clustering.

ART1 is particularly useful in applications where it is critical to learn new

Check failure on line 11 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:11:78: W291 Trailing whitespace
patterns without forgetting previously learned ones, making it suitable for

Check failure on line 12 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:12:76: W291 Trailing whitespace
real-time data clustering and pattern recognition tasks.

References:
1. Carpenter, G. A., & Grossberg, S. (1987). "A Adaptive Resonance Theory."

Check failure on line 16 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:16:76: W291 Trailing whitespace
In: Neural Networks for Pattern Recognition, Oxford University Press,

Check failure on line 17 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:17:73: W291 Trailing whitespace
pp. 194–203.

Check failure on line 18 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

neural_network/adaptive_resonance_theory.py:18:11: RUF002 Docstring contains ambiguous `–` (EN DASH). Did you mean `-` (HYPHEN-MINUS)?
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
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 the learned categories.
"""

def __init__(self, num_features: int, vigilance: float = 0.7) -> None:
"""
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 not positive or vigilance not between 0 and 1.
"""
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[np.ndarray] = [] # Correctly typed list of numpy arrays

def _similarity(self, weight_vector: np.ndarray, input_vector: np.ndarray) -> float:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function _similarity

"""
Calculate similarity between weight and input.

Args:
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.
"""
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: w

Please provide descriptive name for the parameter: x

) -> 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 train(self, data: np.ndarray) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function train

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function train

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function train

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function train

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/adaptive_resonance_theory.py, please provide doctest for the function train

"""
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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

"""
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 assigned clusters.

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()
Loading