

"""simple docstring"""






import math







class 	__UpperCamelCase    :

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Dict  =    0.0
      _lowerCAmelCase       :   Tuple  =    0.0
      for i in range(len(__a      )      ):
          da += math.pow((sample[i] - weights[0][i])    ,2      )
          da += math.pow((sample[i] - weights[1][i])    ,2      )
          return 0 if da > da else 1
      return 0







  def __lowerCamelCase		(		self    ,_A    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      for i in range(len(__a      )      ):
          weights[j][i] += alpha * (sample[i] - weights[j][i])
      return weights





def 		lowerCamelCase__		(					):

    '''simple docstring'''



    _lowerCAmelCase       :   Union[str, Any]  =    [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

    # weight initialization ( n, C )
    _lowerCAmelCase       :   int  =    [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

    # training
    _lowerCAmelCase       :   Optional[int]  =    SelfOrganizingMap()
    _lowerCAmelCase       :   List[Any]  =    3
    _lowerCAmelCase       :   Tuple  =    0.5

    for _ in range(UpperCamelCase__   ):
        for j in range(len(UpperCamelCase__   )   ):
            # training sample
            _lowerCAmelCase       :   Any  =    training_samples[j]

            # Compute the winning vector
            _lowerCAmelCase       :   Optional[int]  =    self_organizing_map.get_winner(UpperCamelCase__ ,   UpperCamelCase__   )

            # Update the winning vector
            _lowerCAmelCase       :   List[Any]  =    self_organizing_map.update(UpperCamelCase__ ,   UpperCamelCase__ ,   UpperCamelCase__ ,   UpperCamelCase__   )

    # classify test sample
    _lowerCAmelCase       :   Dict  =    [0, 0, 0, 1]
    _lowerCAmelCase       :   Dict  =    self_organizing_map.get_winner(UpperCamelCase__ ,   UpperCamelCase__   )

    # 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()








"""simple docstring"""






from collections.abc import Callable







class 	__UpperCamelCase    :

  def __init__(		self    ,_A = None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   list  =    []
      # Stores indexes of each item for supporting updates and deletion.
      _lowerCAmelCase       :   dict  =    {}
      # Stores current size of heap.
      _lowerCAmelCase       :   Union[str, Any]  =    0
      # Stores function used to evaluate the score of an item on which basis ordering
      # will be done.
      _lowerCAmelCase       :   Union[str, Any]  =    key or (lambda _A      : x)

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      return int((i - 1) / 2      ) if i > 0 else None

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    int(2 * i + 1      )
      return left if 0 < left < self.size else None

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    int(2 * i + 2      )
      return right if 0 < right < self.size else None

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase,					_lowerCAmelCase       :   Tuple  =    (
          self.pos_map[self.arr[j][0]],
          self.pos_map[self.arr[i][0]],
      )
      # Then swap the items in the list.
      _lowerCAmelCase,					_lowerCAmelCase       :   Tuple  =    self.arr[j], self.arr[i]

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      return self.arr[i][1] < self.arr[j][1]

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    self._left(_A      )
      _lowerCAmelCase       :   str  =    self._right(_A      )
      _lowerCAmelCase       :   Tuple  =    i

      if left is not None and not self._cmp(_A    ,_A      ):
          _lowerCAmelCase       :   int  =    left
      if right is not None and not self._cmp(_A    ,_A      ):
          _lowerCAmelCase       :   Optional[int]  =    right

      return valid_parent

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Any  =    self._parent(_A      )
      while parent is not None and not self._cmp(_A    ,_A      ):
          self._swap(_A    ,_A      )
          _lowerCAmelCase,					_lowerCAmelCase       :   List[str]  =    parent, self._parent(_A      )

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    self._get_valid_parent(_A      )
      while valid_parent != index:
          self._swap(_A    ,_A      )
          _lowerCAmelCase,					_lowerCAmelCase       :   Optional[int]  =    valid_parent, self._get_valid_parent(_A      )

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      if item not in self.pos_map:
          return
      _lowerCAmelCase       :   int  =    self.pos_map[item]
      _lowerCAmelCase       :   Dict  =    [item, self.key(_A      )]
      # Make sure heap is right in both up and down direction.
      # Ideally only one of them will make any change.
      self._heapify_up(_A      )
      self._heapify_down(_A      )

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      if item not in self.pos_map:
          return
      _lowerCAmelCase       :   List[str]  =    self.pos_map[item]
      del self.pos_map[item]
      _lowerCAmelCase       :   Dict  =    self.arr[self.size - 1]
      _lowerCAmelCase       :   Optional[Any]  =    index
      self.size -= 1
      # Make sure heap is right in both up and down direction. Ideally only one
      # of them will make any change- so no performance loss in calling both.
      if self.size > index:
          self._heapify_up(_A      )
          self._heapify_down(_A      )

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    len(self.arr      )
      if arr_len == self.size:
          self.arr.append([item, self.key(_A      )]      )
      else:
          _lowerCAmelCase       :   Any  =    [item, self.key(_A      )]
      _lowerCAmelCase       :   str  =    self.size
      self.size += 1
      self._heapify_up(self.size - 1      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      return self.arr[0] if self.size else None







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   int  =    self.get_top()
      if top_item_tuple:
          self.delete_item(top_item_tuple[0]      )
      return top_item_tuple





def 		lowerCamelCase__		(					):

    '''simple docstring'''





if __name__ == "__main__":
 import doctest

 doctest.testmod()





