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 18 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
126 changes: 126 additions & 0 deletions neural_network/adaptive_resonance_theory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 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 = []

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

Check failure on line 34 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:34:42: F821 Undefined name `np`

Check failure on line 34 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:34:68: F821 Undefined name `np`

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 certain number."
)

return np.dot(weight_vector, input_vector) / self.num_features

Check failure on line 53 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:53:16: F821 Undefined name `np`

def _learn(
self, w: np.ndarray, x: np.ndarray, learning_rate: float = 0.5

Check failure on line 56 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:56:18: F821 Undefined name `np`

Check failure on line 56 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:56:33: F821 Undefined name `np`

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:

Check failure on line 57 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:57:10: F821 Undefined name `np`
"""
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:

Check failure on line 78 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:78:26: F821 Undefined name `np`

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

Check failure on line 98 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:98:13: F821 Undefined name `np`
) # -1 if no match


# Example usage for ART1
def art1_example() -> None:
"""
Example function demonstrating the usage of the ART1 model.

This function creates dataset, trains 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]])

Check failure on line 116 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

neural_network/adaptive_resonance_theory.py:116:12: F821 Undefined name `np`
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