-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
solving Issue 12321 #12324
Changes from 18 commits
40398b9
13af4c4
d9a0134
241981a
fc3d7dd
cfd23a2
acfe9f0
00a17fd
4bfd5ee
ac9dba6
0975456
6716b43
f350ec0
80c6528
fa54a2d
fbff160
97c6dc6
e813eb8
7b55c67
0f01736
5eeea82
1deb38b
115ac6b
e203df8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
""" | ||
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 | ||
|
||
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
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
) -> 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
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 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]]) | ||
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() |
There was a problem hiding this comment.
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