<s> import os <EOL> import random <EOL> import operator <EOL> import itertools <EOL> import numpy as np <EOL> import FukuML . Utility as utility <EOL> import FukuML . MLBase as ml <EOL> import FukuML . LinearRegression as linear_regression <EOL> class LogisticRegression ( ml . Learner ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . status = '<STR_LIT>' <EOL> self . train_X = [ ] <EOL> self . train_Y = [ ] <EOL> self . W = [ ] <EOL> self . data_num = <NUM_LIT:0> <EOL> self . data_demension = <NUM_LIT:0> <EOL> self . test_X = [ ] <EOL> self . test_Y = [ ] <EOL> self . feature_transform_mode = '<STR_LIT>' <EOL> self . feature_transform_degree = <NUM_LIT:1> <EOL> self . feed_mode = '<STR_LIT>' <EOL> self . step_ita = <NUM_LIT> <EOL> self . updates = <NUM_LIT> <EOL> def load_train_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . status = '<STR_LIT>' <EOL> if ( input_data_file == '<STR_LIT>' ) : <EOL> input_data_file = os . path . normpath ( os . path . join ( os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) , "<STR_LIT>" ) ) <EOL> else : <EOL> if ( os . path . isfile ( input_data_file ) is not True ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . train_X , self . train_Y <EOL> self . train_X , self . train_Y = utility . DatasetLoader . load ( input_data_file ) <EOL> return self . train_X , self . train_Y <EOL> def load_test_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if ( input_data_file == '<STR_LIT>' ) : <EOL> input_data_file = os . path . normpath ( os . path . join ( os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) , "<STR_LIT>" ) ) <EOL> else : <EOL> if ( os . path . isfile ( input_data_file ) is not True ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . test_X , self . test_Y <EOL> self . test_X , self . test_Y = utility . DatasetLoader . load ( input_data_file ) <EOL> if ( self . feature_transform_mode == '<STR_LIT>' ) or ( self . feature_transform_mode == '<STR_LIT>' ) : <EOL> self . test_X = self . test_X [ : , <NUM_LIT:1> : ] <EOL> self . test_X = utility . DatasetLoader . featureTransform ( <EOL> self . test_X , <EOL> self . feature_transform_mode , <EOL> self . feature_transform_degree <EOL> ) <EOL> return self . test_X , self . test_Y <EOL> def setParam ( self , feed_mode = '<STR_LIT>' , step_ita = <NUM_LIT> , updates = <NUM_LIT> ) : <EOL> self . feed_mode = feed_mode <EOL> self . step_ita = step_ita <EOL> self . updates = updates <EOL> return self . feed_mode , self . step_ita , self . updates <EOL> def init_W ( self , mode = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if ( self . status != '<STR_LIT>' ) and ( self . status != '<STR_LIT:train>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . W <EOL> self . status = '<STR_LIT>' <EOL> self . data_num = len ( self . train_Y ) <EOL> self . data_demension = len ( self . train_X [ <NUM_LIT:0> ] ) <EOL> self . W = np . zeros ( self . data_demension ) <EOL> if mode == '<STR_LIT>' : <EOL> accelerator = linear_regression . Accelerator ( ) <EOL> self . W = accelerator . init_W ( self ) <EOL> return self . W <EOL> def theta ( self , s ) : <EOL> '''<STR_LIT>''' <EOL> s = np . where ( s < - <NUM_LIT> , - <NUM_LIT> , s ) <EOL> return <NUM_LIT:1> / ( <NUM_LIT:1> + np . exp ( ( - <NUM_LIT:1> ) * s ) ) <EOL> def score_function ( self , x , W ) : <EOL> '''<STR_LIT>''' <EOL> score = self . theta ( np . inner ( x , W ) ) <EOL> return score <EOL> def error_function ( self , x , y , W ) : <EOL> '''<STR_LIT>''' <EOL> error = np . log ( <NUM_LIT:1> + np . exp ( ( - <NUM_LIT:1> ) * y * np . inner ( x , W ) ) ) <EOL> return error <EOL> def calculate_gradient ( self , X , Y , W ) : <EOL> if type ( Y ) is np . ndarray : <EOL> data_num = len ( Y ) <EOL> else : <EOL> data_num = <NUM_LIT:1> <EOL> gradient_average = np . dot ( self . theta ( ( - <NUM_LIT:1> ) * Y * np . dot ( W , X . transpose ( ) ) ) * ( ( - <NUM_LIT:1> ) * Y ) , X ) / data_num <EOL> return gradient_average <EOL> def calculate_avg_error ( self , X , Y , W ) : <EOL> '''<STR_LIT>''' <EOL> data_num = len ( Y ) <EOL> error_num = <NUM_LIT:0> <EOL> for i in range ( data_num ) : <EOL> error_num = error_num + self . error_function ( X [ i ] , Y [ i ] , W ) <EOL> avg_error = error_num / float ( data_num ) <EOL> return avg_error <EOL> def calculate_test_data_avg_error ( self ) : <EOL> '''<STR_LIT>''' <EOL> avg_error = self . calculate_avg_error ( self . test_X , self . test_Y , self . W ) <EOL> return avg_error <EOL> def train ( self ) : <EOL> '''<STR_LIT>''' <EOL> if ( self . status != '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . W <EOL> self . status = '<STR_LIT:train>' <EOL> for i in range ( <NUM_LIT:0> , self . updates ) : <EOL> if self . feed_mode == '<STR_LIT>' : <EOL> stochastic_i = random . randint ( <NUM_LIT:0> , self . data_num - <NUM_LIT:1> ) <EOL> x = self . train_X [ stochastic_i ] <EOL> y = self . train_Y [ stochastic_i ] <EOL> gradient = self . calculate_gradient ( x , y , self . W ) <EOL> else : <EOL> gradient = self . calculate_gradient ( self . train_X , self . train_Y , self . W ) <EOL> if np . linalg . norm ( gradient ) == <NUM_LIT:0> : <EOL> return self . W <EOL> self . W = self . W - self . step_ita * gradient <EOL> return self . W <EOL> def prediction ( self , input_data = '<STR_LIT>' , mode = '<STR_LIT>' ) : <EOL> return super ( LogisticRegression , self ) . prediction ( input_data , mode ) <EOL> class BinaryClassifier ( LogisticRegression ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . status = '<STR_LIT>' <EOL> self . train_X = [ ] <EOL> self . train_Y = [ ] <EOL> self . W = [ ] <EOL> self . data_num = <NUM_LIT:0> <EOL> self . data_demension = <NUM_LIT:0> <EOL> self . test_X = [ ] <EOL> self . test_Y = [ ] <EOL> self . feature_transform_mode = '<STR_LIT>' <EOL> self . feature_transform_degree = <NUM_LIT:1> <EOL> self . feed_mode = '<STR_LIT>' <EOL> self . step_ita = <NUM_LIT> <EOL> self . updates = <NUM_LIT> <EOL> def load_train_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> return super ( BinaryClassifier , self ) . load_train_data ( input_data_file ) <EOL> def load_test_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> return super ( BinaryClassifier , self ) . load_test_data ( input_data_file ) <EOL> def setParam ( self , feed_mode = '<STR_LIT>' , step_ita = <NUM_LIT> , updates = <NUM_LIT> ) : <EOL> return super ( BinaryClassifier , self ) . setParam ( feed_mode , step_ita , updates ) <EOL> def init_W ( self , mode = '<STR_LIT>' ) : <EOL> return super ( BinaryClassifier , self ) . init_W ( mode ) <EOL> def theta ( self , s ) : <EOL> return super ( BinaryClassifier , self ) . theta ( s ) <EOL> def score_function ( self , x , W ) : <EOL> '''<STR_LIT>''' <EOL> score = super ( BinaryClassifier , self ) . score_function ( x , W ) <EOL> if score >= <NUM_LIT:0.5> : <EOL> score = <NUM_LIT:1.0> <EOL> else : <EOL> score = - <NUM_LIT:1.0> <EOL> return score <EOL> def error_function ( self , y_prediction , y_truth ) : <EOL> '''<STR_LIT>''' <EOL> if y_prediction != y_truth : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def calculate_gradient ( self , X , Y , W ) : <EOL> return super ( BinaryClassifier , self ) . calculate_gradient ( X , Y , W ) <EOL> def calculate_avg_error ( self , X , Y , W ) : <EOL> data_num = len ( Y ) <EOL> error_num = <NUM_LIT:0> <EOL> for i in range ( data_num ) : <EOL> error_num = error_num + self . error_function ( self . score_function ( X [ i ] , W ) , Y [ i ] ) <EOL> avg_error = error_num / float ( data_num ) <EOL> return avg_error <EOL> def calculate_test_data_avg_error ( self ) : <EOL> return super ( BinaryClassifier , self ) . calculate_avg_error ( ) <EOL> def train ( self ) : <EOL> return super ( BinaryClassifier , self ) . train ( ) <EOL> def prediction ( self , input_data = '<STR_LIT>' , mode = '<STR_LIT>' ) : <EOL> return super ( BinaryClassifier , self ) . prediction ( input_data , mode ) <EOL> class MultiClassifier ( LogisticRegression ) : <EOL> class_list = [ ] <EOL> temp_train_X = [ ] <EOL> temp_train_Y = [ ] <EOL> temp_W = { } <EOL> temp_data_num = <NUM_LIT:0> <EOL> decomposition = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . status = '<STR_LIT>' <EOL> self . train_X = [ ] <EOL> self . train_Y = [ ] <EOL> self . W = { } <EOL> self . data_num = <NUM_LIT:0> <EOL> self . data_demension = <NUM_LIT:0> <EOL> self . test_X = [ ] <EOL> self . test_Y = [ ] <EOL> self . feature_transform_mode = '<STR_LIT>' <EOL> self . feature_transform_degree = <NUM_LIT:1> <EOL> self . feed_mode = '<STR_LIT>' <EOL> self . step_ita = <NUM_LIT> <EOL> self . updates = <NUM_LIT> <EOL> self . class_list = [ ] <EOL> self . temp_train_X = [ ] <EOL> self . temp_train_Y = [ ] <EOL> self . temp_W = { } <EOL> self . temp_data_num = <NUM_LIT:0> <EOL> self . decomposition = '<STR_LIT>' <EOL> def load_train_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> self . status = '<STR_LIT>' <EOL> if ( input_data_file == '<STR_LIT>' ) : <EOL> input_data_file = os . path . normpath ( os . path . join ( os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) , "<STR_LIT>" ) ) <EOL> else : <EOL> if ( os . path . isfile ( input_data_file ) is not True ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . train_X , self . train_Y <EOL> self . train_X , self . train_Y = utility . DatasetLoader . load ( input_data_file ) <EOL> return self . train_X , self . train_Y <EOL> def load_test_data ( self , input_data_file = '<STR_LIT>' ) : <EOL> if ( input_data_file == '<STR_LIT>' ) : <EOL> input_data_file = os . path . normpath ( os . path . join ( os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) , "<STR_LIT>" ) ) <EOL> else : <EOL> if ( os . path . isfile ( input_data_file ) is not True ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . test_X , self . test_Y <EOL> self . test_X , self . test_Y = utility . DatasetLoader . load ( input_data_file ) <EOL> if ( self . feature_transform_mode == '<STR_LIT>' ) or ( self . feature_transform_mode == '<STR_LIT>' ) : <EOL> self . test_X = self . test_X [ : , <NUM_LIT:1> : ] <EOL> self . test_X = utility . DatasetLoader . featureTransform ( <EOL> self . test_X , <EOL> self . feature_transform_mode , <EOL> self . feature_transform_degree <EOL> ) <EOL> return self . test_X , self . test_Y <EOL> def setParam ( self , feed_mode = '<STR_LIT>' , step_ita = <NUM_LIT> , updates = <NUM_LIT> ) : <EOL> return super ( MultiClassifier , self ) . setParam ( feed_mode , step_ita , updates ) <EOL> def init_W ( self , mode = '<STR_LIT>' , decomposition = '<STR_LIT>' ) : <EOL> self . W = { } <EOL> if ( self . status != '<STR_LIT>' ) and ( self . status != '<STR_LIT:train>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . W <EOL> self . status = '<STR_LIT>' <EOL> self . data_num = len ( self . train_Y ) <EOL> self . data_demension = len ( self . train_X [ <NUM_LIT:0> ] ) <EOL> self . decomposition = decomposition <EOL> if self . decomposition == '<STR_LIT>' : <EOL> self . class_list = list ( itertools . combinations ( np . unique ( self . train_Y ) , <NUM_LIT:2> ) ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> self . class_list = np . unique ( self . train_Y ) <EOL> for class_item in self . class_list : <EOL> self . W [ class_item ] = np . zeros ( self . data_demension ) <EOL> if mode == '<STR_LIT>' : <EOL> accelerator = linear_regression . Accelerator ( ) <EOL> for class_item in self . class_list : <EOL> if self . decomposition == '<STR_LIT>' : <EOL> modify_X , modify_Y = self . modify_XY ( self . train_X , self . train_Y , class_item ) <EOL> self . temp_train_X = self . train_X <EOL> self . temp_train_Y = self . train_Y <EOL> self . train_X = modify_X <EOL> self . train_Y = modify_Y <EOL> self . temp_data_num = self . data_num <EOL> self . data_num = len ( self . train_Y ) <EOL> self . temp_W = self . W <EOL> self . W = self . temp_W [ class_item ] <EOL> self . temp_W [ class_item ] = accelerator . init_W ( self ) <EOL> self . train_X = self . temp_train_X <EOL> self . train_Y = self . temp_train_Y <EOL> self . temp_train_X = [ ] <EOL> self . temp_train_Y = [ ] <EOL> self . data_num = self . temp_data_num <EOL> self . temp_data_num = <NUM_LIT:0> <EOL> self . W = self . temp_W <EOL> self . temp_W = { } <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> modify_Y = self . modify_Y ( self . train_Y , class_item ) <EOL> self . temp_train_Y = self . train_Y <EOL> self . train_Y = modify_Y <EOL> self . temp_W = self . W <EOL> self . W = self . temp_W [ class_item ] <EOL> self . temp_W [ class_item ] = accelerator . init_W ( self ) <EOL> self . train_Y = self . temp_train_Y <EOL> self . temp_train_Y = [ ] <EOL> self . W = self . temp_W <EOL> self . temp_W = { } <EOL> return self . W <EOL> def theta ( self , s ) : <EOL> return super ( MultiClassifier , self ) . theta ( s ) <EOL> def score_function ( self , x , W ) : <EOL> return super ( MultiClassifier , self ) . score_function ( x , W ) <EOL> def score_function_all_class ( self , x , W ) : <EOL> score_list = { } <EOL> ovo_vote = [ ] <EOL> for class_item in self . class_list : <EOL> score = self . score_function ( x , W [ class_item ] ) <EOL> if self . decomposition == '<STR_LIT>' : <EOL> if score >= <NUM_LIT:0.5> : <EOL> score_list [ class_item ] = class_item [ <NUM_LIT:0> ] <EOL> else : <EOL> score_list [ class_item ] = class_item [ <NUM_LIT:1> ] <EOL> ovo_vote . append ( score_list [ class_item ] ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> score_list [ class_item ] = score <EOL> if self . decomposition == '<STR_LIT>' : <EOL> return max ( set ( ovo_vote ) , key = ovo_vote . count ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> return max ( score_list . items ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> def error_function ( self , y_prediction , y_truth ) : <EOL> if y_prediction != y_truth : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def calculate_gradient ( self , X , Y , W ) : <EOL> return super ( MultiClassifier , self ) . calculate_gradient ( X , Y , W ) <EOL> def calculate_avg_error ( self , X , Y , W ) : <EOL> return super ( MultiClassifier , self ) . calculate_avg_error ( X , Y , W ) <EOL> def calculate_avg_error_all_class ( self , X , Y , W ) : <EOL> data_num = len ( Y ) <EOL> error_num = <NUM_LIT:0> <EOL> for i in range ( data_num ) : <EOL> error_num = error_num + self . error_function ( self . score_function_all_class ( X [ i ] , W ) , Y [ i ] ) <EOL> avg_error = error_num / float ( data_num ) <EOL> return avg_error <EOL> def calculate_test_data_avg_error ( self ) : <EOL> return super ( MultiClassifier , self ) . calculate_avg_error ( ) <EOL> def modify_Y ( self , Y , class_item ) : <EOL> modify_Y = [ ] <EOL> for yi in Y : <EOL> if yi == class_item : <EOL> modify_Y . append ( <NUM_LIT:1> ) <EOL> else : <EOL> modify_Y . append ( - <NUM_LIT:1> ) <EOL> return np . array ( modify_Y ) <EOL> def modify_XY ( self , X , Y , class_item ) : <EOL> modify_X = [ ] <EOL> modify_Y = [ ] <EOL> for idx , val in enumerate ( Y ) : <EOL> if val == class_item [ <NUM_LIT:0> ] : <EOL> modify_Y . append ( <NUM_LIT:1> ) <EOL> modify_X . append ( X [ idx ] ) <EOL> elif val == class_item [ <NUM_LIT:1> ] : <EOL> modify_Y . append ( - <NUM_LIT:1> ) <EOL> modify_X . append ( X [ idx ] ) <EOL> return np . array ( modify_X ) , np . array ( modify_Y ) <EOL> def train ( self ) : <EOL> if ( self . status != '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> return self . W <EOL> for class_item in self . class_list : <EOL> self . status = '<STR_LIT>' <EOL> if self . decomposition == '<STR_LIT>' : <EOL> modify_X , modify_Y = self . modify_XY ( self . train_X , self . train_Y , class_item ) <EOL> self . temp_train_X = self . train_X <EOL> self . temp_train_Y = self . train_Y <EOL> self . train_X = modify_X <EOL> self . train_Y = modify_Y <EOL> self . temp_data_num = self . data_num <EOL> self . data_num = len ( self . train_Y ) <EOL> self . temp_W = self . W <EOL> self . W = self . temp_W [ class_item ] <EOL> self . temp_W [ class_item ] = super ( MultiClassifier , self ) . train ( ) <EOL> self . train_X = self . temp_train_X <EOL> self . train_Y = self . temp_train_Y <EOL> self . temp_train_X = [ ] <EOL> self . temp_train_Y = [ ] <EOL> self . data_num = self . temp_data_num <EOL> self . temp_data_num = <NUM_LIT:0> <EOL> self . W = self . temp_W <EOL> self . temp_W = { } <EOL> print ( "<STR_LIT>" % ( class_item [ <NUM_LIT:0> ] , class_item [ <NUM_LIT:1> ] ) ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> modify_Y = self . modify_Y ( self . train_Y , class_item ) <EOL> self . temp_train_Y = self . train_Y <EOL> self . train_Y = modify_Y <EOL> self . temp_W = self . W <EOL> self . W = self . temp_W [ class_item ] <EOL> self . temp_W [ class_item ] = super ( MultiClassifier , self ) . train ( ) <EOL> self . train_Y = self . temp_train_Y <EOL> self . temp_train_Y = [ ] <EOL> self . W = self . temp_W <EOL> self . temp_W = { } <EOL> print ( "<STR_LIT>" % class_item ) <EOL> self . status = '<STR_LIT:train>' <EOL> return self . W <EOL> def prediction ( self , input_data = '<STR_LIT>' , mode = '<STR_LIT>' ) : <EOL> prediction = { } <EOL> prediction_list = { } <EOL> prediction_return = <NUM_LIT:0.0> <EOL> ovo_vote = [ ] <EOL> for class_item in self . class_list : <EOL> self . temp_W = self . W <EOL> self . W = self . temp_W [ class_item ] <EOL> prediction = super ( MultiClassifier , self ) . prediction ( input_data , mode ) <EOL> if self . decomposition == '<STR_LIT>' : <EOL> if prediction [ '<STR_LIT>' ] >= <NUM_LIT:0.5> : <EOL> prediction_list [ class_item ] = class_item [ <NUM_LIT:0> ] <EOL> else : <EOL> prediction_list [ class_item ] = class_item [ <NUM_LIT:1> ] <EOL> ovo_vote . append ( prediction_list [ class_item ] ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> prediction_list [ class_item ] = prediction [ '<STR_LIT>' ] <EOL> self . W = self . temp_W <EOL> self . temp_W = { } <EOL> if self . decomposition == '<STR_LIT>' : <EOL> prediction_return = max ( set ( ovo_vote ) , key = ovo_vote . count ) <EOL> elif self . decomposition == '<STR_LIT>' : <EOL> prediction_return = max ( prediction_list . items ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) ) [ <NUM_LIT:0> ] <EOL> return { <EOL> "<STR_LIT>" : prediction [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : prediction [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : prediction_return , <EOL> "<STR_LIT>" : prediction_list , <EOL> } </s>
