<s> from __future__ import division <EOL> from numpy import ( zeros , argmin , argwhere , take , sum as np_sum , <EOL> any as np_any , abs as np_abs ) <EOL> from numpy . linalg import norm <EOL> from neupy . utils import format_data <EOL> from neupy . core . properties import IntProperty <EOL> from neupy . algorithms . gd import NoStepSelection <EOL> from neupy . network . base import BaseNetwork <EOL> from neupy . network . learning import UnsupervisedLearning <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class RBFKMeans ( NoStepSelection , UnsupervisedLearning , BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> n_clusters = IntProperty ( minval = <NUM_LIT:2> ) <EOL> def __init__ ( self , ** options ) : <EOL> self . centers = None <EOL> super ( RBFKMeans , self ) . __init__ ( ** options ) <EOL> def predict ( self , input_data ) : <EOL> input_data = format_data ( input_data ) <EOL> centers = self . centers <EOL> classes = zeros ( ( input_data . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> for i , value in enumerate ( input_data ) : <EOL> classes [ i ] = argmin ( norm ( centers - value , axis = <NUM_LIT:1> ) ) <EOL> return classes <EOL> def train_epoch ( self , input_train , target_train ) : <EOL> centers = self . centers <EOL> old_centers = centers . copy ( ) <EOL> output_train = self . predict ( input_train ) <EOL> for i , center in enumerate ( centers ) : <EOL> positions = argwhere ( output_train [ : , <NUM_LIT:0> ] == i ) <EOL> if not np_any ( positions ) : <EOL> continue <EOL> class_data = take ( input_train , positions , axis = <NUM_LIT:0> ) <EOL> centers [ i , : ] = ( <NUM_LIT:1> / len ( class_data ) ) * np_sum ( class_data , axis = <NUM_LIT:0> ) <EOL> return np_abs ( old_centers - centers ) <EOL> def train ( self , input_train , epsilon = <NUM_LIT> , epochs = <NUM_LIT:100> ) : <EOL> n_clusters = self . n_clusters <EOL> input_train = format_data ( input_train ) <EOL> if input_train . shape [ <NUM_LIT:0> ] <= n_clusters : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . centers = input_train [ : n_clusters , : ] . copy ( ) <EOL> super ( RBFKMeans , self ) . train ( input_train , epsilon = epsilon , <EOL> epochs = epochs ) </s>
