#NEWLINE# def train( self , input_data ) : #NEWLINE# #INDENT# input_data = format_data ( input_data ) #NEWLINE# if ( input_data . ndim != 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'Input value must be 2 dimentional, got {0}' . format ( input_data . ndim ) ) #NEWLINE# #UNINDENT# data_size = input_data . shape [ 1 ] #NEWLINE# n_clusters = self . n_clusters #NEWLINE# step = self . step #NEWLINE# rho = self . rho #NEWLINE# if ( list ( sort ( unique ( input_data ) ) ) != [ 0 , 1 ] ) : #NEWLINE# #INDENT# raise ValueError ( 'ART1 Network works only with binary matrix, all matix must contains only 0 and 1' ) #NEWLINE# #UNINDENT# if ( not hasattr ( self , 'weight_21' ) ) : #NEWLINE# #INDENT# self . weight_21 = ones ( ( data_size , n_clusters ) ) #NEWLINE# #UNINDENT# if ( not hasattr ( self , 'weight_12' ) ) : #NEWLINE# #INDENT# self . weight_12 = ( ( step / ( ( step + n_clusters ) - 1 ) ) * self . weight_21 . T ) #NEWLINE# #UNINDENT# weight_21 = self . weight_21 #NEWLINE# weight_12 = self . weight_12 #NEWLINE# if ( data_size != weight_21 . shape [ 0 ] ) : #NEWLINE# #INDENT# raise ValueError ( 'Data dimention is invalid. Get {} columns data set. Must be - {} columns' . format ( data_size , weight_21 . shape [ 0 ] ) ) #NEWLINE# #UNINDENT# classes = zeros ( input_data . shape [ 0 ] ) #NEWLINE# for ( i , p ) in enumerate ( input_data ) : #NEWLINE# #INDENT# disabled_neurons = [ ] #NEWLINE# reseted_values = [ ] #NEWLINE# reset = True #NEWLINE# while reset : #NEWLINE# #INDENT# output1 = p #NEWLINE# input2 = dot ( weight_12 , output1 . T ) #NEWLINE# output2 = zeros ( input2 . size ) #NEWLINE# input2 [ disabled_neurons ] = ( - inf ) #NEWLINE# winner_index = input2 . argmax ( ) #NEWLINE# output2 [ winner_index ] = 1 #NEWLINE# expectation = dot ( weight_21 , output2 ) #NEWLINE# output1 = logical_and ( p , expectation ) . astype ( int ) #NEWLINE# reset_value = ( dot ( output1 . T , output1 ) / dot ( p . T , p ) ) #NEWLINE# reset = ( reset_value < rho ) #NEWLINE# if reset : #NEWLINE# #INDENT# disabled_neurons . append ( winner_index ) #NEWLINE# reseted_values . append ( ( reset_value , winner_index ) ) #NEWLINE# #UNINDENT# if ( len ( rho ) >= n_clusters ) : #NEWLINE# #INDENT# reset = False #NEWLINE# winner_index = None #NEWLINE# #UNINDENT# if ( not reset ) : #NEWLINE# #INDENT# if ( winner_index is not None ) : #NEWLINE# #INDENT# weight_12 [ winner_index , : ] = ( ( step * output1 ) / ( ( step + dot ( output1 . T , output1 ) ) - 1 ) ) #NEWLINE# weight_21 [ : , winner_index ] = output1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# winner_index = max ( reseted_values ) [ 1 ] #NEWLINE# #UNINDENT# classes [ i ] = winner_index #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return classes