<s> import numpy as np <EOL> import numpy <EOL> import random <EOL> FEATURE_NUMBER_step = <NUM_LIT:1> <EOL> SPLIT_VAL_step = <NUM_LIT:2> <EOL> LEFT_POINTER = <NUM_LIT:3> <EOL> RIGHT_POINTER = <NUM_LIT:4> <EOL> serial_no = <NUM_LIT:0> <EOL> count = <NUM_LIT:0> <EOL> class Randomforestlearner ( object ) : <EOL> def __init__ ( self , k ) : <EOL> super ( Randomforestlearner , self ) . __init__ ( ) <EOL> self . k = k <EOL> def split ( self , arr , cond ) : <EOL> return [ arr [ cond ] , arr [ ~ cond ] ] <EOL> def combine_tree ( self , current , left_subtree , right_subtree ) : <EOL> if type ( left_subtree [ <NUM_LIT:0> ] ) == numpy . float64 and type ( right_subtree [ <NUM_LIT:0> ] ) == numpy . float64 : <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] = left_subtree [ <NUM_LIT:0> ] <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:4> ] = right_subtree [ <NUM_LIT:0> ] <EOL> elif type ( left_subtree [ <NUM_LIT:0> ] ) == numpy . float64 and type ( right_subtree [ <NUM_LIT:0> ] ) == numpy . ndarray : <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] = left_subtree [ <NUM_LIT:0> ] <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:4> ] = right_subtree [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> elif type ( left_subtree [ <NUM_LIT:0> ] ) == numpy . ndarray and type ( right_subtree [ <NUM_LIT:0> ] ) == numpy . float64 : <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] = left_subtree [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:4> ] = right_subtree [ <NUM_LIT:0> ] <EOL> else : <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] = left_subtree [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> current [ <NUM_LIT:0> ] [ <NUM_LIT:4> ] = right_subtree [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> return numpy . vstack ( ( current , numpy . vstack ( ( left_subtree , right_subtree ) ) ) ) <EOL> def buildtree ( self , Trainset ) : <EOL> global serial_no <EOL> serial_no = serial_no + <NUM_LIT:1> <EOL> if len ( Trainset ) == <NUM_LIT:1> : <EOL> return numpy . array ( [ serial_no , - <NUM_LIT:1> , Trainset [ : , <NUM_LIT:5> ] , - <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> elif len ( Trainset ) > <NUM_LIT:1> : <EOL> chosen_feature = random . randrange ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> while True : <EOL> chosen_step_1 = random . randrange ( <NUM_LIT:0> , len ( Trainset ) ) <EOL> chosen_step_2 = random . randrange ( <NUM_LIT:0> , len ( Trainset ) ) <EOL> if chosen_step_2 != chosen_step_1 : <EOL> break <EOL> splitval = numpy . mean ( [ Trainset [ chosen_step_1 ] [ chosen_feature ] , Trainset [ chosen_step_2 ] [ chosen_feature ] ] ) <EOL> ( left_data , right_data ) = self . split ( Trainset , Trainset [ : , chosen_feature ] <= splitval ) <EOL> if ( len ( left_data ) == <NUM_LIT:0> or len ( right_data ) == <NUM_LIT:0> ) : <EOL> return numpy . array ( [ serial_no , - <NUM_LIT:1> , numpy . mean ( Trainset [ : , chosen_feature ] ) , - <NUM_LIT:1> , - <NUM_LIT:1> ] ) <EOL> current = numpy . array ( [ [ serial_no , chosen_feature , splitval , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> left_subtree = self . buildtree ( left_data ) <EOL> right_subtree = self . buildtree ( right_data ) <EOL> self . tree = self . combine_tree ( current , left_subtree , right_subtree ) <EOL> return self . tree <EOL> def treechecker ( self , Tree ) : <EOL> while len ( Tree ) < <NUM_LIT> : <EOL> Tree = self . buildtree ( self . Trainset ) <EOL> return Tree <EOL> def addEvidence ( self , XTrain , Ytrain ) : <EOL> self . XTrain = XTrain <EOL> self . Ytrain = Ytrain <EOL> self . Trainset = numpy . concatenate ( ( XTrain , Ytrain ) , axis = <NUM_LIT:1> ) <EOL> self . forest = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . k ) : <EOL> Tree = self . treechecker ( self . buildtree ( self . Trainset ) ) <EOL> print len ( Tree ) , "<STR_LIT:U+0020>" , i <EOL> self . forest . append ( Tree ) <EOL> global serial_no <EOL> serial_no = <NUM_LIT:0> <EOL> def query ( self , XTest ) : <EOL> return self . queryforest ( XTest ) <EOL> def queryforest ( self , XTest ) : <EOL> mean_val_list = [ ] <EOL> for x_values in XTest : <EOL> Values = [ ] <EOL> Value = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . k ) : <EOL> Value = self . queryone ( x_values , self . forest [ i ] ) <EOL> Values . append ( Value ) <EOL> mean_val = sum ( Values ) / float ( len ( Values ) ) <EOL> mean_val_list . append ( mean_val ) <EOL> return mean_val_list <EOL> def queryone ( self , XTest_single , Tree ) : <EOL> step = <NUM_LIT:0> <EOL> value = <NUM_LIT:0> <EOL> while step <= len ( Tree ) - <NUM_LIT:1> : <EOL> if ( Tree [ step ] [ FEATURE_NUMBER_step ] == <NUM_LIT:1> ) : <EOL> if ( XTest_single [ <NUM_LIT:1> ] <= Tree [ step ] [ SPLIT_VAL_step ] ) : <EOL> step = Tree [ step ] [ LEFT_POINTER ] <EOL> continue <EOL> else : <EOL> step = Tree [ step ] [ RIGHT_POINTER ] <EOL> continue <EOL> elif ( Tree [ step ] [ FEATURE_NUMBER_step ] == - <NUM_LIT:1> ) : <EOL> value = Tree [ step ] [ SPLIT_VAL_step ] <EOL> break <EOL> elif ( Tree [ step ] [ FEATURE_NUMBER_step ] == <NUM_LIT:0> ) : <EOL> if ( XTest_single [ <NUM_LIT:0> ] <= Tree [ step ] [ SPLIT_VAL_step ] ) : <EOL> step = Tree [ step ] [ LEFT_POINTER ] <EOL> continue <EOL> else : <EOL> step = Tree [ step ] [ RIGHT_POINTER ] <EOL> continue <EOL> elif ( Tree [ step ] [ FEATURE_NUMBER_step ] == <NUM_LIT:2> ) : <EOL> if ( XTest_single [ <NUM_LIT:2> ] <= Tree [ step ] [ SPLIT_VAL_step ] ) : <EOL> step = Tree [ step ] [ LEFT_POINTER ] <EOL> continue <EOL> else : <EOL> step = Tree [ step ] [ RIGHT_POINTER ] <EOL> continue <EOL> elif ( Tree [ step ] [ FEATURE_NUMBER_step ] == <NUM_LIT:3> ) : <EOL> if ( XTest_single [ <NUM_LIT:3> ] <= Tree [ step ] [ SPLIT_VAL_step ] ) : <EOL> step = Tree [ step ] [ LEFT_POINTER ] <EOL> continue <EOL> else : <EOL> step = Tree [ step ] [ RIGHT_POINTER ] <EOL> continue <EOL> elif ( Tree [ step ] [ FEATURE_NUMBER_step ] == <NUM_LIT:4> ) : <EOL> if ( XTest_single [ <NUM_LIT:4> ] <= Tree [ step ] [ SPLIT_VAL_step ] ) : <EOL> step = Tree [ step ] [ LEFT_POINTER ] <EOL> continue <EOL> else : <EOL> step = Tree [ step ] [ RIGHT_POINTER ] <EOL> continue <EOL> return value <EOL> def getflatcsv ( self , fname ) : <EOL> inf = open ( fname ) <EOL> return numpy . array ( [ map ( float , s . strip ( ) . split ( '<STR_LIT:U+002C>' ) ) for s in inf . readlines ( ) ] ) </s>
