

import math






class  UpperCAmelCase__	:






  """simple docstring"""


  def    lowercase_	(							self		:	int			,     __lowerCamelCase		:	list[list[float]]			,     __lowerCamelCase		:	list[int]      )	->							int:
       SCREAMING_SNAKE_CASE__					=					0.0
       SCREAMING_SNAKE_CASE__					=					0.0
       for i in range(len(__lowerCamelCase      )      ):
            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    lowercase_	(							self		:	Optional[int]			,     __lowerCamelCase		:	list[list[int | float]]			,     __lowerCamelCase		:	list[int]			,     __lowerCamelCase		:	int			,     __lowerCamelCase		:	float      )	->							list[list[int | float]]:
       for i in range(len(__lowerCamelCase      )      ):
            weights[j][i] += alpha * (sample[i] - weights[j][i])
       return weights



def        UpperCAmelCase_     (		):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

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

     # training
     SCREAMING_SNAKE_CASE__					=					SelfOrganizingMap()
     SCREAMING_SNAKE_CASE__					=					3
     SCREAMING_SNAKE_CASE__					=					0.5

     for _ in range(_A       ):
          for j in range(len(_A       )       ):
               # training sample
               SCREAMING_SNAKE_CASE__					=					training_samples[j]

               # Compute the winning vector
               SCREAMING_SNAKE_CASE__					=					self_organizing_map.get_winner(_A   ,	_A       )

               # Update the winning vector
               SCREAMING_SNAKE_CASE__					=					self_organizing_map.update(_A   ,	_A   ,	_A   ,	_A       )

    # classify test sample
     SCREAMING_SNAKE_CASE__					=					[0, 0, 0, 1]
     SCREAMING_SNAKE_CASE__					=					self_organizing_map.get_winner(_A   ,	_A       )

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





def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[]
     for data in source_data:
          for i, el in enumerate(_A       ):
               if len(_A       ) < i + 1:
                    data_lists.append([]       )
               data_lists[i].append(float(_A       )       )
     return data_lists
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[]
     for dlist, weight in zip(_A   ,	_A       ):
          SCREAMING_SNAKE_CASE__					=					min(_A       )
          SCREAMING_SNAKE_CASE__					=					max(_A       )

          SCREAMING_SNAKE_CASE__					=					[]
          # for weight 0 score is 1 - actual score
          if weight == 0:
               for item in dlist:
                    try:
                         score.append(1 - ((item - mind) / (maxd - mind))       )
                    except ZeroDivisionError:
                         score.append(1       )

          elif weight == 1:
               for item in dlist:
                    try:
                         score.append((item - mind) / (maxd - mind)       )
                    except ZeroDivisionError:
                         score.append(0       )

        # weight not 0 or 1
          else:
               SCREAMING_SNAKE_CASE__					=					F'''Invalid weight of {weight:f} provided'''
               raise ValueError(_A       )

          score_lists.append(_A       )

     return score_lists
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[0 for i in range(len(score_lists[0]       )       )]

     for slist in score_lists:
          for j, ele in enumerate(_A       ):
               SCREAMING_SNAKE_CASE__					=					final_scores[j] + ele

     return final_scores







def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					get_data(_A       )
     SCREAMING_SNAKE_CASE__					=					calculate_each_score(_A   ,	_A       )
     SCREAMING_SNAKE_CASE__					=					generate_final_scores(_A       )

     # append scores to source data
     for i, ele in enumerate(_A       ):
          source_data[i].append(_A       )

     return source_data


