From e1cdd45bef33118dabfb9529cc1053f6ccbbfafd Mon Sep 17 00:00:00 2001 From: Eeman Majumder Date: Sun, 9 Oct 2022 19:53:18 +0530 Subject: [PATCH 1/9] added self organising maps algo --- machine_learning/Self_Organising_Maps.py | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 machine_learning/Self_Organising_Maps.py diff --git a/machine_learning/Self_Organising_Maps.py b/machine_learning/Self_Organising_Maps.py new file mode 100644 index 000000000000..b09e8d1bbc34 --- /dev/null +++ b/machine_learning/Self_Organising_Maps.py @@ -0,0 +1,62 @@ +import math + + + +class SOM: + # Function here computes the winning vector + # by Euclidean distance + def winner(self, weights, sample): + D0 = 0 + D1 = 0 + for i in range(len(sample)): + D0 = D0 + math.pow((sample[i] - weights[0][i]), 2) + D1 = D1 + math.pow((sample[i] - weights[1][i]), 2) + if D0 > D1: + return 0 + else: + return 1 + + # Function here updates the winning vector + def update(self, weights, sample, J, alpha): + for i in range(len(weights)): + weights[J][i] = weights[J][i] + alpha * (sample[i] - weights[J][i]) + return weights + +# Driver code +def main(): + # Training Examples ( m, n ) + T = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] + m, n = len(T), len(T[0]) + + # weight initialization ( n, C ) + weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] + + # training + ob = SOM() + epochs = 3 + alpha = 0.5 + + for i in range(epochs): + for j in range(m): + + # training sample + sample = T[j] + + # Compute winner vector + J = ob.winner(weights, sample) + + # Update winning vector + weights = ob.update(weights, sample, J, alpha) + + # classify test sample + s = [0, 0, 0, 1] + J = ob.winner(weights, s) + +#results + print("Clusters that the test sample belongs to : ", J) + print("Weights that have been trained : ", weights) + +#running the main() function +if __name__ == "__main__": + main() + From 9015ab76495ba4f5275bb27638ba0abce95bcf94 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Oct 2022 14:32:43 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/Self_Organising_Maps.py | 90 ++++++++++++------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/machine_learning/Self_Organising_Maps.py b/machine_learning/Self_Organising_Maps.py index b09e8d1bbc34..f273963b4737 100644 --- a/machine_learning/Self_Organising_Maps.py +++ b/machine_learning/Self_Organising_Maps.py @@ -1,62 +1,62 @@ import math - class SOM: - # Function here computes the winning vector - # by Euclidean distance - def winner(self, weights, sample): - D0 = 0 - D1 = 0 - for i in range(len(sample)): - D0 = D0 + math.pow((sample[i] - weights[0][i]), 2) - D1 = D1 + math.pow((sample[i] - weights[1][i]), 2) - if D0 > D1: - return 0 - else: - return 1 - - # Function here updates the winning vector - def update(self, weights, sample, J, alpha): - for i in range(len(weights)): - weights[J][i] = weights[J][i] + alpha * (sample[i] - weights[J][i]) - return weights + # Function here computes the winning vector + # by Euclidean distance + def winner(self, weights, sample): + D0 = 0 + D1 = 0 + for i in range(len(sample)): + D0 = D0 + math.pow((sample[i] - weights[0][i]), 2) + D1 = D1 + math.pow((sample[i] - weights[1][i]), 2) + if D0 > D1: + return 0 + else: + return 1 + + # Function here updates the winning vector + def update(self, weights, sample, J, alpha): + for i in range(len(weights)): + weights[J][i] = weights[J][i] + alpha * (sample[i] - weights[J][i]) + return weights + # Driver code def main(): - # Training Examples ( m, n ) - T = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] - m, n = len(T), len(T[0]) + # Training Examples ( m, n ) + T = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] + m, n = len(T), len(T[0]) - # weight initialization ( n, C ) - weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] + # weight initialization ( n, C ) + weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] - # training - ob = SOM() - epochs = 3 - alpha = 0.5 + # training + ob = SOM() + epochs = 3 + alpha = 0.5 - for i in range(epochs): - for j in range(m): + for i in range(epochs): + for j in range(m): - # training sample - sample = T[j] + # training sample + sample = T[j] - # Compute winner vector - J = ob.winner(weights, sample) + # Compute winner vector + J = ob.winner(weights, sample) - # Update winning vector - weights = ob.update(weights, sample, J, alpha) + # Update winning vector + weights = ob.update(weights, sample, J, alpha) - # classify test sample - s = [0, 0, 0, 1] - J = ob.winner(weights, s) + # classify test sample + s = [0, 0, 0, 1] + J = ob.winner(weights, s) -#results - print("Clusters that the test sample belongs to : ", J) - print("Weights that have been trained : ", weights) + # results + print("Clusters that the test sample belongs to : ", J) + print("Weights that have been trained : ", weights) -#running the main() function -if __name__ == "__main__": - main() +# running the main() function +if __name__ == "__main__": + main() From ceaeb716ed9bf335bcd5c7aaf4b536bd6f21c3cf Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 20:53:29 +0200 Subject: [PATCH 3/9] Update machine_learning/Self_Organising_Maps.py --- machine_learning/Self_Organising_Maps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/Self_Organising_Maps.py b/machine_learning/Self_Organising_Maps.py index f273963b4737..7dec2975f068 100644 --- a/machine_learning/Self_Organising_Maps.py +++ b/machine_learning/Self_Organising_Maps.py @@ -4,7 +4,7 @@ class SOM: # Function here computes the winning vector # by Euclidean distance - def winner(self, weights, sample): + def winner(self, weights: int, sample: int) --> list[int]: D0 = 0 D1 = 0 for i in range(len(sample)): From df8c8da580ad3c706f48a901e042c34391b148ea Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 21:21:12 +0200 Subject: [PATCH 4/9] Update and rename Self_Organising_Maps.py to self_organizing_map.py --- machine_learning/Self_Organising_Maps.py | 62 -------------------- machine_learning/self_organizing_map.py | 74 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 62 deletions(-) delete mode 100644 machine_learning/Self_Organising_Maps.py create mode 100644 machine_learning/self_organizing_map.py diff --git a/machine_learning/Self_Organising_Maps.py b/machine_learning/Self_Organising_Maps.py deleted file mode 100644 index 7dec2975f068..000000000000 --- a/machine_learning/Self_Organising_Maps.py +++ /dev/null @@ -1,62 +0,0 @@ -import math - - -class SOM: - # Function here computes the winning vector - # by Euclidean distance - def winner(self, weights: int, sample: int) --> list[int]: - D0 = 0 - D1 = 0 - for i in range(len(sample)): - D0 = D0 + math.pow((sample[i] - weights[0][i]), 2) - D1 = D1 + math.pow((sample[i] - weights[1][i]), 2) - if D0 > D1: - return 0 - else: - return 1 - - # Function here updates the winning vector - def update(self, weights, sample, J, alpha): - for i in range(len(weights)): - weights[J][i] = weights[J][i] + alpha * (sample[i] - weights[J][i]) - return weights - - -# Driver code -def main(): - # Training Examples ( m, n ) - T = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] - m, n = len(T), len(T[0]) - - # weight initialization ( n, C ) - weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] - - # training - ob = SOM() - epochs = 3 - alpha = 0.5 - - for i in range(epochs): - for j in range(m): - - # training sample - sample = T[j] - - # Compute winner vector - J = ob.winner(weights, sample) - - # Update winning vector - weights = ob.update(weights, sample, J, alpha) - - # classify test sample - s = [0, 0, 0, 1] - J = ob.winner(weights, s) - - # results - print("Clusters that the test sample belongs to : ", J) - print("Weights that have been trained : ", weights) - - -# running the main() function -if __name__ == "__main__": - main() diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py new file mode 100644 index 000000000000..775d0f2af524 --- /dev/null +++ b/machine_learning/self_organizing_map.py @@ -0,0 +1,74 @@ +""" +https://en.wikipedia.org/wiki/Self-organizing_map +""" +import math + + +class SelfOrganizingMap: + def get_winner(self, weights: list[list[int]], sample: list[int]) -> list[list[int]]: + """ + Compute the winning vector by Euclidean distance + + >>> SelfOrganizingMap().get_winner([[1, 2, 3], [4, 5, 6]], [1, 2, 3]) + 1 + """ + d0 = 0 + d1 = 0 + for i in range(len(sample)): + d0 = d0 + math.pow((sample[i] - weights[0][i]), 2) + d1 = d1 + math.pow((sample[i] - weights[1][i]), 2) + if d0 > d1: + return 0 + else: + return 1 + + def update(self, weights: list[list[int]], sample: list[int], j: int, alpha: float) -> list[list[int | float]]: + """ + Update the winning vector. + + >>> SelfOrganizingMap().update([[1, 2, 3], [4, 5, 6]], [1, 2, 3], 1, 0.1) + [[1, 2, 3], [3.7, 4.7, 6]] + """ + for i in range(len(weights)): + weights[j][i] = weights[j][i] + alpha * (sample[i] - weights[j][i]) + return weights + + +# Driver code +def main(): + # Training Examples ( m, n ) + training_samples = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] + m, n = len(training_samples), len(training_samples[0]) + + # weight initialization ( n, C ) + weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] + + # training + self_organizing_map = SelfOrganizingMap() + epochs = 3 + alpha = 0.5 + + for i in range(epochs): + for j in range(m): + + # training sample + sample = training_samples[j] + + # Compute the winning vector + winner = self_organizing_map.get_winner(weights, sample) + + # Update the winning vector + weights = self_organizing_map.update(weights, sample, winner, alpha) + + # classify test sample + sample = [0, 0, 0, 1] + winner = self_organizing_map.get_winner(weights, sample) + + # results + print(f"Clusters that the test sample belongs to : {winner}") + print(f"Weights that have been trained : {weights}") + + +# running the main() function +if __name__ == "__main__": + main() From 1e6dc55d47a92d8618b4aa11d608fa27a8a32bbc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:22:36 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/self_organizing_map.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py index 775d0f2af524..1a9edd340583 100644 --- a/machine_learning/self_organizing_map.py +++ b/machine_learning/self_organizing_map.py @@ -5,7 +5,9 @@ class SelfOrganizingMap: - def get_winner(self, weights: list[list[int]], sample: list[int]) -> list[list[int]]: + def get_winner( + self, weights: list[list[int]], sample: list[int] + ) -> list[list[int]]: """ Compute the winning vector by Euclidean distance @@ -22,7 +24,9 @@ def get_winner(self, weights: list[list[int]], sample: list[int]) -> list[list[i else: return 1 - def update(self, weights: list[list[int]], sample: list[int], j: int, alpha: float) -> list[list[int | float]]: + def update( + self, weights: list[list[int]], sample: list[int], j: int, alpha: float + ) -> list[list[int | float]]: """ Update the winning vector. From 322d95a192100074152aca41bc163713c45b282a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 21:33:08 +0200 Subject: [PATCH 6/9] Update self_organizing_map.py --- machine_learning/self_organizing_map.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py index 1a9edd340583..8bbe13879a29 100644 --- a/machine_learning/self_organizing_map.py +++ b/machine_learning/self_organizing_map.py @@ -5,27 +5,23 @@ class SelfOrganizingMap: - def get_winner( - self, weights: list[list[int]], sample: list[int] - ) -> list[list[int]]: + def get_winner(self, weights: list[list[int]], sample: list[int]) -> int: """ Compute the winning vector by Euclidean distance >>> SelfOrganizingMap().get_winner([[1, 2, 3], [4, 5, 6]], [1, 2, 3]) 1 """ - d0 = 0 - d1 = 0 + d0 = 0.0 + d1 = 0.0 for i in range(len(sample)): - d0 = d0 + math.pow((sample[i] - weights[0][i]), 2) - d1 = d1 + math.pow((sample[i] - weights[1][i]), 2) - if d0 > d1: - return 0 - else: - return 1 + d0 += math.pow((sample[i] - weights[0][i]), 2) + d1 += math.pow((sample[i] - weights[1][i]), 2) + return 0 if d0 > d1 else 1 + return 0 def update( - self, weights: list[list[int]], sample: list[int], j: int, alpha: float + self, weights: list[list[int | float]], sample: list[int], j: int, alpha: float ) -> list[list[int | float]]: """ Update the winning vector. @@ -34,7 +30,7 @@ def update( [[1, 2, 3], [3.7, 4.7, 6]] """ for i in range(len(weights)): - weights[j][i] = weights[j][i] + alpha * (sample[i] - weights[j][i]) + weights[j][i] += alpha * (sample[i] - weights[j][i]) return weights From 5b2ae6d039dfec854b5750afc08c45b47f204c5a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 21:33:58 +0200 Subject: [PATCH 7/9] Update self_organizing_map.py --- machine_learning/self_organizing_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py index 8bbe13879a29..9b8fa248907b 100644 --- a/machine_learning/self_organizing_map.py +++ b/machine_learning/self_organizing_map.py @@ -35,7 +35,7 @@ def update( # Driver code -def main(): +def main() -> None: # Training Examples ( m, n ) training_samples = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] m, n = len(training_samples), len(training_samples[0]) From f16c1a56dafa4b89dfdb08b77141dc61134fc62a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 21:35:52 +0200 Subject: [PATCH 8/9] Update self_organizing_map.py --- machine_learning/self_organizing_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py index 9b8fa248907b..57c20eddceaa 100644 --- a/machine_learning/self_organizing_map.py +++ b/machine_learning/self_organizing_map.py @@ -5,7 +5,7 @@ class SelfOrganizingMap: - def get_winner(self, weights: list[list[int]], sample: list[int]) -> int: + def get_winner(self, weights: list[list[float]], sample: list[int]) -> int: """ Compute the winning vector by Euclidean distance From 3b463150fd098ac8a6bc23dd32470d509527de83 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 21:38:30 +0200 Subject: [PATCH 9/9] Update self_organizing_map.py --- machine_learning/self_organizing_map.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/machine_learning/self_organizing_map.py b/machine_learning/self_organizing_map.py index 57c20eddceaa..bd3d388f910f 100644 --- a/machine_learning/self_organizing_map.py +++ b/machine_learning/self_organizing_map.py @@ -38,7 +38,6 @@ def update( def main() -> None: # Training Examples ( m, n ) training_samples = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]] - m, n = len(training_samples), len(training_samples[0]) # weight initialization ( n, C ) weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]] @@ -49,7 +48,7 @@ def main() -> None: alpha = 0.5 for i in range(epochs): - for j in range(m): + for j in range(len(training_samples)): # training sample sample = training_samples[j]