MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / SelfOrganizingMap

Class SelfOrganizingMap

machine_learning/self_organizing_map.py:8–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7
8class SelfOrganizingMap:
9 def get_winner(self, weights: list[list[float]], sample: list[int]) -> int:
10 """
11 Compute the winning vector by Euclidean distance
12
13 >>> SelfOrganizingMap().get_winner([[1, 2, 3], [4, 5, 6]], [1, 2, 3])
14 1
15 """
16 d0 = 0.0
17 d1 = 0.0
18 for i in range(len(sample)):
19 d0 += math.pow((sample[i] - weights[0][i]), 2)
20 d1 += math.pow((sample[i] - weights[1][i]), 2)
21 return 0 if d0 > d1 else 1
22 return 0
23
24 def update(
25 self, weights: list[list[int | float]], sample: list[int], j: int, alpha: float
26 ) -> list[list[int | float]]:
27 """
28 Update the winning vector.
29
30 >>> SelfOrganizingMap().update([[1, 2, 3], [4, 5, 6]], [1, 2, 3], 1, 0.1)
31 [[1, 2, 3], [3.7, 4.7, 6]]
32 """
33 for i in range(len(weights)):
34 weights[j][i] += alpha * (sample[i] - weights[j][i])
35 return weights
36
37
38# Driver code

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected