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

class ART1:

Check failure on line 3 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

neural_network/adaptive_resonance_theory.py:1:1: I001 Import block is un-sorted or un-formatted
"""
Adaptive Resonance Theory 1 (ART1) model for binary data clustering.

The ART1 algorithm is a type of neural network used for unsupervised

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:73: W291 Trailing whitespace
learning and clustering of binary input data. It continuously learns

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:73: W291 Trailing whitespace
to categorize inputs based on similarity while preserving previously

Check failure on line 9 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:9:73: W291 Trailing whitespace
learned categories. The vigilance parameter controls the degree of

Check failure on line 10 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/adaptive_resonance_theory.py:10:71: W291 Trailing whitespace
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

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
an input matches an existing cluster.
weights (list): List of cluster weights representing the learned categories.
"""

Check failure on line 20 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:20:1: W293 Blank line contains whitespace
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 23 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory.py:23: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 28 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:28:1: W293 Blank line contains whitespace
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

Check failure on line 39 in neural_network/adaptive_resonance_theory.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/adaptive_resonance_theory.py:39:1: W293 Blank line contains whitespace
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:

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

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

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

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

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