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 5 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
123 changes: 123 additions & 0 deletions neural_network/adaptive_resonance_theory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import numpy as np


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

def __init__(self, num_features: int, vigilance: float = 0.7) -> None:
"""
Initialize the ART1 model with the given number of features and vigilance parameter.

Check failure on line 19 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:19:89: E501 Line too long (92 > 88)

Args:
num_features (int): Number of features in the input data.
vigilance (float): Threshold for similarity (default is 0.7).

Check failure on line 24 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:24:1: W293 Blank line contains whitespace
Raises:
ValueError: If num_features is not positive or if vigilance is not between 0 and 1.

Check failure on line 26 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:26:89: E501 Line too long (95 > 88)
"""
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.")

Check failure on line 32 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:32:1: W293 Blank line contains whitespace
self.vigilance = vigilance
self.num_features = num_features
self.weights = []

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:

Check failure on line 48 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:48:89: E501 Line too long (93 > 88)
raise ValueError(f"Both weight_vector and input_vector must have {self.num_features} features.")

Check failure on line 49 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

neural_network/adaptive_resonance_theory.py:49:30: EM102 Exception must not use an f-string literal, assign to variable first

Check failure on line 49 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:49:89: E501 Line too long (108 > 88)

Check failure on line 50 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:50:1: W293 Blank line contains whitespace
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 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 the assigned clusters for each data point.

Check failure on line 104 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:104:89: E501 Line too long (113 > 88)

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